Skip to content

Commit

Permalink
Merge pull request #38844 from nextcloud/enh/add-detected-mime-type-t…
Browse files Browse the repository at this point in the history
…o-exception

feat: add detected mime type to exception
  • Loading branch information
szaimen committed Jun 24, 2023
2 parents 10d563a + f21cbff commit 3f4b764
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apps/theming/lib/ImageManager.php
Expand Up @@ -240,7 +240,7 @@ public function updateImage(string $key, string $tmpFile): string {
$supportedFormats = $this->getSupportedUploadImageFormats($key);
$detectedMimeType = mime_content_type($tmpFile);
if (!in_array($detectedMimeType, $supportedFormats, true)) {
throw new \Exception('Unsupported image type');
throw new \Exception('Unsupported image type: ' . $detectedMimeType);
}

if ($key === 'background' && $this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) {
Expand Down
26 changes: 26 additions & 0 deletions apps/theming/tests/ImageManagerTest.php
Expand Up @@ -390,4 +390,30 @@ public function testUpdateImage($key, $tmpFile, $folderExists, $shouldConvert) {

$this->imageManager->updateImage($key, $tmpFile);
}

public function testUnsupportedImageType(): void {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Unsupported image type: text/plain');

$file = $this->createMock(ISimpleFile::class);
$folder = $this->createMock(ISimpleFolder::class);
$oldFile = $this->createMock(ISimpleFile::class);

$folder->expects($this->any())
->method('getFile')
->willReturn($oldFile);

$this->rootFolder
->expects($this->any())
->method('getFolder')
->with('images')
->willReturn($folder);

$folder->expects($this->once())
->method('newFile')
->with('favicon')
->willReturn($file);

$this->imageManager->updateImage('favicon', __DIR__ . '/../../../tests/data/lorem.txt');
}
}

0 comments on commit 3f4b764

Please sign in to comment.