From dea729954c596bdb6cdaecba6f73df9f3e2c4255 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Mon, 11 Apr 2022 15:32:22 +0200 Subject: [PATCH] Ensure we throw an exception when we try to resolve a mime-type for a non-existing file. --- src/Local/LocalFilesystemAdapter.php | 5 +++++ src/Local/LocalFilesystemAdapterTest.php | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Local/LocalFilesystemAdapter.php b/src/Local/LocalFilesystemAdapter.php index ac0a693c9..24fe86967 100644 --- a/src/Local/LocalFilesystemAdapter.php +++ b/src/Local/LocalFilesystemAdapter.php @@ -367,6 +367,11 @@ public function mimeType(string $path): FileAttributes { $location = $this->prefixer->prefixPath($path); error_clear_last(); + + if ( ! is_file($location)) { + throw UnableToRetrieveMetadata::mimeType($location, 'No such file exists.'); + } + $mimeType = $this->mimeTypeDetector->detectMimeTypeFromFile($location); if ($mimeType === null) { diff --git a/src/Local/LocalFilesystemAdapterTest.php b/src/Local/LocalFilesystemAdapterTest.php index b6bc96107..f05f214fa 100644 --- a/src/Local/LocalFilesystemAdapterTest.php +++ b/src/Local/LocalFilesystemAdapterTest.php @@ -4,6 +4,7 @@ namespace League\Flysystem\Local; +use League\MimeTypeDetection\FinfoMimeTypeDetector; use const LOCK_EX; use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase; use League\Flysystem\Config; @@ -539,7 +540,10 @@ public function fetching_unknown_mime_type_of_a_file(): void public function not_being_able_to_get_mimetype(): void { $this->expectException(UnableToRetrieveMetadata::class); - $adapter = new LocalFilesystemAdapter(static::ROOT); + $adapter = new LocalFilesystemAdapter( + location: static::ROOT, + mimeTypeDetector: new FinfoMimeTypeDetector(), + ); $adapter->mimeType('flysystem.svg'); }