Previews on for all trashbin files

* Previews possible for all files in the trashbin
* Set caching
* Use the fileid to find the file

* Fix test

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-05-23 14:25:51 +02:00
parent bc008f8d5f
commit 608456d2a9
No known key found for this signature in database
GPG Key ID: F941078878347C0C
5 changed files with 73 additions and 55 deletions

View File

@ -29,7 +29,7 @@ $application->registerRoutes($this, [
'routes' => [ 'routes' => [
[ [
'name' => 'Preview#getPreview', 'name' => 'Preview#getPreview',
'url' => '/ajax/preview.php', 'url' => '/preview',
'verb' => 'GET', 'verb' => 'GET',
], ],
], ],

View File

@ -260,7 +260,7 @@
}, },
generatePreviewUrl: function(urlSpec) { generatePreviewUrl: function(urlSpec) {
return OC.generateUrl('/apps/files_trashbin/ajax/preview.php?') + $.param(urlSpec); return OC.generateUrl('/apps/files_trashbin/preview?') + $.param(urlSpec);
}, },
getDownloadUrl: function() { getDownloadUrl: function() {

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
* *
@ -26,6 +27,7 @@ namespace OCA\Files_Trashbin\Controller;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeDetector;
@ -48,45 +50,36 @@ class PreviewController extends Controller {
/** @var IPreview */ /** @var IPreview */
private $previewManager; private $previewManager;
/** /** @var ITimeFactory */
* @param string $appName private $time;
* @param IRequest $request
* @param IRootFolder $rootFolder public function __construct(string $appName,
* @param $userId
* @param IMimeTypeDetector $mimeTypeDetector
* @param IPreview $previewManager
*/
public function __construct($appName,
IRequest $request, IRequest $request,
IRootFolder $rootFolder, IRootFolder $rootFolder,
$userId, string $userId,
IMimeTypeDetector $mimeTypeDetector, IMimeTypeDetector $mimeTypeDetector,
IPreview $previewManager) { IPreview $previewManager,
ITimeFactory $time) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->userId = $userId; $this->userId = $userId;
$this->mimeTypeDetector = $mimeTypeDetector; $this->mimeTypeDetector = $mimeTypeDetector;
$this->previewManager = $previewManager; $this->previewManager = $previewManager;
$this->time = $time;
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* *
* @param string $file
* @param int $x
* @param int $y
* @return DataResponse|Http\FileDisplayResponse * @return DataResponse|Http\FileDisplayResponse
*/ */
public function getPreview( public function getPreview(
$file = '', int $fileId,
$x = 44, int $x = 44,
$y = 44 int $y = 44
) { ) {
if ($file === '') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
if ($x === 0 || $y === 0) { if ($x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST); return new DataResponse([], Http::STATUS_BAD_REQUEST);
@ -96,23 +89,41 @@ class PreviewController extends Controller {
$userFolder = $this->rootFolder->getUserFolder($this->userId); $userFolder = $this->rootFolder->getUserFolder($this->userId);
/** @var Folder $trash */ /** @var Folder $trash */
$trash = $userFolder->getParent()->get('files_trashbin/files'); $trash = $userFolder->getParent()->get('files_trashbin/files');
$trashFile = $trash->get($file); $trashFiles = $trash->getById($fileId);
if (empty($trashFiles)) {
throw new NotFoundException();
}
$trashFile = array_pop($trashFiles);
if ($trashFile instanceof Folder) { if ($trashFile instanceof Folder) {
return new DataResponse([], Http::STATUS_BAD_REQUEST); return new DataResponse([], Http::STATUS_BAD_REQUEST);
} }
/** @var File $trashFile */ /*
$fileName = $trashFile->getName(); * Files in the root of the trashbin are timetamped.
$i = strrpos($fileName, '.'); * So we have to strip that in order to properly detect the mimetype of the file.
if ($i !== false) { */
$fileName = substr($fileName, 0, $i); if ($trashFile->getParent()->getPath() === $trash->getPath()) {
/** @var File $trashFile */
$fileName = $trashFile->getName();
$i = strrpos($fileName, '.');
if ($i !== false) {
$fileName = substr($fileName, 0, $i);
}
$mimeType = $this->mimeTypeDetector->detectPath($fileName);
} else {
$mimeType = $this->mimeTypeDetector->detectPath($trashFile->getName());
} }
$mimeType = $this->mimeTypeDetector->detectPath($fileName);
$f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType); $f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType);
return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
// Cache previews for 24H
$response->cacheFor(3600 * 24);
return $response;
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND); return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {

View File

@ -116,10 +116,9 @@ class Helper {
*/ */
public static function formatFileInfos($fileInfos) { public static function formatFileInfos($fileInfos) {
$files = array(); $files = array();
$id = 0;
foreach ($fileInfos as $i) { foreach ($fileInfos as $i) {
$entry = \OCA\Files\Helper::formatFileInfo($i); $entry = \OCA\Files\Helper::formatFileInfo($i);
$entry['id'] = $id++; $entry['id'] = $i->getId();
$entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
$entry['permissions'] = \OCP\Constants::PERMISSION_READ; $entry['permissions'] = \OCP\Constants::PERMISSION_READ;
$files[] = $entry; $files[] = $entry;

View File

@ -26,6 +26,7 @@ use OCA\Files_Trashbin\Controller\PreviewController;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeDetector;
@ -50,6 +51,9 @@ class PreviewControllerTest extends TestCase {
/** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */
private $previewManager; private $previewManager;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
private $time;
/** @var PreviewController */ /** @var PreviewController */
private $controller; private $controller;
@ -60,6 +64,7 @@ class PreviewControllerTest extends TestCase {
$this->userId = 'user'; $this->userId = 'user';
$this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class); $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
$this->previewManager = $this->createMock(IPreview::class); $this->previewManager = $this->createMock(IPreview::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->controller = new PreviewController( $this->controller = new PreviewController(
'files_versions', 'files_versions',
@ -67,26 +72,20 @@ class PreviewControllerTest extends TestCase {
$this->rootFolder, $this->rootFolder,
$this->userId, $this->userId,
$this->mimeTypeDetector, $this->mimeTypeDetector,
$this->previewManager $this->previewManager,
$this->time
); );
} }
public function testInvalidFile() {
$res = $this->controller->getPreview('');
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expected, $res);
}
public function testInvalidWidth() { public function testInvalidWidth() {
$res = $this->controller->getPreview('file', 0); $res = $this->controller->getPreview(42, 0);
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expected, $res); $this->assertEquals($expected, $res);
} }
public function testInvalidHeight() { public function testInvalidHeight() {
$res = $this->controller->getPreview('file', 10, 0); $res = $this->controller->getPreview(42, 10, 0);
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expected, $res); $this->assertEquals($expected, $res);
@ -111,12 +110,15 @@ class PreviewControllerTest extends TestCase {
->willReturn('myMime'); ->willReturn('myMime');
$file = $this->createMock(File::class); $file = $this->createMock(File::class);
$trash->method('get') $trash->method('getById')
->with($this->equalTo('file.1234')) ->with($this->equalTo(42))
->willReturn($file); ->willReturn([$file]);
$file->method('getName') $file->method('getName')
->willReturn('file.1234'); ->willReturn('file.1234');
$file->method('getParent')
->willReturn($trash);
$preview = $this->createMock(ISimpleFile::class); $preview = $this->createMock(ISimpleFile::class);
$this->previewManager->method('getPreview') $this->previewManager->method('getPreview')
->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime') ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
@ -124,8 +126,14 @@ class PreviewControllerTest extends TestCase {
$preview->method('getMimeType') $preview->method('getMimeType')
->willReturn('previewMime'); ->willReturn('previewMime');
$res = $this->controller->getPreview('file.1234', 10, 10); $this->time->method('getTime')
->willReturn(1337);
$this->overwriteService(ITimeFactory::class, $this->time);
$res = $this->controller->getPreview(42, 10, 10);
$expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']); $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
$expected->cacheFor(3600 * 24);
$this->assertEquals($expected, $res); $this->assertEquals($expected, $res);
} }
@ -144,11 +152,11 @@ class PreviewControllerTest extends TestCase {
->with('files_trashbin/files') ->with('files_trashbin/files')
->willReturn($trash); ->willReturn($trash);
$trash->method('get') $trash->method('getById')
->with($this->equalTo('file.1234')) ->with($this->equalTo(42))
->willThrowException(new NotFoundException()); ->willReturn([]);
$res = $this->controller->getPreview('file.1234', 10, 10); $res = $this->controller->getPreview(42, 10, 10);
$expected = new DataResponse([], Http::STATUS_NOT_FOUND); $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
$this->assertEquals($expected, $res); $this->assertEquals($expected, $res);
@ -169,11 +177,11 @@ class PreviewControllerTest extends TestCase {
->willReturn($trash); ->willReturn($trash);
$folder = $this->createMock(Folder::class); $folder = $this->createMock(Folder::class);
$trash->method('get') $trash->method('getById')
->with($this->equalTo('folder')) ->with($this->equalTo(43))
->willReturn($folder); ->willReturn([$folder]);
$res = $this->controller->getPreview('folder', 10, 10); $res = $this->controller->getPreview(43, 10, 10);
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expected, $res); $this->assertEquals($expected, $res);