diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2016-11-03 21:07:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-03 21:07:16 +0100 |
commit | c0bbae28f0bb1041a22645691fe653c642f0d1cb (patch) | |
tree | c75e0f48a2c0c44af5eca2062592855d044f1f10 /apps | |
parent | 8bf57462ea52cc2f4300b6fa097ae23100882c83 (diff) | |
parent | f44cd7aed37d84137902ce8f65aabd115ae1adef (diff) | |
download | nextcloud-server-c0bbae28f0bb1041a22645691fe653c642f0d1cb.tar.gz nextcloud-server-c0bbae28f0bb1041a22645691fe653c642f0d1cb.zip |
Merge pull request #1741 from nextcloud/new_preview
Improve previews
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/lib/Controller/ApiController.php | 23 | ||||
-rw-r--r-- | apps/files/tests/Controller/ApiControllerTest.php | 45 | ||||
-rw-r--r-- | apps/files_sharing/ajax/publicpreview.php | 126 | ||||
-rw-r--r-- | apps/files_sharing/appinfo/routes.php | 18 | ||||
-rw-r--r-- | apps/files_sharing/lib/Controller/PublicPreviewController.php | 102 | ||||
-rw-r--r-- | apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php | 193 | ||||
-rw-r--r-- | apps/files_trashbin/ajax/preview.php | 80 | ||||
-rw-r--r-- | apps/files_trashbin/appinfo/routes.php | 12 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Controller/PreviewController.php | 119 | ||||
-rw-r--r-- | apps/files_trashbin/tests/Controller/PreviewControllerTest.php | 181 | ||||
-rw-r--r-- | apps/files_versions/ajax/preview.php | 66 | ||||
-rw-r--r-- | apps/files_versions/appinfo/routes.php | 15 | ||||
-rw-r--r-- | apps/files_versions/lib/Controller/PreviewController.php | 99 | ||||
-rw-r--r-- | apps/files_versions/lib/Storage.php | 2 | ||||
-rw-r--r-- | apps/files_versions/tests/Controller/PreviewControllerTest.php | 167 |
15 files changed, 942 insertions, 306 deletions
diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php index 138b68601cb..790da4a184a 100644 --- a/apps/files/lib/Controller/ApiController.php +++ b/apps/files/lib/Controller/ApiController.php @@ -31,11 +31,13 @@ namespace OCA\Files\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Controller; +use OCP\Files\File; use OCP\Files\Folder; +use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IRequest; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Http\DataDisplayResponse; +use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\Response; use OCA\Files\Service\TagService; use OCP\IPreview; @@ -101,18 +103,27 @@ class ApiController extends Controller { * @param int $x * @param int $y * @param string $file URL-encoded filename - * @return DataResponse|DataDisplayResponse + * @return DataResponse|FileDisplayResponse */ public function getThumbnail($x, $y, $file) { if($x < 1 || $y < 1) { return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); } - $preview = $this->previewManager->createPreview('files/'.$file, $x, $y, true); - if ($preview->valid()) { - return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']); - } else { + try { + $file = $this->userFolder->get($file); + if ($file instanceof Folder) { + throw new NotFoundException(); + } + + /** @var File $file */ + $preview = $this->previewManager->getPreview($file, $x, $y, true); + + return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]); + } catch (NotFoundException $e) { return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); + } catch (\Exception $e) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); } } diff --git a/apps/files/tests/Controller/ApiControllerTest.php b/apps/files/tests/Controller/ApiControllerTest.php index 4b7bec065a0..56d0f6c8dee 100644 --- a/apps/files/tests/Controller/ApiControllerTest.php +++ b/apps/files/tests/Controller/ApiControllerTest.php @@ -28,11 +28,15 @@ namespace OCA\Files\Controller; use OC\Files\FileInfo; use OCP\AppFramework\Http; +use OCP\Files\File; +use OCP\Files\Folder; use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\StorageNotAvailableException; use OCP\IConfig; use OCP\IUser; use OCP\IUserSession; +use OCP\Share\IManager; use Test\TestCase; use OCP\IRequest; use OCA\Files\Service\TagService; @@ -54,7 +58,7 @@ class ApiControllerTest extends TestCase { private $request; /** @var TagService */ private $tagService; - /** @var IPreview */ + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ private $preview; /** @var ApiController */ private $apiController; @@ -62,11 +66,13 @@ class ApiControllerTest extends TestCase { private $shareManager; /** @var \OCP\IConfig */ private $config; - /** @var \OC\Files\Node\Folder */ + /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */ private $userFolder; public function setUp() { - $this->request = $this->getMockBuilder('\OCP\IRequest') + parent::setUp(); + + $this->request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); $this->user = $this->createMock(IUser::class); @@ -77,17 +83,17 @@ class ApiControllerTest extends TestCase { $userSession->expects($this->any()) ->method('getUser') ->will($this->returnValue($this->user)); - $this->tagService = $this->getMockBuilder('\OCA\Files\Service\TagService') + $this->tagService = $this->getMockBuilder(TagService::class) ->disableOriginalConstructor() ->getMock(); - $this->shareManager = $this->getMockBuilder('\OCP\Share\IManager') + $this->shareManager = $this->getMockBuilder(IManager::class) ->disableOriginalConstructor() ->getMock(); - $this->preview = $this->getMockBuilder('\OCP\IPreview') + $this->preview = $this->getMockBuilder(IPreview::class) ->disableOriginalConstructor() ->getMock(); $this->config = $this->createMock(IConfig::class); - $this->userFolder = $this->getMockBuilder('\OC\Files\Node\Folder') + $this->userFolder = $this->getMockBuilder(Folder::class) ->disableOriginalConstructor() ->getMock(); @@ -153,28 +159,41 @@ class ApiControllerTest extends TestCase { } public function testGetThumbnailInvalidSize() { + $this->userFolder->method('get') + ->with($this->equalTo('')) + ->willThrowException(new NotFoundException()); $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, '')); } public function testGetThumbnailInvaidImage() { + $file = $this->createMock(File::class); + $this->userFolder->method('get') + ->with($this->equalTo('unknown.jpg')) + ->willReturn($file); $this->preview->expects($this->once()) - ->method('createPreview') - ->with('files/unknown.jpg', 10, 10, true) - ->willReturn(new Image); + ->method('getPreview') + ->with($file, 10, 10, true) + ->willThrowException(new NotFoundException()); $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg')); } public function testGetThumbnail() { + $file = $this->createMock(File::class); + $this->userFolder->method('get') + ->with($this->equalTo('known.jpg')) + ->willReturn($file); + $preview = $this->createMock(ISimpleFile::class); $this->preview->expects($this->once()) - ->method('createPreview') - ->with('files/known.jpg', 10, 10, true) - ->willReturn(new Image(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + ->method('getPreview') + ->with($this->equalTo($file), 10, 10, true) + ->willReturn($preview); $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg'); $this->assertEquals(Http::STATUS_OK, $ret->getStatus()); + $this->assertInstanceOf(Http\FileDisplayResponse::class, $ret); } public function testUpdateFileSorting() { diff --git a/apps/files_sharing/ajax/publicpreview.php b/apps/files_sharing/ajax/publicpreview.php deleted file mode 100644 index 4c60f9c20ca..00000000000 --- a/apps/files_sharing/ajax/publicpreview.php +++ /dev/null @@ -1,126 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Georg Ehrke <georg@owncloud.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -OCP\JSON::checkAppEnabled('files_sharing'); - -\OC_User::setIncognitoMode(true); - -$file = array_key_exists('file', $_GET) ? (string) $_GET['file'] : ''; -$maxX = array_key_exists('x', $_GET) ? (int) $_GET['x'] : '32'; -$maxY = array_key_exists('y', $_GET) ? (int) $_GET['y'] : '32'; -$scalingUp = array_key_exists('scalingup', $_GET) ? (bool) $_GET['scalingup'] : true; -$token = array_key_exists('t', $_GET) ? (string) $_GET['t'] : ''; -$keepAspect = array_key_exists('a', $_GET) ? true : false; - -if($token === ''){ - \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST); - \OCP\Util::writeLog('core-preview', 'No token parameter was passed', \OCP\Util::DEBUG); - exit; -} - -$linkedItem = \OCP\Share::getShareByToken($token); -$shareManager = \OC::$server->getShareManager(); -$share = $shareManager->getShareByToken($token); -if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { - OCP\JSON::error(array('data' => 'Share is not readable.')); - exit(); -} - -if($linkedItem === false || ($linkedItem['item_type'] !== 'file' && $linkedItem['item_type'] !== 'folder')) { - \OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND); - \OCP\Util::writeLog('core-preview', 'Passed token parameter is not valid', \OCP\Util::DEBUG); - exit; -} - -if(!isset($linkedItem['uid_owner']) || !isset($linkedItem['file_source'])) { - \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR); - \OCP\Util::writeLog('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OCP\Util::WARN); - exit; -} - -$rootLinkItem = OCP\Share::resolveReShare($linkedItem); -$userId = $rootLinkItem['uid_owner']; - -OCP\JSON::checkUserExists($rootLinkItem['uid_owner']); -\OC_Util::setupFS($userId); -\OC\Files\Filesystem::initMountPoints($userId); -$view = new \OC\Files\View('/' . $userId . '/files'); - -$pathId = $linkedItem['file_source']; -$path = $view->getPath($pathId); - -if($path === null) { - \OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND); - \OCP\Util::writeLog('core-preview', 'Could not resolve file for shared item', \OCP\Util::WARN); - exit; -} - -$pathInfo = $view->getFileInfo($path); -$sharedFile = null; - -if($linkedItem['item_type'] === 'folder') { - $isValid = \OC\Files\Filesystem::isValidPath($file); - if(!$isValid) { - \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST); - \OCP\Util::writeLog('core-preview', 'Passed filename is not valid, might be malicious (file:"' . $file . '";ip:"' . \OC::$server->getRequest()->getRemoteAddress() . '")', \OCP\Util::WARN); - exit; - } - $sharedFile = \OC\Files\Filesystem::normalizePath($file); -} - -if($linkedItem['item_type'] === 'file') { - $parent = $pathInfo['parent']; - $path = $view->getPath($parent); - $sharedFile = $pathInfo['name']; -} - -$path = \OC\Files\Filesystem::normalizePath($path, false); -if(substr($path, 0, 1) === '/') { - $path = substr($path, 1); -} - -if($maxX === 0 || $maxY === 0) { - \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST); - \OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG); - exit; -} - -$root = 'files/' . $path; - -try{ - $preview = new \OC\Preview($userId, $root); - $preview->setFile($sharedFile); - $preview->setMaxX($maxX); - $preview->setMaxY($maxY); - $preview->setScalingUp($scalingUp); - $preview->setKeepAspect($keepAspect); - - $preview->showPreview(); -} catch (\Exception $e) { - \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR); - \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); -} diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 267e53829da..439d46253d4 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -39,6 +39,17 @@ $application->registerRoutes($this, [ 'url' => '/testremote', 'verb' => 'GET' ], + [ + 'name' => 'PublicPreview#getPreview', + 'url' => '/publicpreview', + 'verb' => 'GET', + ], + + [ + 'name' => 'PublicPreview#getPreview', + 'url' => '/ajax/publicpreview.php', + 'verb' => 'GET', + ], ], 'ocs' => [ /* @@ -114,15 +125,8 @@ $application->registerRoutes($this, [ ]); /** @var $this \OCP\Route\IRouter */ -$this->create('core_ajax_public_preview', '/publicpreview')->action( - function() { - require_once __DIR__ . '/../ajax/publicpreview.php'; - }); - $this->create('files_sharing_ajax_list', 'ajax/list.php') ->actionInclude('files_sharing/ajax/list.php'); -$this->create('files_sharing_ajax_publicpreview', 'ajax/publicpreview.php') - ->actionInclude('files_sharing/ajax/publicpreview.php'); $this->create('sharing_external_shareinfo', '/shareinfo') ->actionInclude('files_sharing/ajax/shareinfo.php'); diff --git a/apps/files_sharing/lib/Controller/PublicPreviewController.php b/apps/files_sharing/lib/Controller/PublicPreviewController.php new file mode 100644 index 00000000000..b91b84c9c34 --- /dev/null +++ b/apps/files_sharing/lib/Controller/PublicPreviewController.php @@ -0,0 +1,102 @@ +<?php +/** + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files_Sharing\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Constants; +use OCP\Files\Folder; +use OCP\Files\NotFoundException; +use OCP\IPreview; +use OCP\IRequest; +use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IManager as ShareManager; + +class PublicPreviewController extends Controller { + + /** @var ShareManager */ + private $shareManager; + + /** @var IPreview */ + private $previewManager; + + public function __construct($appName, + IRequest $request, + ShareManager $shareManger, + IPreview $previewManager) { + parent::__construct($appName, $request); + + $this->shareManager = $shareManger; + $this->previewManager = $previewManager; + } + + /** + * @PublicPage + * @NoCSRFRequired + * + * @param string $file + * @param int $x + * @param int $y + * @param string $t + * @param bool $a + * @return DataResponse|FileDisplayResponse + */ + public function getPreview( + $file = '', + $x = 32, + $y = 32, + $t = '', + $a = false + ) { + + if ($t === '' || $x === 0 || $y === 0) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + try { + $share = $this->shareManager->getShareByToken($t); + } catch (ShareNotFound $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { + return new DataResponse([], Http::STATUS_FORBIDDEN); + } + + try { + $node = $share->getNode(); + if ($node instanceof Folder) { + $file = $node->get($file); + } else { + $file = $node; + } + + $f = $this->previewManager->getPreview($file, $x, $y, !$a); + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + } +} diff --git a/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php b/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php new file mode 100644 index 00000000000..174abbb6f60 --- /dev/null +++ b/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php @@ -0,0 +1,193 @@ +<?php +/** + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files_Sharing\Tests\Controller; + +use OCA\Files_Sharing\Controller\PublicPreviewController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Constants; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IPreview; +use OCP\IRequest; +use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IManager; +use OCP\Share\IShare; +use Punic\Data; +use Test\TestCase; + +class PublicPreviewControllerTest extends TestCase { + + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ + private $previewManager; + + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + private $shareManager; + + /** @var PublicPreviewController */ + private $controller; + + public function setUp() { + parent::setUp(); + + $this->previewManager = $this->createMock(IPreview::class); + $this->shareManager = $this->createMock(IManager::class); + + $this->controller = new PublicPreviewController( + 'files_sharing', + $this->createMock(IRequest::class), + $this->shareManager, + $this->previewManager + ); + } + + public function testInvalidToken() { + $res = $this->controller->getPreview('file', 10, 10, ''); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidWidth() { + $res = $this->controller->getPreview('file', 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidHeight() { + $res = $this->controller->getPreview('file', 10, 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidShare() { + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willThrowException(new ShareNotFound()); + + $res = $this->controller->getPreview('file', 10, 10, 'token'); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + + public function testShareNotAccessable() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(0); + + $res = $this->controller->getPreview('file', 10, 10, 'token'); + $expected = new DataResponse([], Http::STATUS_FORBIDDEN); + + $this->assertEquals($expected, $res); + } + + public function testPreviewFile() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + + $file = $this->createMock(File::class); + $share->method('getNode') + ->willReturn($file); + + $preview = $this->createMock(ISimpleFile::class); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, false) + ->willReturn($preview); + + $preview->method('getMimeType') + ->willReturn('myMime'); + + $res = $this->controller->getPreview('file', 10, 10, 'token', true); + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + $this->assertEquals($expected, $res); + } + + public function testPreviewFolderInvalidFile() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + + $folder = $this->createMock(Folder::class); + $share->method('getNode') + ->willReturn($folder); + + $folder->method('get') + ->with($this->equalTo('file')) + ->willThrowException(new NotFoundException()); + + $res = $this->controller->getPreview('file', 10, 10, 'token', true); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + $this->assertEquals($expected, $res); + } + + + public function testPreviewFolderValidFile() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + + $folder = $this->createMock(Folder::class); + $share->method('getNode') + ->willReturn($folder); + + $file = $this->createMock(File::class); + $folder->method('get') + ->with($this->equalTo('file')) + ->willReturn($file); + + $preview = $this->createMock(ISimpleFile::class); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, false) + ->willReturn($preview); + + $preview->method('getMimeType') + ->willReturn('myMime'); + + $res = $this->controller->getPreview('file', 10, 10, 'token', true); + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + $this->assertEquals($expected, $res); + } +} diff --git a/apps/files_trashbin/ajax/preview.php b/apps/files_trashbin/ajax/preview.php deleted file mode 100644 index 3f895161f00..00000000000 --- a/apps/files_trashbin/ajax/preview.php +++ /dev/null @@ -1,80 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Georg Ehrke <georg@owncloud.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -\OC_Util::checkLoggedIn(); -\OC::$server->getSession()->close(); - -if(!\OC_App::isEnabled('files_trashbin')){ - exit; -} - -$file = array_key_exists('file', $_GET) ? (string) $_GET['file'] : ''; -$maxX = array_key_exists('x', $_GET) ? (int) $_GET['x'] : '44'; -$maxY = array_key_exists('y', $_GET) ? (int) $_GET['y'] : '44'; -$scalingUp = array_key_exists('scalingup', $_GET) ? (bool) $_GET['scalingup'] : true; - -if($file === '') { - \OC_Response::setStatus(400); //400 Bad Request - \OCP\Util::writeLog('core-preview', 'No file parameter was passed', \OCP\Util::DEBUG); - exit; -} - -if($maxX === 0 || $maxY === 0) { - \OC_Response::setStatus(400); //400 Bad Request - \OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG); - exit; -} - -try{ - $preview = new \OC\Preview(\OC_User::getUser(), 'files_trashbin/files', $file); - $view = new \OC\Files\View('/'.\OC_User::getUser(). '/files_trashbin/files'); - if ($view->is_dir($file)) { - $mimetype = 'httpd/unix-directory'; - } else { - $pathInfo = pathinfo(ltrim($file, '/')); - $fileName = $pathInfo['basename']; - // if in root dir - if ($pathInfo['dirname'] === '.') { - // cut off the .d* suffix - $i = strrpos($fileName, '.'); - if ($i !== false) { - $fileName = substr($fileName, 0, $i); - } - } - $mimetype = \OC::$server->getMimeTypeDetector()->detectPath($fileName); - } - $preview->setMimetype($mimetype); - $preview->setMaxX($maxX); - $preview->setMaxY($maxY); - $preview->setScalingUp($scalingUp); - - $preview->showPreview(); -} catch (\OC\PreviewNotAvailableException $e) { - \OC_Response::setStatus(404); -}catch(\Exception $e) { - \OC_Response::setStatus(500); - \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); -} diff --git a/apps/files_trashbin/appinfo/routes.php b/apps/files_trashbin/appinfo/routes.php index 2f4988c9dcc..5241bec742e 100644 --- a/apps/files_trashbin/appinfo/routes.php +++ b/apps/files_trashbin/appinfo/routes.php @@ -1,6 +1,7 @@ <?php /** * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * * @author Lukas Reschke <lukas@statuscode.ch> * @author Roeland Jago Douma <roeland@famdouma.nl> @@ -25,9 +26,16 @@ namespace OCA\Files_Trashbin\AppInfo; $application = new Application(); +$application->registerRoutes($this, [ + 'routes' => [ + [ + 'name' => 'Preview#getPreview', + 'url' => '/ajax/preview.php', + 'verb' => 'GET', + ], + ], +]); -$this->create('core_ajax_trashbin_preview', 'ajax/preview.php') - ->actionInclude('files_trashbin/ajax/preview.php'); $this->create('files_trashbin_ajax_delete', 'ajax/delete.php') ->actionInclude('files_trashbin/ajax/delete.php'); $this->create('files_trashbin_ajax_isEmpty', 'ajax/isEmpty.php') diff --git a/apps/files_trashbin/lib/Controller/PreviewController.php b/apps/files_trashbin/lib/Controller/PreviewController.php new file mode 100644 index 00000000000..c73b1c17c16 --- /dev/null +++ b/apps/files_trashbin/lib/Controller/PreviewController.php @@ -0,0 +1,119 @@ +<?php +/** + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files_Trashbin\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\IPreview; +use OCP\IRequest; + +class PreviewController extends Controller { + + /** @var IRootFolder */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var IMimeTypeDetector */ + private $mimeTypeDetector; + + /** @var IPreview */ + private $previewManager; + + /** + * @param string $appName + * @param IRequest $request + * @param IRootFolder $rootFolder + * @param $userId + * @param IMimeTypeDetector $mimeTypeDetector + * @param IPreview $previewManager + */ + public function __construct($appName, + IRequest $request, + IRootFolder $rootFolder, + $userId, + IMimeTypeDetector $mimeTypeDetector, + IPreview $previewManager) { + parent::__construct($appName, $request); + + $this->rootFolder = $rootFolder; + $this->userId = $userId; + $this->mimeTypeDetector = $mimeTypeDetector; + $this->previewManager = $previewManager; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @param string $file + * @param int $x + * @param int $y + * @return DataResponse|Http\FileDisplayResponse + */ + public function getPreview( + $file = '', + $x = 44, + $y = 44 + ) { + if ($file === '') { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + if ($x === 0 || $y === 0) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + try { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + /** @var Folder $trash */ + $trash = $userFolder->getParent()->get('files_trashbin/files'); + $trashFile = $trash->get($file); + + if ($trashFile instanceof Folder) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + /** @var File $trashFile */ + $fileName = $trashFile->getName(); + $i = strrpos($fileName, '.'); + if ($i !== false) { + $fileName = substr($fileName, 0, $i); + } + + $mimeType = $this->mimeTypeDetector->detectPath($fileName); + + $f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType); + return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + } +} diff --git a/apps/files_trashbin/tests/Controller/PreviewControllerTest.php b/apps/files_trashbin/tests/Controller/PreviewControllerTest.php new file mode 100644 index 00000000000..685e4cebfd1 --- /dev/null +++ b/apps/files_trashbin/tests/Controller/PreviewControllerTest.php @@ -0,0 +1,181 @@ +<?php +/** + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files_Trashbin\Tests\Controller; + +use OCA\Files_Trashbin\Controller\PreviewController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IPreview; +use OCP\IRequest; +use Test\TestCase; + +class PreviewControllerTest extends TestCase { + + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var IMimeTypeDetector|\PHPUnit_Framework_MockObject_MockObject */ + private $mimeTypeDetector; + + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ + private $previewManager; + + /** @var PreviewController */ + private $controller; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->userId = 'user'; + $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class); + $this->previewManager = $this->createMock(IPreview::class); + + $this->controller = new PreviewController( + 'files_versions', + $this->createMock(IRequest::class), + $this->rootFolder, + $this->userId, + $this->mimeTypeDetector, + $this->previewManager + ); + } + + public function testInvalidFile() { + $res = $this->controller->getPreview(''); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidWidth() { + $res = $this->controller->getPreview('file', 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidHeight() { + $res = $this->controller->getPreview('file', 10, 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testValidPreview() { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $trash = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->userId) + ->willReturn($userFolder); + $userFolder->method('getParent') + ->willReturn($userRoot); + $userRoot->method('get') + ->with('files_trashbin/files') + ->willReturn($trash); + + $this->mimeTypeDetector->method('detectPath') + ->with($this->equalTo('file')) + ->willReturn('myMime'); + + $file = $this->createMock(File::class); + $trash->method('get') + ->with($this->equalTo('file.1234')) + ->willReturn($file); + $file->method('getName') + ->willReturn('file.1234'); + + $preview = $this->createMock(ISimpleFile::class); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime') + ->willReturn($preview); + $preview->method('getMimeType') + ->willReturn('previewMime'); + + $res = $this->controller->getPreview('file.1234', 10, 10); + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']); + + $this->assertEquals($expected, $res); + } + + public function testTrashFileNotFound() { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $trash = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->userId) + ->willReturn($userFolder); + $userFolder->method('getParent') + ->willReturn($userRoot); + $userRoot->method('get') + ->with('files_trashbin/files') + ->willReturn($trash); + + $trash->method('get') + ->with($this->equalTo('file.1234')) + ->willThrowException(new NotFoundException()); + + $res = $this->controller->getPreview('file.1234', 10, 10); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + + public function testTrashFolder() { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $trash = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->userId) + ->willReturn($userFolder); + $userFolder->method('getParent') + ->willReturn($userRoot); + $userRoot->method('get') + ->with('files_trashbin/files') + ->willReturn($trash); + + $folder = $this->createMock(Folder::class); + $trash->method('get') + ->with($this->equalTo('folder')) + ->willReturn($folder); + + $res = $this->controller->getPreview('folder', 10, 10); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } +} diff --git a/apps/files_versions/ajax/preview.php b/apps/files_versions/ajax/preview.php deleted file mode 100644 index 78e830d7c8f..00000000000 --- a/apps/files_versions/ajax/preview.php +++ /dev/null @@ -1,66 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -\OC_Util::checkLoggedIn(); - -if(!\OC_App::isEnabled('files_versions')){ - exit; -} - -$file = array_key_exists('file', $_GET) ? (string) urldecode($_GET['file']) : ''; -$maxX = array_key_exists('x', $_GET) ? (int) $_GET['x'] : 44; -$maxY = array_key_exists('y', $_GET) ? (int) $_GET['y'] : 44; -$version = array_key_exists('version', $_GET) ? $_GET['version'] : ''; -$scalingUp = array_key_exists('scalingup', $_GET) ? (bool) $_GET['scalingup'] : true; - -if($file === '' && $version === '') { - \OC_Response::setStatus(400); //400 Bad Request - \OCP\Util::writeLog('versions-preview', 'No file parameter was passed', \OCP\Util::DEBUG); - exit; -} - -if($maxX === 0 || $maxY === 0) { - \OC_Response::setStatus(400); //400 Bad Request - \OCP\Util::writeLog('versions-preview', 'x and/or y set to 0', \OCP\Util::DEBUG); - exit; -} - -try { - list($user, $file) = \OCA\Files_Versions\Storage::getUidAndFilename($file); - $preview = new \OC\Preview($user, 'files_versions', $file.'.v'.$version); - $mimetype = \OC::$server->getMimeTypeDetector()->detectPath($file); - $preview->setMimetype($mimetype); - $preview->setMaxX($maxX); - $preview->setMaxY($maxY); - $preview->setScalingUp($scalingUp); - - $preview->showPreview(); -} catch (\OCP\Files\NotFoundException $e) { - \OC_Response::setStatus(404); - \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); -} catch (\Exception $e) { - \OC_Response::setStatus(500); - \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); -} diff --git a/apps/files_versions/appinfo/routes.php b/apps/files_versions/appinfo/routes.php index c2b686c38f3..434ff9d26ac 100644 --- a/apps/files_versions/appinfo/routes.php +++ b/apps/files_versions/appinfo/routes.php @@ -1,6 +1,7 @@ <?php /** * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * * @author Björn Schießle <bjoern@schiessle.org> * @author Jörn Friedrich Dreyer <jfd@butonic.de> @@ -29,13 +30,17 @@ namespace OCA\Files_Versions\AppInfo; $application = new Application(); +$application->registerRoutes($this, [ + 'routes' => [ + [ + 'name' => 'Preview#getPreview', + 'url' => '/preview', + 'verb' => 'GET', + ], + ], +]); /** @var $this \OCP\Route\IRouter */ -$this->create('core_ajax_versions_preview', '/preview')->action( -function() { - require_once __DIR__ . '/../ajax/preview.php'; -}); - $this->create('files_versions_download', 'download.php') ->actionInclude('files_versions/download.php'); $this->create('files_versions_ajax_getVersions', 'ajax/getVersions.php') diff --git a/apps/files_versions/lib/Controller/PreviewController.php b/apps/files_versions/lib/Controller/PreviewController.php new file mode 100644 index 00000000000..8d961f47ee6 --- /dev/null +++ b/apps/files_versions/lib/Controller/PreviewController.php @@ -0,0 +1,99 @@ +<?php +/** + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files_Versions\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\IPreview; +use OCP\IRequest; + +class PreviewController extends Controller { + + /** @var IRootFolder */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var IMimeTypeDetector */ + private $mimeTypeDetector; + + /** @var IPreview */ + private $previewManager; + + public function __construct($appName, + IRequest $request, + IRootFolder $rootFolder, + $userId, + IMimeTypeDetector $mimeTypeDetector, + IPreview $previewManager) { + parent::__construct($appName, $request); + + $this->rootFolder = $rootFolder; + $this->userId = $userId; + $this->mimeTypeDetector = $mimeTypeDetector; + $this->previewManager = $previewManager; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @param string $file + * @param int $x + * @param int $y + * @param string $version + * @return DataResponse|FileDisplayResponse + */ + public function getPreview( + $file = '', + $x = 44, + $y = 44, + $version = '' + ) { + if($file === '' || $version === '' || $x === 0 || $y === 0) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + try { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + /** @var Folder $versionFolder */ + $versionFolder = $userFolder->getParent()->get('files_versions'); + $mimeType = $this->mimeTypeDetector->detectPath($file); + $file = $versionFolder->get($file.'.v'.$version); + + /** @var File $file */ + $f = $this->previewManager->getPreview($file, $x, $y, true, IPreview::MODE_FILL, $mimeType); + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + } +} diff --git a/apps/files_versions/lib/Storage.php b/apps/files_versions/lib/Storage.php index cf0e09f64ca..e7c2c8b7593 100644 --- a/apps/files_versions/lib/Storage.php +++ b/apps/files_versions/lib/Storage.php @@ -462,7 +462,7 @@ class Storage { if (empty($userFullPath)) { $versions[$key]['preview'] = ''; } else { - $versions[$key]['preview'] = \OCP\Util::linkToRoute('core_ajax_versions_preview', array('file' => $userFullPath, 'version' => $timestamp)); + $versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]); } $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename); $versions[$key]['name'] = $versionedFile; diff --git a/apps/files_versions/tests/Controller/PreviewControllerTest.php b/apps/files_versions/tests/Controller/PreviewControllerTest.php new file mode 100644 index 00000000000..384f43cf495 --- /dev/null +++ b/apps/files_versions/tests/Controller/PreviewControllerTest.php @@ -0,0 +1,167 @@ +<?php +/** + * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files_Versions\Tests\Controller; + +use OCA\Files_Versions\Controller\PreviewController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IPreview; +use OCP\IRequest; +use Test\TestCase; + +class PreviewControllerTest extends TestCase { + + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var IMimeTypeDetector|\PHPUnit_Framework_MockObject_MockObject */ + private $mimeTypeDetector; + + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ + private $previewManager; + + /** @var PreviewController */ + private $controller; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->userId = 'user'; + $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class); + $this->previewManager = $this->createMock(IPreview::class); + + $this->controller = new PreviewController( + 'files_versions', + $this->createMock(IRequest::class), + $this->rootFolder, + $this->userId, + $this->mimeTypeDetector, + $this->previewManager + ); + } + + public function testInvalidFile() { + $res = $this->controller->getPreview(''); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidWidth() { + $res = $this->controller->getPreview('file', 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidHeight() { + $res = $this->controller->getPreview('file', 10, 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testInvalidVersion() { + $res = $this->controller->getPreview('file', 10, 0); + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); + + $this->assertEquals($expected, $res); + } + + public function testValidPreview() { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $versions = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->userId) + ->willReturn($userFolder); + $userFolder->method('getParent') + ->willReturn($userRoot); + $userRoot->method('get') + ->with('files_versions') + ->willReturn($versions); + + $this->mimeTypeDetector->method('detectPath') + ->with($this->equalTo('file')) + ->willReturn('myMime'); + + $file = $this->createMock(File::class); + $versions->method('get') + ->with($this->equalTo('file.v42')) + ->willReturn($file); + + $preview = $this->createMock(ISimpleFile::class); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime') + ->willReturn($preview); + $preview->method('getMimeType') + ->willReturn('previewMime'); + + $res = $this->controller->getPreview('file', 10, 10, '42'); + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']); + + $this->assertEquals($expected, $res); + } + + public function testVersionNotFound() { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $versions = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->userId) + ->willReturn($userFolder); + $userFolder->method('getParent') + ->willReturn($userRoot); + $userRoot->method('get') + ->with('files_versions') + ->willReturn($versions); + + $this->mimeTypeDetector->method('detectPath') + ->with($this->equalTo('file')) + ->willReturn('myMime'); + + $file = $this->createMock(File::class); + $versions->method('get') + ->with($this->equalTo('file.v42')) + ->willThrowException(new NotFoundException()); + + $res = $this->controller->getPreview('file', 10, 10, '42'); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + +} |