summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlandos <bugs-github@antipoul.fr>2022-11-01 16:15:30 +0100
committerSimon L <szaimen@e.mail.de>2023-04-17 16:29:13 +0200
commit9b4d5146d5e0fc8b2c25d2e0e0976c2489cb9659 (patch)
treea4eb2b7558536414cfbcb60ea27e3e485652b6a1
parente63720b7140d15cd4c0b080c57c226d9a2dda8a6 (diff)
downloadnextcloud-server-9b4d5146d5e0fc8b2c25d2e0e0976c2489cb9659.tar.gz
nextcloud-server-9b4d5146d5e0fc8b2c25d2e0e0976c2489cb9659.zip
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>
-rw-r--r--lib/private/Preview/Generator.php18
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php
index 47f2952c6e6..a005d46adb4 100644
--- a/lib/private/Preview/Generator.php
+++ b/lib/private/Preview/Generator.php
@@ -171,6 +171,8 @@ class Generator {
[$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion);
$preview = null;
+ // List every existing preview first instead of trying to find them one by one
+ $previewFiles = $previewFolder->getDirectoryListing();
foreach ($specifications as $specification) {
$width = $specification['width'] ?? -1;
@@ -197,7 +199,7 @@ class Generator {
// Try to get a cached preview. Else generate (and store) one
try {
try {
- $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
+ $preview = $this->getCachedPreview($previewFiles, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
} catch (NotFoundException $e) {
if (!$this->previewManager->isMimeSupported($mimeType)) {
throw new NotFoundException();
@@ -208,6 +210,8 @@ class Generator {
}
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
+ // New file, augment our array
+ $previewFiles[] = $preview;
}
} catch (\InvalidArgumentException $e) {
throw new NotFoundException("", 0, $e);
@@ -649,7 +653,7 @@ class Generator {
}
/**
- * @param ISimpleFolder $previewFolder
+ * @param array $files Array of FileInfo, as the result of getDirectoryListing()
* @param int $width
* @param int $height
* @param bool $crop
@@ -659,10 +663,14 @@ class Generator {
*
* @throws NotFoundException
*/
- private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) {
+ private function getCachedPreview($files, $width, $height, $crop, $mimeType, $prefix) {
$path = $this->generatePath($width, $height, $crop, $mimeType, $prefix);
-
- return $previewFolder->getFile($path);
+ foreach($files as $file) {
+ if ($file->getName() === $path) {
+ return $file;
+ }
+ }
+ throw new NotFoundException();
}
/**