Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

No need to construct the image in memory #265

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/Controller/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand Down Expand Up @@ -156,7 +162,7 @@ private function getFile($fileId) {
* @param bool $keepAspect
* @param bool $base64Encode
*
* @return array<\OC_Image|string, int>
* @return array<File|ISimpleFile, int>
*/
private function getPreviewData(
$file, $animatedPreview, $width, $height, $keepAspect, $base64Encode
Expand Down
18 changes: 15 additions & 3 deletions lib/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;

}

}
16 changes: 3 additions & 13 deletions lib/Service/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
19 changes: 4 additions & 15 deletions lib/Service/PreviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down