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 <roeland@famdouma.nl>
This commit is contained in:
parent
41400d1358
commit
46dbc7cc61
|
@ -87,9 +87,15 @@ trait Preview {
|
|||
);
|
||||
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 @@ trait Preview {
|
|||
* @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
|
||||
|
|
|
@ -121,7 +121,7 @@ class PreviewController extends Controller {
|
|||
* @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 @@ class PreviewController extends Controller {
|
|||
], $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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
|
|
@ -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 @@ class PreviewService extends Service {
|
|||
* @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');
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue