From 46dbc7cc6107d8ee1556acf63cc12033b1e88982 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 5 May 2017 20:25:43 +0200 Subject: [PATCH] No need to construct the image in memory Not constructing the image in memory saves valuable resources * Pass around File or ISimpleFile objects * Use the FileDisplayResponse so we have all the fancy caching Signed-off-by: Roeland Jago Douma --- lib/Controller/Preview.php | 10 ++++++++-- lib/Controller/PreviewController.php | 18 +++++++++++++++--- lib/Service/DownloadService.php | 16 +++------------- lib/Service/PreviewService.php | 19 ++++--------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/Controller/Preview.php b/lib/Controller/Preview.php index 06f4cbcd4..a1d2da3df 100644 --- a/lib/Controller/Preview.php +++ b/lib/Controller/Preview.php @@ -87,9 +87,15 @@ private function getThumbnail($fileId, $square, $scale) { ); if ($preview === null) { $preview = $this->prepareEmptyThumbnail($file, $status); + return [$preview, $status]; } - return [$preview, $status]; + $result = [ + 'preview' => $base64Encode ? base64_encode($preview->getContent()) : $preview->getContent(), + 'mimetype' => $file->getMimeType() + ]; + + return [$result, $status]; } /** @@ -156,7 +162,7 @@ private function getFile($fileId) { * @param bool $keepAspect * @param bool $base64Encode * - * @return array<\OC_Image|string, int> + * @return array */ private function getPreviewData( $file, $animatedPreview, $width, $height, $keepAspect, $base64Encode diff --git a/lib/Controller/PreviewController.php b/lib/Controller/PreviewController.php index 83f319127..522cbf273 100644 --- a/lib/Controller/PreviewController.php +++ b/lib/Controller/PreviewController.php @@ -121,7 +121,7 @@ public function getThumbnails($ids, $square, $scale) { * @param int $width * @param int $height * - * @return ImageResponse|Http\JSONResponse + * @return Http\FileDisplayResponse|Http\JSONResponse */ public function getPreview($fileId, $width, $height) { /** @type File $file */ @@ -135,9 +135,21 @@ public function getPreview($fileId, $width, $height) { ], $status ); } - $preview['name'] = $file->getName(); - return new ImageResponse($preview, $status); + $response = new Http\FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]); + + // Let cache this! + $response->addHeader('Pragma', 'public'); + + // Cache previews for 24H + $response->cacheFor(3600 * 24); + $expires = new \DateTime(); + $expires->add(new \DateInterval('P1D')); + $response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); + $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($file->getName()) . '; filename="' + . rawurlencode($file->getName()) . '"'); + return $response; + } } diff --git a/lib/Service/DownloadService.php b/lib/Service/DownloadService.php index 0d6595606..f41e5cd48 100644 --- a/lib/Service/DownloadService.php +++ b/lib/Service/DownloadService.php @@ -28,26 +28,16 @@ class DownloadService extends Service { * Downloads the requested file * * @param File $file - * @param bool $base64Encode * - * @return array|false + * @return File * @throws NotFoundServiceException */ - public function downloadFile($file, $base64Encode = false) { + public function downloadFile($file) { try { $this->logger->debug( "[DownloadService] File to Download: {name}", ['name' => $file->getName()] ); - $download = [ - 'preview' => $file->getContent(), - 'mimetype' => $file->getMimeType() - ]; - - if ($base64Encode) { - $download['preview'] = $this->encode($download['preview']); - } - - return $download; + return $file; } catch (\Exception $exception) { throw new NotFoundServiceException('There was a problem accessing the file'); } diff --git a/lib/Service/PreviewService.php b/lib/Service/PreviewService.php index 18b585359..a818e5534 100644 --- a/lib/Service/PreviewService.php +++ b/lib/Service/PreviewService.php @@ -13,6 +13,7 @@ namespace OCA\Gallery\Service; use OCP\Files\File; +use OCP\Files\SimpleFS\ISimpleFile; use OCP\Image; use OCP\IPreview; use OCP\ILogger; @@ -94,26 +95,14 @@ public function isPreviewRequired($file, $animatedPreview) { * @param int $maxX asked width for the preview * @param int $maxY asked height for the preview * @param bool $keepAspect - * @param bool $base64Encode * - * @return string|\OC_Image|string|false preview data + * @return ISimpleFile preview * @throws InternalServerErrorServiceException */ - public function createPreview( - $file, $maxX = 0, $maxY = 0, $keepAspect = true, $base64Encode = false + public function createPreview(File $file, $maxX = 0, $maxY = 0, $keepAspect = true ) { try { - $preview = $this->previewManager->getPreview($file, $maxX, $maxY, !$keepAspect); - $img = new Image($preview->getContent()); - $mimeType = $img->mimeType(); - if ($img && $base64Encode) { - $img = $this->encode($img); - } - - return [ - 'preview' => $img, - 'mimetype' => $mimeType - ]; + return $this->previewManager->getPreview($file, $maxX, $maxY, !$keepAspect); } catch (\Exception $exception) { throw new InternalServerErrorServiceException('Preview generation has failed'); }