Skip to content

Commit 9b4d514

Browse files
Glandosszaimen
authored andcommitted
List preview directory only once
getCachedPreview used to call `getFile`, and this calls `getDirectoryListing` (or underlying function that list directory) to find the file. This was done for every preview spec. Now, this is done only once at the beginning of the loop, and the array is just iterated when needed to find the correct entry. Signed-off-by: Glandos <bugs-github@antipoul.fr>
1 parent e63720b commit 9b4d514

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/private/Preview/Generator.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
171171
[$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion);
172172

173173
$preview = null;
174+
// List every existing preview first instead of trying to find them one by one
175+
$previewFiles = $previewFolder->getDirectoryListing();
174176

175177
foreach ($specifications as $specification) {
176178
$width = $specification['width'] ?? -1;
@@ -197,7 +199,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
197199
// Try to get a cached preview. Else generate (and store) one
198200
try {
199201
try {
200-
$preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
202+
$preview = $this->getCachedPreview($previewFiles, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
201203
} catch (NotFoundException $e) {
202204
if (!$this->previewManager->isMimeSupported($mimeType)) {
203205
throw new NotFoundException();
@@ -208,6 +210,8 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
208210
}
209211

210212
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
213+
// New file, augment our array
214+
$previewFiles[] = $preview;
211215
}
212216
} catch (\InvalidArgumentException $e) {
213217
throw new NotFoundException("", 0, $e);
@@ -649,7 +653,7 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
649653
}
650654

651655
/**
652-
* @param ISimpleFolder $previewFolder
656+
* @param array $files Array of FileInfo, as the result of getDirectoryListing()
653657
* @param int $width
654658
* @param int $height
655659
* @param bool $crop
@@ -659,10 +663,14 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
659663
*
660664
* @throws NotFoundException
661665
*/
662-
private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) {
666+
private function getCachedPreview($files, $width, $height, $crop, $mimeType, $prefix) {
663667
$path = $this->generatePath($width, $height, $crop, $mimeType, $prefix);
664-
665-
return $previewFolder->getFile($path);
668+
foreach($files as $file) {
669+
if ($file->getName() === $path) {
670+
return $file;
671+
}
672+
}
673+
throw new NotFoundException();
666674
}
667675

668676
/**

0 commit comments

Comments
 (0)