Skip to content

Commit

Permalink
fx thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
labudzinski committed Mar 12, 2024
1 parent f22fbcb commit a817378
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/Controller/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ enum: ['asset', 'object', 'version']
schema: new OA\Schema(
type: 'string'
),
examples: [
new OA\Examples('pimcore-system-treepreview', '', value: 'pimcore-system-treepreview'),
new OA\Examples('galleryThumbnail', '', value: 'galleryThumbnail')
]
examples: [new OA\Examples('pimcore-system-treepreview', '', value: 'pimcore-system-treepreview')]
),
],
responses: [
Expand Down Expand Up @@ -168,39 +165,19 @@ public function download(): Response
$elementFile = $element->getThumbnail($thumbnail);
}

$storagePath = $this->getStoragePath($elementFile,
$element->getId(),
$element->getFilename(),
$element->getRealPath(),
$element->getChecksum()
);

$storage = Storage::get('thumbnail');
if (!$storage->fileExists($storagePath)) {
$response = new StreamedResponse(function () use ($elementFile) {
fpassthru($elementFile->getStream());
}, 200, [
'Content-Type' => $elementFile->getMimetype(),
]);
} else {
$response = new StreamedResponse(function () use ($storagePath) {
$storage = Storage::get('thumbnail');
fpassthru($storage->readStream($storagePath));
}, 200, [
'Content-Type' => $storage->mimeType($storagePath),
]);
$response = $this->getStreamedResponse($elementFile, $element);
} else {
$response = $this->getStreamedResponse($elementFile, $element);
// If it is not a thumbnail then send DISPOSITION_ATTACHMENT of the download.
if (!$this->request->request->has('thumbnail')) {
$filename = basename(rawurldecode($elementFile->getPath()));
$filenameFallback = preg_replace("/[^\w\-\.]/", '', $filename);
$response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, $filenameFallback);
$response->headers->set('Content-Length', $elementFile->getFileSize());
}
}

$response->headers->add($crossOriginHeaders);

// If it is not a thumbnail then send DISPOSITION_ATTACHMENT of the download.
if (!$this->request->request->has('thumbnail')) {
$filename = basename(rawurldecode($elementFile->getPath()));
$filenameFallback = preg_replace("/[^\w\-\.]/", '', $filename);
$response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, $filenameFallback);
$response->headers->set('Content-Length', $elementFile->getFileSize());
}
try {
// Add cache to headers
$this->addThumbnailCacheHeaders($response);
Expand Down Expand Up @@ -373,4 +350,33 @@ public function getStoragePath(ThumbnailInterface $thumb, int $id, string $filen

return $thumbDir.'/'.$filename;
}

/**
* @throws FilesystemException
*/
private function getStreamedResponse(mixed $elementFile, mixed $element): StreamedResponse
{
$storagePath = $this->getStoragePath($elementFile,
$element->getId(),
$element->getFilename(),
$element->getRealPath(),
$element->getChecksum()
);
$storage = Storage::get('thumbnail');
if (!$storage->fileExists($storagePath)) {
$response = new StreamedResponse(function () use ($elementFile) {
fpassthru($elementFile->getStream());
}, 200, [
'Content-Type' => $elementFile->getMimetype(),
]);
} else {
$response = new StreamedResponse(function () use ($storagePath, $storage) {
fpassthru($storage->readStream($storagePath));
}, 200, [
'Content-Type' => $storage->mimeType($storagePath),
]);
}

return $response;
}
}

0 comments on commit a817378

Please sign in to comment.