Skip to content
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
18 changes: 13 additions & 5 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
[$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;
Expand All @@ -195,7 +197,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
// 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();
Expand All @@ -206,6 +208,8 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
}

$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);
Expand Down Expand Up @@ -545,7 +549,7 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
}

/**
* @param ISimpleFolder $previewFolder
* @param ISimpleFile[] $files Array of FileInfo, as the result of getDirectoryListing()
* @param int $width
* @param int $height
* @param bool $crop
Expand All @@ -555,10 +559,14 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
*
* @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();
}

/**
Expand Down
19 changes: 7 additions & 12 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,12 @@ public function testGetCachedPreview() {
$maxPreview->method('getMimeType')
->willReturn('image/png');

$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]);

$previewFile = $this->createMock(ISimpleFile::class);
$previewFile->method('getSize')->willReturn(1000);
$previewFile->method('getName')->willReturn('256-256.png');

$previewFolder->method('getFile')
->with($this->equalTo('256-256.png'))
->willReturn($previewFile);
$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview, $previewFile]);

$this->legacyEventDispatcher->expects($this->once())
->method('dispatch')
Expand Down Expand Up @@ -340,14 +337,12 @@ public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {
$maxPreview->method('getMimeType')
->willReturn('image/png');

$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]);

$preview = $this->createMock(ISimpleFile::class);
$preview->method('getSize')->willReturn(1000);
$previewFolder->method('getFile')
->with($this->equalTo('1024-512-crop.png'))
->willReturn($preview);
$preview->method('getName')->willReturn('1024-512-crop.png');

$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview, $preview]);

$this->previewManager->expects($this->never())
->method('isMimeSupported');
Expand Down