Skip to content
Open
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
7 changes: 7 additions & 0 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Generator {
public const SEMAPHORE_ID_ALL = 0x0a11;
public const SEMAPHORE_ID_NEW = 0x07ea;

/** The viewer only shows the max preview of images */
private const MAX_NON_IMAGE_PREVIEW_SIZE = 1024;

private array $cachedNumConcurrentPreviews = [];

public function __construct(
Expand Down Expand Up @@ -327,6 +330,10 @@ private function getMaxPreview(array $previews, File $file, string $mimeType, ?s

$maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096);
$maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096);
if (!str_starts_with($mimeType, 'image/')) {
$maxWidth = min($maxWidth, self::MAX_NON_IMAGE_PREVIEW_SIZE);
$maxHeight = min($maxHeight, self::MAX_NON_IMAGE_PREVIEW_SIZE);
}

try {
return $this->generateProviderPreview($file, $maxWidth, $maxHeight, false, true, $mimeType, $version);
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,38 @@ public function testCorrectSize(int $maxX, int $maxY, int $reqX, int $reqY, bool
}
}

#[TestWith(['image/jpeg', 4096])]
#[TestWith(['text/plain', 1024])]
#[TestWith(['application/pdf', 1024])]
public function testMaxPreviewSize(string $mimeType, int $expectedSize): void {
$file = $this->getFile(42, $mimeType);

$this->getAutoMock(PreviewMapper::class)->method('getAvailablePreviews')
->willReturn([42 => []]);
$this->getAutoMock(IConfig::class)->method('getSystemValueInt')
->willReturnCallback(fn ($key, $default) => $default);

$provider = $this->createMock(IProviderV2::class);
$provider->method('isAvailable')->willReturn(true);
$this->getAutoMock(IPreview::class)->method('getProviders')
->willReturn(['/.*/' => ['provider']]);
$this->getAutoMock(GeneratorHelper::class)->method('getProvider')
->willReturn($provider);

$this->getAutoMock(GeneratorHelper::class)->expects($this->once())
->method('getThumbnail')
->with($provider, $file, $expectedSize, $expectedSize)
->willReturn($this->getMockImage($expectedSize, $expectedSize, 'my data'));

$this->getAutoMock(PreviewMapper::class)->method('insert')
->willReturnCallback(fn (Preview $preview): Preview => $preview);
$this->getAutoMock(StorageFactory::class)->method('writePreview')
->willReturn(1000);

$result = $this->generator->getPreview($file);
$this->assertSame("$expectedSize-$expectedSize-max.png", $result->getName());
}

public function testUnreadbleFile(): void {
$file = $this->createMock(File::class);
$file->method('isReadable')
Expand Down
Loading