diff options
author | Olivier Paroz <github@oparoz.com> | 2015-03-31 21:17:31 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-05-31 22:54:11 +0300 |
commit | 80c1016d8cd9bf2ab0e7e516f3074aea5c6a96a5 (patch) | |
tree | 8f70ad6e34d032db503765816010188c9316a466 /controller | |
parent | 686b2f7a74ef157c6fe707ac6ca102b1df566aa9 (diff) |
Make requests based on the file ID instead of using the path
1st implementation
(cherry picked from commit 844bd7b)
Diffstat (limited to 'controller')
-rw-r--r-- | controller/filescontroller.php | 27 | ||||
-rw-r--r-- | controller/previewcontroller.php | 114 | ||||
-rw-r--r-- | controller/publicpreviewcontroller.php | 32 |
3 files changed, 97 insertions, 76 deletions
diff --git a/controller/filescontroller.php b/controller/filescontroller.php index 01451e1e..49ba17a1 100644 --- a/controller/filescontroller.php +++ b/controller/filescontroller.php @@ -32,6 +32,7 @@ use OCA\GalleryPlus\Service\SearchMediaService; */ class FilesController extends Controller { + use PathManipulation; use JsonHttpError; /** @@ -105,7 +106,8 @@ class FilesController extends Controller { $this->configService->getAlbumInfo($folderNode, $folderPathFromRoot, $features); $files = $this->searchMediaService->getMediaFiles($folderNode, $mediaTypesArray, $features); - + $files = $this->fixPaths($files, $folderPathFromRoot); + return $this->formatResults($files, $albumInfo, $locationHasChanged); } catch (\Exception $exception) { return $this->error($exception); @@ -113,6 +115,29 @@ class FilesController extends Controller { } /** + * Generates shortened paths to the media files + * + * We only want to keep one folder between the current folder and the found media file + * /root/folder/sub1/sub2/file.ext + * becomes + * /root/folder/file.ext + * + * @param $files + * @param $folderPathFromRoot + * + * @return array + */ + private function fixPaths($files, $folderPathFromRoot) { + if (!empty($files)) { + foreach ($files as &$file) { + $file['path'] = $this->getReducedPath($file['path'], $folderPathFromRoot); + } + } + + return $files; + } + + /** * Simply builds and returns an array containing the list of files, the album information and * whether the location has changed or not * diff --git a/controller/previewcontroller.php b/controller/previewcontroller.php index f92fde01..22f290f2 100644 --- a/controller/previewcontroller.php +++ b/controller/previewcontroller.php @@ -18,12 +18,14 @@ use OCP\IRequest; use OCP\IURLGenerator; use OCP\IEventSource; use OCP\ILogger; +use OCP\Files\File; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCA\GalleryPlus\Http\ImageResponse; use OCA\GalleryPlus\Service\ServiceException; +use OCA\GalleryPlus\Service\NotFoundServiceException; use OCA\GalleryPlus\Service\ThumbnailService; use OCA\GalleryPlus\Service\PreviewService; use OCA\GalleryPlus\Service\DownloadService; @@ -61,6 +63,10 @@ class PreviewController extends Controller { * @var ILogger */ private $logger; + /** + * @type bool + */ + private $download = false; /** * Constructor @@ -122,17 +128,18 @@ class PreviewController extends Controller { * * WARNING: Returning a JSON response does not get rid of the problem * - * @param string $images + * @param string $ids the ID of the files of which we need thumbnail previews of * @param bool $square * @param bool $scale * * @return array<string,array|string> */ - public function getThumbnails($images, $square, $scale) { - $imagesArray = explode(';', $images); + public function getThumbnails($ids, $square, $scale) { + $idsArray = explode(';', $ids); - foreach ($imagesArray as $image) { - $thumbnail = $this->getThumbnail($image, $square, $scale); + foreach ($idsArray as $id) { + $thumbnail = $this->getThumbnail((int)$id, $square, $scale); + $thumbnail['fileid'] = $id; $this->eventSource->send('preview', $thumbnail); } $this->eventSource->close(); @@ -146,18 +153,19 @@ class PreviewController extends Controller { * Sends either a large preview of the requested file or the * original file itself * - * If the browser can use the file as-is then we simply let - * the browser download the file, straight from the filesystem - * - * @param string $file - * @param int $x - * @param int $y + * @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|Http\JSONResponse */ - public function showPreview($file, $x, $y) { + public function getPreview($fileId, $width, $height, $download) { + if (!is_null($download)) { + $this->download = true; + } try { - $preview = $this->getPreview($file, $x, $y); + $preview = $this->getPreviewData($fileId, $width, $height); return new ImageResponse($preview['data'], $preview['status']); } catch (ServiceException $exception) { @@ -165,24 +173,6 @@ class PreviewController extends Controller { } } - /** - * @NoAdminRequired - * - * Downloads the file - * - * @param string $file - * - * @return \OCA\GalleryPlus\Http\ImageResponse|Http\JSONResponse - */ - public function downloadPreview($file) { - try { - $download = $this->downloadService->downloadFile($file); - - return new ImageResponse($download); - } catch (ServiceException $exception) { - return $this->error($exception); - } - } /** * Retrieves the thumbnail to send back to the browser @@ -190,19 +180,20 @@ class PreviewController extends Controller { * The thumbnail is either a resized preview of the file or the original file * Thumbnails are base64encoded before getting sent back * - * @param string $image - * @param bool $square - * @param bool $scale + * + * @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 bool $scale whether we're allowed to scale the preview up * * @return array<string,array|string> */ - private function getThumbnail($image, $square, $scale) { + private function getThumbnail($fileId, $square, $scale) { list($width, $height, $aspect, $animatedPreview, $base64Encode) = $this->thumbnailService->getThumbnailSpecs($square, $scale); try { - $preview = $this->getPreview( - $image, $width, $height, $aspect, $animatedPreview, $base64Encode + $preview = $this->getPreviewData( + $fileId, $width, $height, $aspect, $animatedPreview, $base64Encode ); } catch (ServiceException $exception) { $preview = ['data' => null, 'status' => 500, 'type' => 'error']; @@ -231,7 +222,7 @@ class PreviewController extends Controller { * ] * ); * - * @param string $image + * @param int $fileId * @param int $width * @param int $height * @param bool $keepAspect @@ -239,24 +230,43 @@ class PreviewController extends Controller { * @param bool $base64Encode * * @return array<string,\OC_Image|string> + * + * @throws NotFoundServiceException */ - private function getPreview( - $image, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false + private function getPreviewData( + $fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false ) { $status = Http::STATUS_OK; - $previewRequired = $this->previewService->isPreviewRequired($image, $animatedPreview); - if ($previewRequired) { - $type = 'preview'; - $preview = $this->previewService->createPreview( - $image, $width, $height, $keepAspect, $base64Encode - ); - if (!$this->previewService->isPreviewValid()) { - $type = 'error'; - $status = Http::STATUS_NOT_FOUND; + try { + /** @type File $file */ + $file = $this->previewService->getResourceFromId($fileId); + if (!$this->download) { + $previewRequired = + $this->previewService->isPreviewRequired($file, $animatedPreview); + if ($previewRequired) { + $type = 'preview'; + $preview = $this->previewService->createPreview( + $file, $width, $height, $keepAspect, $base64Encode + ); + if (!$this->previewService->isPreviewValid()) { + $type = 'error'; + $status = Http::STATUS_NOT_FOUND; + } + } else { + $type = 'download'; + $preview = $this->downloadService->downloadFile($file, $base64Encode); + } + } else { + $type = 'download'; + $preview = $this->downloadService->downloadFile($file, $base64Encode); } - } else { - $type = 'download'; - $preview = $this->downloadService->downloadFile($image, $base64Encode); + + $preview['name'] = $file->getName(); + + } catch (\Exception $exception) { + $type = 'error'; + $status = Http::STATUS_INTERNAL_SERVER_ERROR; + $preview = null; } return ['data' => $preview, 'status' => $status, 'type' => $type]; diff --git a/controller/publicpreviewcontroller.php b/controller/publicpreviewcontroller.php index 2a6d57f3..6686e89e 100644 --- a/controller/publicpreviewcontroller.php +++ b/controller/publicpreviewcontroller.php @@ -44,12 +44,12 @@ class PublicPreviewController extends PreviewController { * * @inheritDoc * - * @param string $images + * @param string $ids the ID of the files of which we need thumbnail previews of * @param bool $square * @param bool $scale */ - public function getThumbnails($images, $square, $scale) { - return parent::getThumbnails($images, $square, $scale); + public function getThumbnails($ids, $square, $scale) { + return parent::getThumbnails($ids, $square, $scale); } /** @@ -63,27 +63,13 @@ class PublicPreviewController extends PreviewController { * * @inheritDoc * - * @param string $file - * @param int $x - * @param int $y + * @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 */ - public function showPreview($file, $x, $y) { - return parent::showPreview($file, $x, $y); - } - - /** - * @PublicPage - * @UseSession - * - * Downloads the file - * - * The session needs to be maintained open or previews can't be generated - * for files located on encrypted storage - * - * @inheritDoc - */ - public function downloadPreview($file) { - return parent::downloadPreview($file); + public function getPreview($fileId, $width, $height, $download) { + return parent::getPreview($fileId, $width, $height, $download); } } |