diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-18 01:51:49 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-18 01:51:49 +0300 |
commit | b9035581998136c19cf4bcb31fedd7f244d48bec (patch) | |
tree | e4a4165899029808742417ba4b2b1252f0b42104 /controller | |
parent | 73563afdb879b28e95ed437326e6284c076a6c31 (diff) |
Add Preview API
Diffstat (limited to 'controller')
-rw-r--r-- | controller/filesapicontroller.php | 7 | ||||
-rw-r--r-- | controller/preview.php | 236 | ||||
-rw-r--r-- | controller/previewapicontroller.php | 114 | ||||
-rw-r--r-- | controller/previewcontroller.php | 203 | ||||
-rw-r--r-- | controller/previewpubliccontroller.php (renamed from controller/publicpreviewcontroller.php) | 4 |
5 files changed, 356 insertions, 208 deletions
diff --git a/controller/filesapicontroller.php b/controller/filesapicontroller.php index 4e2c5168..283a4e65 100644 --- a/controller/filesapicontroller.php +++ b/controller/filesapicontroller.php @@ -64,12 +64,7 @@ class FilesApiController extends ApiController { * * Returns a list of all media files available to the authenticated user * - * * Authentication can be via a login/password or a token/(password) - * * For private galleries, it returns all media files, with the full path from the root - * folder For public galleries, the path starts from the folder the link gives access to - * (virtual root) - * * An exception is only caught in case something really wrong happens. As we don't test - * files before including them in the list, we may return some bad apples + * @see FilesController::getList() * * @param string $location a path representing the current album in the app * @param string $features the list of supported features diff --git a/controller/preview.php b/controller/preview.php new file mode 100644 index 00000000..d479ca99 --- /dev/null +++ b/controller/preview.php @@ -0,0 +1,236 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * @author Robin Appelman <icewind@owncloud.com> + * + * @copyright Olivier Paroz 2014-2015 + * @copyright Robin Appelman 2012-2014 + */ + +namespace OCA\Gallery\Controller; + +use OCP\IURLGenerator; +use OCP\IEventSource; +use OCP\ILogger; +use OCP\Files\File; + +use OCP\AppFramework\Http; + +use OCA\Gallery\Service\ServiceException; +use OCA\Gallery\Service\NotFoundServiceException; +use OCA\Gallery\Service\ThumbnailService; +use OCA\Gallery\Service\PreviewService; +use OCA\Gallery\Service\DownloadService; + +/** + * Class Preview + * + * @package OCA\Gallery\Controller + */ +trait Preview { + + /** + * @var IURLGenerator + */ + private $urlGenerator; + /** + * @var ThumbnailService + */ + private $thumbnailService; + /** + * @var PreviewService + */ + private $previewService; + /** + * @var DownloadService + */ + private $downloadService; + /** + * @var IEventSource + */ + private $eventSource; + /** + * @var ILogger + */ + private $logger; + /** + * @type bool + */ + private $download = false; + + + /** + * Retrieves the thumbnail to send back to the browser + * + * The thumbnail is either a resized preview of the file or the original file + * Thumbnails are base64encoded before getting sent back + * + * + * @param int $fileId the ID of the file of which we need a thumbnail preview of + * @param bool $square whether the thumbnail should be square + * @param double $scale whether we're allowed to scale the preview up + * + * @return array<string,array|string> + */ + private function getThumbnail($fileId, $square, $scale) { + list($width, $height, $aspect, $animatedPreview, $base64Encode) = + $this->thumbnailService->getThumbnailSpecs($square, $scale); + /** @type File $file */ + list($file, $preview, $status, $type) = + $this->getData( + $fileId, $width, $height, $aspect, $animatedPreview, $base64Encode + ); + if ($preview === null) { + if ($status !== Http::STATUS_NOT_FOUND) { + $preview = ['preview' => null, 'mimetype' => $file->getMimeType()]; + } + } else { + if ($type === 'preview') { + $preview['preview'] = + $this->previewService->previewValidator($square, $base64Encode); + } + } + + return [$preview, $status]; + } + + /** + * Returns either a generated preview, the file as-is or an empty object + * + * @param int $fileId + * @param int $width + * @param int $height + * @param bool $keepAspect + * @param bool $animatedPreview + * @param bool $base64Encode + * + * @return array<string,\OC_Image|string> + * + * @throws NotFoundServiceException + */ + private function getData( + $fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false + ) { + try { + /** @type File $file */ + $file = $this->previewService->getResourceFromId($fileId); + if (!is_null($file)) { + $previewRequired = $this->isPreviewRequired($file, $animatedPreview); + $data = $this->getPreviewData( + $file, $previewRequired, $width, $height, $keepAspect, $base64Encode + ); + } else { + // Uncaught problem, should never reach this point... + $data = $this->getErrorData(Http::STATUS_NOT_FOUND); + } + } catch (ServiceException $exception) { + $file = null; + $data = $this->getExceptionData($exception); + } + array_unshift($data, $file); + + return $data; + } + + /** + * Returns true if we need to generate a preview for that file + * + * @param $file + * @param bool $animatedPreview + * + * @return bool + */ + private function isPreviewRequired($file, $animatedPreview) { + $previewRequired = false; + + if (!$this->download) { + $previewRequired = + $this->previewService->isPreviewRequired($file, $animatedPreview); + } + + return $previewRequired; + } + + /** + * @param $file + * @param $previewRequired + * @param $width + * @param $height + * @param $keepAspect + * @param $base64Encode + * + * @return array + */ + private function getPreviewData( + $file, $previewRequired, $width, $height, $keepAspect, $base64Encode + ) { + $status = Http::STATUS_OK; + if ($previewRequired) { + $type = 'preview'; + $preview = $this->previewService->createPreview( + $file, $width, $height, $keepAspect, $base64Encode + ); + } else { + $type = 'download'; + $preview = $this->downloadService->downloadFile($file, $base64Encode); + } + if (!$preview) { + $type = 'error'; + $status = Http::STATUS_INTERNAL_SERVER_ERROR; + $preview = null; + } + + return [$preview, $status, $type]; + } + + /** + * Returns an error array + * + * @param $status + * + * @return array<null|int|string> + */ + private function getErrorData($status) { + return [null, $status, 'error']; + } + + /** + * Returns an error array + * + * @param $exception + * + * @return array<null|int|string> + */ + private function getExceptionData($exception) { + if ($exception instanceof NotFoundServiceException) { + $status = Http::STATUS_NOT_FOUND; + } else { + $status = Http::STATUS_INTERNAL_SERVER_ERROR; + } + + return $this->getErrorData($status); + } + + /** + * Returns an URL based on the HTTP status code + * + * @param $status + * + * @return string + */ + private function getErrorUrl($status) { + return $this->urlGenerator->linkToRoute( + $this->appName . '.page.error_page', + [ + 'message' => 'There was a problem accessing the file', + 'code' => $status + ] + ); + } + +} diff --git a/controller/previewapicontroller.php b/controller/previewapicontroller.php new file mode 100644 index 00000000..9c505c1e --- /dev/null +++ b/controller/previewapicontroller.php @@ -0,0 +1,114 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Controller; + +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\IEventSource; +use OCP\ILogger; +use OCP\Files\File; + +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\JSONResponse; + +use OCA\Gallery\Http\ImageResponse; +use OCA\Gallery\Service\ThumbnailService; +use OCA\Gallery\Service\PreviewService; +use OCA\Gallery\Service\DownloadService; + +/** + * Class PreviewApiController + * + * @package OCA\Gallery\Controller + */ +class PreviewApiController extends ApiController { + + use Preview; + use JsonHttpError; + + /** + * @var IEventSource + */ + private $eventSource; + + /** + * Constructor + * + * @param string $appName + * @param IRequest $request + * @param IURLGenerator $urlGenerator + * @param ThumbnailService $thumbnailService + * @param PreviewService $previewService + * @param DownloadService $downloadService + * @param IEventSource $eventSource + * @param ILogger $logger + */ + public function __construct( + $appName, + IRequest $request, + IURLGenerator $urlGenerator, + ThumbnailService $thumbnailService, + PreviewService $previewService, + DownloadService $downloadService, + IEventSource $eventSource, + ILogger $logger + ) { + parent::__construct($appName, $request); + + $this->urlGenerator = $urlGenerator; + $this->thumbnailService = $thumbnailService; + $this->previewService = $previewService; + $this->downloadService = $downloadService; + $this->eventSource = $eventSource; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * + * Sends either a large preview of the requested file or the original file itself + * + * @param int $fileId the ID of the file of which we need a large preview of + * @param int $width + * @param int $height + * @param string|null $download + * + * @return ImageResponse|RedirectResponse|Http\JSONResponse + */ + public function getPreview($fileId, $width, $height, $download) { + if (!is_null($download)) { + $this->download = true; + } + /** @type File $file */ + list($file, $preview, $status) = $this->getData($fileId, $width, $height); + + if ($preview === null) { + if ($this->download) { + $url = $this->getErrorUrl($status); + + return new RedirectResponse($url); + } else { + + return new JSONResponse(['message' => 'Oh Nooooes!', 'success' => false], $status); + } + } + $preview['name'] = $file->getName(); + + return new ImageResponse($preview, $status); + } + +} diff --git a/controller/previewcontroller.php b/controller/previewcontroller.php index 2b16091b..01ac9a00 100644 --- a/controller/previewcontroller.php +++ b/controller/previewcontroller.php @@ -6,10 +6,8 @@ * later. See the COPYING file. * * @author Olivier Paroz <owncloud@interfasys.ch> - * @author Robin Appelman <icewind@owncloud.com> * - * @copyright Olivier Paroz 2014-2015 - * @copyright Robin Appelman 2012-2014 + * @copyright Olivier Paroz 2015 */ namespace OCA\Gallery\Controller; @@ -26,8 +24,6 @@ use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\JSONResponse; use OCA\Gallery\Http\ImageResponse; -use OCA\Gallery\Service\ServiceException; -use OCA\Gallery\Service\NotFoundServiceException; use OCA\Gallery\Service\ThumbnailService; use OCA\Gallery\Service\PreviewService; use OCA\Gallery\Service\DownloadService; @@ -39,36 +35,13 @@ use OCA\Gallery\Service\DownloadService; */ class PreviewController extends Controller { + use Preview; use JsonHttpError; /** - * @var IURLGenerator - */ - private $urlGenerator; - /** - * @var ThumbnailService - */ - private $thumbnailService; - /** - * @var PreviewService - */ - private $previewService; - /** - * @var DownloadService - */ - private $downloadService; - /** * @var IEventSource */ private $eventSource; - /** - * @var ILogger - */ - private $logger; - /** - * @type bool - */ - private $download = false; /** * Constructor @@ -141,8 +114,7 @@ class PreviewController extends Controller { /** * @NoAdminRequired * - * Sends either a large preview of the requested file or the - * original file itself + * Sends either a large preview of the requested file or the original file itself * * @param int $fileId the ID of the file of which we need a large preview of * @param int $width @@ -173,173 +145,4 @@ class PreviewController extends Controller { return new ImageResponse($preview, $status); } - /** - * Retrieves the thumbnail to send back to the browser - * - * The thumbnail is either a resized preview of the file or the original file - * Thumbnails are base64encoded before getting sent back - * - * - * @param int $fileId the ID of the file of which we need a thumbnail preview of - * @param bool $square whether the thumbnail should be square - * @param double $scale whether we're allowed to scale the preview up - * - * @return array<string,array|string> - */ - private function getThumbnail($fileId, $square, $scale) { - list($width, $height, $aspect, $animatedPreview, $base64Encode) = - $this->thumbnailService->getThumbnailSpecs($square, $scale); - /** @type File $file */ - list($file, $preview, $status, $type) = - $this->getData( - $fileId, $width, $height, $aspect, $animatedPreview, $base64Encode - ); - if ($preview === null) { - if ($status !== Http::STATUS_NOT_FOUND) { - $preview = ['preview' => null, 'mimetype' => $file->getMimeType()]; - } - } else { - if ($type === 'preview') { - $preview['preview'] = - $this->previewService->previewValidator($square, $base64Encode); - } - } - - return [$preview, $status]; - } - - /** - * Returns either a generated preview, the file as-is or an empty object - * - * @param int $fileId - * @param int $width - * @param int $height - * @param bool $keepAspect - * @param bool $animatedPreview - * @param bool $base64Encode - * - * @return array<string,\OC_Image|string> - * - * @throws NotFoundServiceException - */ - private function getData( - $fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false - ) { - try { - /** @type File $file */ - $file = $this->previewService->getResourceFromId($fileId); - if (!is_null($file)) { - $previewRequired = $this->isPreviewRequired($file, $animatedPreview); - $data = $this->getPreviewData( - $file, $previewRequired, $width, $height, $keepAspect, $base64Encode - ); - } else { - // Uncaught problem, should never reach this point... - $data = $this->getErrorData(Http::STATUS_NOT_FOUND); - } - } catch (ServiceException $exception) { - $file = null; - $data = $this->getExceptionData($exception); - } - array_unshift($data, $file); - - return $data; - } - - /** - * Returns true if we need to generate a preview for that file - * - * @param $file - * @param bool $animatedPreview - * - * @return bool - */ - private function isPreviewRequired($file, $animatedPreview) { - $previewRequired = false; - - if (!$this->download) { - $previewRequired = - $this->previewService->isPreviewRequired($file, $animatedPreview); - } - - return $previewRequired; - } - - /** - * @param $file - * @param $previewRequired - * @param $width - * @param $height - * @param $keepAspect - * @param $base64Encode - * - * @return array - */ - private function getPreviewData( - $file, $previewRequired, $width, $height, $keepAspect, $base64Encode - ) { - $status = Http::STATUS_OK; - if ($previewRequired) { - $type = 'preview'; - $preview = $this->previewService->createPreview( - $file, $width, $height, $keepAspect, $base64Encode - ); - } else { - $type = 'download'; - $preview = $this->downloadService->downloadFile($file, $base64Encode); - } - if (!$preview) { - $type = 'error'; - $status = Http::STATUS_INTERNAL_SERVER_ERROR; - $preview = null; - } - - return [$preview, $status, $type]; - } - - /** - * Returns an error array - * - * @param $status - * - * @return array<null|int|string> - */ - private function getErrorData($status) { - return [null, $status, 'error']; - } - - /** - * Returns an error array - * - * @param $exception - * - * @return array<null|int|string> - */ - private function getExceptionData($exception) { - if ($exception instanceof NotFoundServiceException) { - $status = Http::STATUS_NOT_FOUND; - } else { - $status = Http::STATUS_INTERNAL_SERVER_ERROR; - } - - return $this->getErrorData($status); - } - - /** - * Returns an URL based on the HTTP status code - * - * @param $status - * - * @return string - */ - private function getErrorUrl($status) { - return $this->urlGenerator->linkToRoute( - $this->appName . '.page.error_page', - [ - 'message' => 'There was a problem accessing the file', - 'code' => $status - ] - ); - } - } diff --git a/controller/publicpreviewcontroller.php b/controller/previewpubliccontroller.php index d7e83d56..0570e746 100644 --- a/controller/publicpreviewcontroller.php +++ b/controller/previewpubliccontroller.php @@ -13,14 +13,14 @@ namespace OCA\Gallery\Controller; /** - * Class PublicPreviewController + * Class PreviewPublicController * * Note: Type casting only works if the "@param" parameters are also included in this class as * their not yet inherited * * @package OCA\Gallery\Controller */ -class PublicPreviewController extends PreviewController { +class PreviewPublicController extends PreviewController { /** * @PublicPage |