]> source.dussan.org Git - nextcloud-server.git/commitdiff
feat: add detected mime type to exception 38844/head
authorDaniel Kesselberg <mail@danielkesselberg.de>
Thu, 15 Jun 2023 18:14:17 +0000 (20:14 +0200)
committerDaniel <mail@danielkesselberg.de>
Sat, 24 Jun 2023 20:38:57 +0000 (22:38 +0200)
The mimetype may not match the file extension.
Helps the user to find out why a provided image is not accepted.

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
apps/theming/lib/ImageManager.php
apps/theming/tests/ImageManagerTest.php

index 87123a6f7c9c08db47e10e8fa44626881efa3c3f..f609e5ef9d4f22837fd891bde3b8ed48dacd15fc 100644 (file)
@@ -240,7 +240,7 @@ class ImageManager {
                $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))) {
index 22432a00103f3bb28ad7a09d8252a3fb755962dc..e0e00615edbe4adb34cb9f207af6ef1f5c2a4a70 100644 (file)
@@ -390,4 +390,30 @@ class ImageManagerTest extends TestCase {
 
                $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');
+       }
 }