From 3822bceea2051da61cae99e3c7b87442165adb65 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 16 Oct 2016 16:59:38 +0200 Subject: Move publicpreview over Signed-off-by: Roeland Jago Douma --- apps/files_sharing/ajax/publicpreview.php | 126 --------------------- apps/files_sharing/appinfo/routes.php | 18 +-- .../lib/Controller/PublicPreviewController.php | 108 ++++++++++++++++++ 3 files changed, 119 insertions(+), 133 deletions(-) delete mode 100644 apps/files_sharing/ajax/publicpreview.php create mode 100644 apps/files_sharing/lib/Controller/PublicPreviewController.php (limited to 'apps') 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 @@ - - * @author Georg Ehrke - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * - * @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 - * - */ - -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..0c8a2c36c3d --- /dev/null +++ b/apps/files_sharing/lib/Controller/PublicPreviewController.php @@ -0,0 +1,108 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OCA\Files_Sharing\Controller; + +use OC\PreviewManager; +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\IRequest; +use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IManager as ShareManager; + +class PublicPreviewController extends Controller { + + /** @var ShareManager */ + private $shareManager; + + /** @var PreviewManager */ + private $previewManager; + + public function __construct($appName, + IRequest $request, + ShareManager $shareManger, + PreviewManager $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 === '') { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + if ($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); + } + + $node = $share->getNode(); + + if ($node instanceof Folder) { + try { + $file = $node->get($file); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + } else { + $file = $node; + } + + $f = $this->previewManager->getPreview($file, $x, $y, !$a); + + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + } +} -- cgit v1.2.3 From d720a2fb5719122a0250d63c269380c6ba413755 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 16 Oct 2016 20:42:35 +0200 Subject: Moved over files_versions Signed-off-by: Roeland Jago Douma --- apps/files_versions/ajax/preview.php | 66 ------------ apps/files_versions/appinfo/routes.php | 15 ++- .../lib/Controller/PreviewController.php | 111 +++++++++++++++++++++ apps/files_versions/lib/Storage.php | 2 +- lib/private/Preview/Generator.php | 15 ++- lib/private/PreviewManager.php | 5 +- lib/public/IPreview.php | 3 +- 7 files changed, 137 insertions(+), 80 deletions(-) delete mode 100644 apps/files_versions/ajax/preview.php create mode 100644 apps/files_versions/lib/Controller/PreviewController.php (limited to 'apps') 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 @@ - - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Vincent Petry - * - * @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 - * - */ -\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 @@ * * @author Björn Schießle * @author Jörn Friedrich Dreyer @@ -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..3225febfcff --- /dev/null +++ b/apps/files_versions/lib/Controller/PreviewController.php @@ -0,0 +1,111 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +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 === '') { + 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 $versionFolder */ + $versionFolder = $userFolder->getParent()->get('files_versions'); + $mimeType = $this->mimeTypeDetector->detectPath($file); + $file = $versionFolder->get($file.'.v'.$version); + + if ($file instanceof Folder) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + /** @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); + } catch (\Exception $e) { + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + + } +} 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/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index ea162926afa..d4c38b1cb6a 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -76,11 +76,15 @@ class Generator { * @param int $height * @param bool $crop * @param string $mode + * @param string $mimeType * @return ISimpleFile * @throws NotFoundException */ - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL) { - if (!$this->previewManager->isMimeSupported($file->getMimeType())) { + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { + if ($mimeType === null) { + $mimeType = $file->getMimeType(); + } + if (!$this->previewManager->isMimeSupported($mimeType)) { throw new NotFoundException(); } @@ -91,7 +95,7 @@ class Generator { $previewFolder = $this->getPreviewFolder($file); // Get the max preview and infer the max preview sizes from that - $maxPreview = $this->getMaxPreview($previewFolder, $file); + $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType); list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); // Calculate the preview size @@ -110,10 +114,11 @@ class Generator { /** * @param ISimpleFolder $previewFolder * @param File $file + * @param string $mimeType * @return ISimpleFile * @throws NotFoundException */ - private function getMaxPreview(ISimpleFolder $previewFolder, File $file) { + private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { $nodes = $previewFolder->getDirectoryListing(); foreach ($nodes as $node) { @@ -124,7 +129,7 @@ class Generator { $previewProviders = $this->previewManager->getProviders(); foreach ($previewProviders as $supportedMimeType => $providers) { - if (!preg_match($supportedMimeType, $file->getMimeType())) { + if (!preg_match($supportedMimeType, $mimeType)) { continue; } diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 0cda55751bf..2c17a5f3c80 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -151,11 +151,12 @@ class PreviewManager implements IPreview { * @param int $height * @param bool $crop * @param string $mode + * @param string $mimeType * @return ISimpleFile * @throws NotFoundException * @since 9.2.0 */ - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL) { + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { if ($this->generator === null) { $this->generator = new Generator( $this->rootFolder, @@ -165,7 +166,7 @@ class PreviewManager implements IPreview { ); } - return $this->generator->getPreview($file, $width, $height, $crop, $mode); + return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType); } /** diff --git a/lib/public/IPreview.php b/lib/public/IPreview.php index 0942db4784c..c6417b4d182 100644 --- a/lib/public/IPreview.php +++ b/lib/public/IPreview.php @@ -96,11 +96,12 @@ interface IPreview { * @param int $height * @param bool $crop * @param string $mode + * @param string $mimeType To force a given mimetype for the file (files_versions needs this) * @return ISimpleFile * @throws NotFoundException * @since 9.2.0 */ - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL); + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null); /** * Returns true if the passed mime type is supported -- cgit v1.2.3 From 5d1108519030668cae45d8691b2de7e5629523dc Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 17 Oct 2016 08:42:01 +0200 Subject: Move over files_trashbin Signed-off-by: Roeland Jago Douma --- apps/files_trashbin/ajax/preview.php | 80 ------------- apps/files_trashbin/appinfo/routes.php | 12 +- .../lib/Controller/PreviewController.php | 124 +++++++++++++++++++++ 3 files changed, 134 insertions(+), 82 deletions(-) delete mode 100644 apps/files_trashbin/ajax/preview.php create mode 100644 apps/files_trashbin/lib/Controller/PreviewController.php (limited to 'apps') 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 @@ - - * @author Georg Ehrke - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Vincent Petry - * - * @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 - * - */ -\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 @@ * * @author Lukas Reschke * @author Roeland Jago Douma @@ -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..1e1f68e4317 --- /dev/null +++ b/apps/files_trashbin/lib/Controller/PreviewController.php @@ -0,0 +1,124 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +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); + } catch (\OC\PreviewNotAvailableException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + }catch(\Exception $e) { + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + + } +} -- cgit v1.2.3 From 3d6ee7ffc97af5c426ae7553bcb40f3c28064f93 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 17 Oct 2016 10:16:56 +0200 Subject: Update thumbnail endpoint to new generato Signed-off-by: Roeland Jago Douma --- apps/files/lib/Controller/ApiController.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'apps') 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); } } -- cgit v1.2.3 From 87855aa97b0d90f8036ec40174ac1a3dcbd463e8 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 17 Oct 2016 21:53:23 +0200 Subject: Added genertor helper & tests Signed-off-by: Roeland Jago Douma --- apps/files/tests/Controller/ApiControllerTest.php | 45 ++- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Preview/Generator.php | 44 +-- lib/private/Preview/GeneratorHelper.php | 91 ++++++ lib/private/PreviewManager.php | 7 +- tests/lib/Preview/GeneratorTest.php | 338 ++++++++++++++++++++++ 7 files changed, 477 insertions(+), 50 deletions(-) create mode 100644 lib/private/Preview/GeneratorHelper.php create mode 100644 tests/lib/Preview/GeneratorTest.php (limited to 'apps') 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/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 19905bdbe7b..42cfb8c45e1 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -622,6 +622,7 @@ return array( 'OC\\Preview\\Font' => $baseDir . '/lib/private/Preview/Font.php', 'OC\\Preview\\GIF' => $baseDir . '/lib/private/Preview/GIF.php', 'OC\\Preview\\Generator' => $baseDir . '/lib/private/Preview/Generator.php', + 'OC\\Preview\\GeneratorHelper' => $baseDir . '/lib/private/Preview/GeneratorHelper.php', 'OC\\Preview\\Illustrator' => $baseDir . '/lib/private/Preview/Illustrator.php', 'OC\\Preview\\Image' => $baseDir . '/lib/private/Preview/Image.php', 'OC\\Preview\\JPEG' => $baseDir . '/lib/private/Preview/JPEG.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 82dba9c92f5..d7e937577f2 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -652,6 +652,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Preview\\Font' => __DIR__ . '/../../..' . '/lib/private/Preview/Font.php', 'OC\\Preview\\GIF' => __DIR__ . '/../../..' . '/lib/private/Preview/GIF.php', 'OC\\Preview\\Generator' => __DIR__ . '/../../..' . '/lib/private/Preview/Generator.php', + 'OC\\Preview\\GeneratorHelper' => __DIR__ . '/../../..' . '/lib/private/Preview/GeneratorHelper.php', 'OC\\Preview\\Illustrator' => __DIR__ . '/../../..' . '/lib/private/Preview/Illustrator.php', 'OC\\Preview\\Image' => __DIR__ . '/../../..' . '/lib/private/Preview/Image.php', 'OC\\Preview\\JPEG' => __DIR__ . '/../../..' . '/lib/private/Preview/JPEG.php', diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index d4c38b1cb6a..3d4e9bf3677 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -23,46 +23,43 @@ namespace OC\Preview; -use OC\Files\View; use OCP\Files\File; use OCP\Files\IAppData; -use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; use OCP\IImage; -use OCP\Image as img; use OCP\IPreview; use OCP\Preview\IProvider; class Generator { - /** @var IRootFolder*/ - private $rootFolder; /** @var IPreview */ private $previewManager; /** @var IConfig */ private $config; /** @var IAppData */ private $appData; + /** @var GeneratorHelper */ + private $helper; /** - * @param IRootFolder $rootFolder * @param IConfig $config * @param IPreview $previewManager * @param IAppData $appData + * @param GeneratorHelper $helper */ public function __construct( - IRootFolder $rootFolder, IConfig $config, IPreview $previewManager, - IAppData $appData + IAppData $appData, + GeneratorHelper $helper ) { - $this->rootFolder = $rootFolder; $this->config = $config; $this->previewManager = $previewManager; $this->appData = $appData; + $this->helper = $helper; } /** @@ -88,10 +85,6 @@ class Generator { throw new NotFoundException(); } - /* - * Get the preview folder - * TODO: Separate preview creation from storing previews - */ $previewFolder = $this->getPreviewFolder($file); // Get the max preview and infer the max preview sizes from that @@ -134,17 +127,15 @@ class Generator { } foreach ($providers as $provider) { - $provider = $provider(); + $provider = $this->helper->getProvider($provider); if (!($provider instanceof IProvider)) { continue; } - list($view, $path) = $this->getViewAndPath($file); - $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048); $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048); - $preview = $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view); + $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); if (!($preview instanceof IImage)) { continue; @@ -185,24 +176,7 @@ class Generator { return $path; } - /** - * @param File $file - * @return array - * This is required to create the old view and path - */ - private function getViewAndPath(File $file) { - $owner = $file->getOwner()->getUID(); - - $userFolder = $this->rootFolder->getUserFolder($owner)->getParent(); - $nodes = $userFolder->getById($file->getId()); - - $file = $nodes[0]; - $view = new View($userFolder->getPath()); - $path = $userFolder->getRelativePath($file->getPath()); - - return [$view, $path]; - } /** * @param int $width @@ -300,7 +274,7 @@ class Generator { * @throws NotFoundException */ private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) { - $preview = new img($maxPreview->getContent()); + $preview = $this->helper->getImage($maxPreview); if ($crop) { if ($height !== $preview->height() && $width !== $preview->width()) { diff --git a/lib/private/Preview/GeneratorHelper.php b/lib/private/Preview/GeneratorHelper.php new file mode 100644 index 00000000000..282c46a2a5d --- /dev/null +++ b/lib/private/Preview/GeneratorHelper.php @@ -0,0 +1,91 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OC\Preview; + +use OC\Files\View; +use OCP\Files\File; +use OCP\Files\IRootFolder; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IImage; +use OCP\Image as img; +use OCP\Preview\IProvider; + +/** + * Very small wrapper class to make the generator fully unit testable + */ +class GeneratorHelper { + + /** @var IRootFolder */ + private $rootFolder; + + public function __construct(IRootFolder $rootFolder) { + $this->rootFolder = $rootFolder; + } + + /** + * @param IProvider $provider + * @param File $file + * @param int $maxWidth + * @param int $maxHeight + * @return bool|IImage + */ + public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) { + list($view, $path) = $this->getViewAndPath($file); + return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view); + } + + /** + * @param File $file + * @return array + * This is required to create the old view and path + */ + private function getViewAndPath(File $file) { + $owner = $file->getOwner()->getUID(); + + $userFolder = $this->rootFolder->getUserFolder($owner)->getParent(); + + $nodes = $userFolder->getById($file->getId()); + $file = $nodes[0]; + + $view = new View($userFolder->getPath()); + $path = $userFolder->getRelativePath($file->getPath()); + + return [$view, $path]; + } + + /** + * @param ISimpleFile $maxPreview + * @return IImage + */ + public function getImage(ISimpleFile $maxPreview) { + return new img($maxPreview->getContent()); + } + + /** + * @param $provider + * @return IProvider + */ + public function getProvider($provider) { + return $provider(); + } +} diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 2c17a5f3c80..a2ef9659b3b 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -26,6 +26,7 @@ namespace OC; use OC\Preview\Generator; +use OC\Preview\GeneratorHelper; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\IRootFolder; @@ -159,10 +160,12 @@ class PreviewManager implements IPreview { public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { if ($this->generator === null) { $this->generator = new Generator( - $this->rootFolder, $this->config, $this, - $this->appData + $this->appData, + new GeneratorHelper( + $this->rootFolder + ) ); } diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php new file mode 100644 index 00000000000..d64a0b912e1 --- /dev/null +++ b/tests/lib/Preview/GeneratorTest.php @@ -0,0 +1,338 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace Test\Preview; + +use OC\Preview\Generator; +use OC\Preview\GeneratorHelper; +use OCP\Files\File; +use OCP\Files\IAppData; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; +use OCP\IConfig; +use OCP\IImage; +use OCP\IPreview; +use OCP\Preview\IProvider; + +class GeneratorTest extends \Test\TestCase { + + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; + + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ + private $previewManager; + + /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ + private $appData; + + /** @var GeneratorHelper|\PHPUnit_Framework_MockObject_MockObject */ + private $helper; + + /** @var Generator */ + private $generator; + + public function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->previewManager = $this->createMock(IPreview::class); + $this->appData = $this->createMock(IAppData::class); + $this->helper = $this->createMock(GeneratorHelper::class); + + $this->generator = new Generator( + $this->config, + $this->previewManager, + $this->appData, + $this->helper + ); + } + + public function testGetCachedPreview() { + $file = $this->createMock(File::class); + $file->method('getMimeType') + ->willReturn('myMimeType'); + $file->method('getId') + ->willReturn(42); + + $this->previewManager->method('isMimeSupported') + ->with($this->equalTo('myMimeType')) + ->willReturn(true); + + $previewFolder = $this->createMock(ISimpleFolder::class); + $this->appData->method('getFolder') + ->with($this->equalTo(42)) + ->willReturn($previewFolder); + + $maxPreview = $this->createMock(ISimpleFile::class); + $maxPreview->method('getName') + ->willReturn('1000-1000-max.png'); + + $previewFolder->method('getDirectoryListing') + ->willReturn([$maxPreview]); + + $previewFile = $this->createMock(ISimpleFile::class); + + $previewFolder->method('getFile') + ->with($this->equalTo('128-128.png')) + ->willReturn($previewFile); + + $result = $this->generator->getPreview($file, 100, 100); + $this->assertSame($previewFile, $result); + } + + public function testGetNewPreview() { + $file = $this->createMock(File::class); + $file->method('getMimeType') + ->willReturn('myMimeType'); + $file->method('getId') + ->willReturn(42); + + $this->previewManager->method('isMimeSupported') + ->with($this->equalTo('myMimeType')) + ->willReturn(true); + + $previewFolder = $this->createMock(ISimpleFolder::class); + $this->appData->method('getFolder') + ->with($this->equalTo(42)) + ->willThrowException(new NotFoundException()); + + $this->appData->method('newFolder') + ->with($this->equalTo(42)) + ->willReturn($previewFolder); + + $this->config->method('getSystemValue') + ->will($this->returnCallback(function($key, $defult) { + return $defult; + })); + + $invalidProvider = $this->createMock(IProvider::class); + $validProvider = $this->createMock(IProvider::class); + + $this->previewManager->method('getProviders') + ->willReturn([ + '/image\/png/' => ['wrongProvider'], + '/myMimeType/' => ['brokenProvider', 'invalidProvider', 'validProvider'], + ]); + + $this->helper->method('getProvider') + ->will($this->returnCallback(function($provider) use ($invalidProvider, $validProvider) { + if ($provider === 'wrongProvider') { + $this->fail('Wrongprovider should not be constructed!'); + } else if ($provider === 'brokenProvider') { + return false; + } else if ($provider === 'invalidProvider') { + return $invalidProvider; + } else if ($provider === 'validProvider') { + return $validProvider; + } + $this->fail('Unexpected provider requested'); + })); + + $image = $this->createMock(IImage::class); + $image->method('width')->willReturn(2048); + $image->method('height')->willReturn(2048); + + $this->helper->method('getThumbnail') + ->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) { + if ($provider === $validProvider) { + return $image; + } else { + return false; + } + })); + + $image->method('data') + ->willReturn('my data'); + + $maxPreview = $this->createMock(ISimpleFile::class); + $maxPreview->method('getName')->willReturn('2048-2048-max.png'); + + $previewFile = $this->createMock(ISimpleFile::class); + + $previewFolder->method('getDirectoryListing') + ->willReturn([]); + $previewFolder->method('newFile') + ->will($this->returnCallback(function($filename) use ($maxPreview, $previewFile) { + if ($filename === '2048-2048-max.png') { + return $maxPreview; + } else if ($filename === '128-128.png') { + return $previewFile; + } + $this->fail('Unexpected file'); + })); + + $maxPreview->expects($this->once()) + ->method('putContent') + ->with($this->equalTo('my data')); + + $previewFolder->method('getFile') + ->with($this->equalTo('128-128.png')) + ->willThrowException(new NotFoundException()); + + $image = $this->createMock(IImage::class); + $this->helper->method('getImage') + ->with($this->equalTo($maxPreview)) + ->willReturn($image); + + $image->expects($this->once()) + ->method('resize') + ->with(128); + $image->method('data') + ->willReturn('my resized data'); + + $previewFile->expects($this->once()) + ->method('putContent') + ->with('my resized data'); + + $result = $this->generator->getPreview($file, 100, 100); + $this->assertSame($previewFile, $result); + } + + public function testInvalidMimeType() { + $this->expectException(NotFoundException::class); + + $file = $this->createMock(File::class); + + $this->previewManager->method('isMimeSupported') + ->with('invalidType') + ->willReturn(false); + + $this->generator->getPreview($file, 0, 0, true, IPreview::MODE_COVER, 'invalidType'); + } + + public function testNoProvider() { + $file = $this->createMock(File::class); + $file->method('getMimeType') + ->willReturn('myMimeType'); + $file->method('getId') + ->willReturn(42); + + $this->previewManager->method('isMimeSupported') + ->with($this->equalTo('myMimeType')) + ->willReturn(true); + + $previewFolder = $this->createMock(ISimpleFolder::class); + $this->appData->method('getFolder') + ->with($this->equalTo(42)) + ->willReturn($previewFolder); + + $previewFolder->method('getDirectoryListing') + ->willReturn([]); + + $this->previewManager->method('getProviders') + ->willReturn([]); + + $this->expectException(NotFoundException::class); + $this->generator->getPreview($file, 100, 100); + } + + public function dataSize() { + return [ + [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512], + [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024], + [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 512, 512], + [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 512, 512], + + [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512], + [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024], + + [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126], + [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100], + + [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048], + [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048], + + + [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256], + [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512], + [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 512, 512], + [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 512, 512], + + [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512], + [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256], + + [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512], + [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512], + ]; + } + + /** + * @dataProvider dataSize + * + * @param int $maxX + * @param int $maxY + * @param int $reqX + * @param int $reqY + * @param bool $crop + * @param string $mode + * @param int $expectedX + * @param int $expectedY + */ + public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) { + $file = $this->createMock(File::class); + $file->method('getMimeType') + ->willReturn('myMimeType'); + $file->method('getId') + ->willReturn(42); + + $this->previewManager->method('isMimeSupported') + ->with($this->equalTo('myMimeType')) + ->willReturn(true); + + $previewFolder = $this->createMock(ISimpleFolder::class); + $this->appData->method('getFolder') + ->with($this->equalTo(42)) + ->willReturn($previewFolder); + + $maxPreview = $this->createMock(ISimpleFile::class); + $maxPreview->method('getName') + ->willReturn($maxX . '-' . $maxY . '-max.png'); + + $previewFolder->method('getDirectoryListing') + ->willReturn([$maxPreview]); + + $filename = $expectedX . '-' . $expectedY; + if ($crop) { + $filename .= '-crop'; + } + $filename .= '.png'; + $previewFolder->method('getFile') + ->with($this->equalTo($filename)) + ->willThrowException(new NotFoundException()); + + $image = $this->createMock(IImage::class); + $this->helper->method('getImage') + ->with($this->equalTo($maxPreview)) + ->willReturn($image); + $image->method('height')->willReturn($maxY); + $image->method('width')->willReturn($maxX); + + $preview = $this->createMock(ISimpleFile::class); + $previewFolder->method('newFile') + ->with($this->equalTo($filename)) + ->willReturn($preview); + + $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode); + $this->assertSame($preview, $result); + } +} -- cgit v1.2.3 From 55af6b45f72e0717d7b6f07b69bc0f12f4fb4cd9 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 18 Oct 2016 10:36:57 +0200 Subject: More tests * PreviewController test * PublicPreview test * Versions Preview test * Trash Preview test Signed-off-by: Roeland Jago Douma --- .../lib/Controller/PublicPreviewController.php | 34 ++-- .../Controller/PublicPreviewControllerTest.php | 193 ++++++++++++++++++ .../lib/Controller/PreviewController.php | 5 - .../tests/Controller/PreviewControllerTest.php | 181 +++++++++++++++++ .../lib/Controller/PreviewController.php | 14 +- .../tests/Controller/PreviewControllerTest.php | 167 +++++++++++++++ core/Controller/PreviewController.php | 11 +- tests/Core/Controller/PreviewControllerTest.php | 223 +++++++++++++++++++++ 8 files changed, 783 insertions(+), 45 deletions(-) create mode 100644 apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php create mode 100644 apps/files_trashbin/tests/Controller/PreviewControllerTest.php create mode 100644 apps/files_versions/tests/Controller/PreviewControllerTest.php create mode 100644 tests/Core/Controller/PreviewControllerTest.php (limited to 'apps') diff --git a/apps/files_sharing/lib/Controller/PublicPreviewController.php b/apps/files_sharing/lib/Controller/PublicPreviewController.php index 0c8a2c36c3d..b91b84c9c34 100644 --- a/apps/files_sharing/lib/Controller/PublicPreviewController.php +++ b/apps/files_sharing/lib/Controller/PublicPreviewController.php @@ -22,7 +22,6 @@ */ namespace OCA\Files_Sharing\Controller; -use OC\PreviewManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -30,6 +29,7 @@ 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; @@ -39,13 +39,13 @@ class PublicPreviewController extends Controller { /** @var ShareManager */ private $shareManager; - /** @var PreviewManager */ + /** @var IPreview */ private $previewManager; public function __construct($appName, IRequest $request, ShareManager $shareManger, - PreviewManager $previewManager) { + IPreview $previewManager) { parent::__construct($appName, $request); $this->shareManager = $shareManger; @@ -71,11 +71,7 @@ class PublicPreviewController extends Controller { $a = false ) { - if ($t === '') { - return new DataResponse([], Http::STATUS_BAD_REQUEST); - } - - if ($x === 0 || $y === 0) { + if ($t === '' || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } @@ -89,20 +85,18 @@ class PublicPreviewController extends Controller { return new DataResponse([], Http::STATUS_FORBIDDEN); } - $node = $share->getNode(); - - if ($node instanceof Folder) { - try { + try { + $node = $share->getNode(); + if ($node instanceof Folder) { $file = $node->get($file); - } catch (NotFoundException $e) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + } else { + $file = $node; } - } else { - $file = $node; - } - - $f = $this->previewManager->getPreview($file, $x, $y, !$a); - return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + $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 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +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/lib/Controller/PreviewController.php b/apps/files_trashbin/lib/Controller/PreviewController.php index 1e1f68e4317..c73b1c17c16 100644 --- a/apps/files_trashbin/lib/Controller/PreviewController.php +++ b/apps/files_trashbin/lib/Controller/PreviewController.php @@ -114,11 +114,6 @@ class PreviewController extends Controller { return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); } catch (NotFoundException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); - } catch (\OC\PreviewNotAvailableException $e) { - return new DataResponse([], Http::STATUS_NOT_FOUND); - }catch(\Exception $e) { - return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } - } } 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 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +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/lib/Controller/PreviewController.php b/apps/files_versions/lib/Controller/PreviewController.php index 3225febfcff..8d961f47ee6 100644 --- a/apps/files_versions/lib/Controller/PreviewController.php +++ b/apps/files_versions/lib/Controller/PreviewController.php @@ -78,11 +78,7 @@ class PreviewController extends Controller { $y = 44, $version = '' ) { - if($file === '' && $version === '') { - return new DataResponse([], Http::STATUS_BAD_REQUEST); - } - - if($x === 0 || $y === 0) { + if($file === '' || $version === '' || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } @@ -93,19 +89,11 @@ class PreviewController extends Controller { $mimeType = $this->mimeTypeDetector->detectPath($file); $file = $versionFolder->get($file.'.v'.$version); - if ($file instanceof Folder) { - return new DataResponse([], Http::STATUS_BAD_REQUEST); - } - /** @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); - } catch (\Exception $e) { - return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } - } } 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 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +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); + } + +} diff --git a/core/Controller/PreviewController.php b/core/Controller/PreviewController.php index f54681a856b..3a245374e96 100644 --- a/core/Controller/PreviewController.php +++ b/core/Controller/PreviewController.php @@ -23,11 +23,11 @@ namespace OC\Core\Controller; -use OC\DatabaseException; use OCP\AppFramework\Controller; use OCP\Files\File; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IPreview; @@ -86,11 +86,7 @@ class PreviewController extends Controller { $forceIcon = true, $mode = 'fill') { - if ($file === '') { - return new DataResponse([], Http::STATUS_BAD_REQUEST); - } - - if ($x === 0 || $y === 0) { + if ($file === '' || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } @@ -109,9 +105,10 @@ class PreviewController extends Controller { try { $f = $this->preview->getPreview($file, $x, $y, !$a, $mode); + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); } catch (NotFoundException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } - return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + } } diff --git a/tests/Core/Controller/PreviewControllerTest.php b/tests/Core/Controller/PreviewControllerTest.php new file mode 100644 index 00000000000..63a26761344 --- /dev/null +++ b/tests/Core/Controller/PreviewControllerTest.php @@ -0,0 +1,223 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace Tests\Core\Controller; + +use OC\Core\Controller\PreviewController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IPreview; +use OCP\IRequest; + +class PreviewControllerTest extends \Test\TestCase { + + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ + private $previewManager; + + /** @var PreviewController|\PHPUnit_Framework_MockObject_MockObject */ + private $controller; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->userId = 'user'; + $this->previewManager = $this->createMock(IPreview::class); + + $this->controller = new PreviewController( + 'core', + $this->createMock(IRequest::class), + $this->previewManager, + $this->rootFolder, + $this->userId + ); + } + + 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 testFileNotFound() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo($this->userId)) + ->willReturn($userFolder); + + $userFolder->method('get') + ->with($this->equalTo('file')) + ->willThrowException(new NotFoundException()); + + $res = $this->controller->getPreview('file'); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + + public function testNotAFile() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo($this->userId)) + ->willReturn($userFolder); + + $folder = $this->createMock(Folder::class); + $userFolder->method('get') + ->with($this->equalTo('file')) + ->willReturn($folder); + + $res = $this->controller->getPreview('file'); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + + public function testNoPreviewAndNoIcon() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo($this->userId)) + ->willReturn($userFolder); + + $file = $this->createMock(File::class); + $userFolder->method('get') + ->with($this->equalTo('file')) + ->willReturn($file); + + $this->previewManager->method('isAvailable') + ->with($this->equalTo($file)) + ->willReturn(false); + + $res = $this->controller->getPreview('file', 10, 10, true, false); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + + public function testForbiddenFile() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo($this->userId)) + ->willReturn($userFolder); + + $file = $this->createMock(File::class); + $userFolder->method('get') + ->with($this->equalTo('file')) + ->willReturn($file); + + $this->previewManager->method('isAvailable') + ->with($this->equalTo($file)) + ->willReturn(true); + + $file->method('isReadable') + ->willReturn(false); + + $res = $this->controller->getPreview('file', 10, 10, true, true); + $expected = new DataResponse([], Http::STATUS_FORBIDDEN); + + $this->assertEquals($expected, $res); + } + + public function testNoPreview() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo($this->userId)) + ->willReturn($userFolder); + + $file = $this->createMock(File::class); + $userFolder->method('get') + ->with($this->equalTo('file')) + ->willReturn($file); + + $this->previewManager->method('isAvailable') + ->with($this->equalTo($file)) + ->willReturn(true); + + $file->method('isReadable') + ->willReturn(true); + + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode')) + ->willThrowException(new NotFoundException()); + + $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode'); + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals($expected, $res); + } + + public function testValidPreview() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo($this->userId)) + ->willReturn($userFolder); + + $file = $this->createMock(File::class); + $userFolder->method('get') + ->with($this->equalTo('file')) + ->willReturn($file); + + $this->previewManager->method('isAvailable') + ->with($this->equalTo($file)) + ->willReturn(true); + + $file->method('isReadable') + ->willReturn(true); + + $preview = $this->createMock(ISimpleFile::class); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode')) + ->willReturn($preview); + $preview->method('getMimeType') + ->willReturn('myMime'); + + $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode'); + $expected = new Http\FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + + $this->assertEquals($expected, $res); + } +} -- cgit v1.2.3