Skip to content

Commit 3ef2cd7

Browse files
authored
[9.x] Avoid throwing on invalid mime-type (#42761)
* Avoid throwing on invalid mime-type * update lowest league\flysystem version
1 parent dbfeda5 commit 3ef2cd7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"fruitcake/php-cors": "^1.2",
2525
"laravel/serializable-closure": "^1.0",
2626
"league/commonmark": "^2.2",
27-
"league/flysystem": "^3.0",
27+
"league/flysystem": "^3.0.16",
2828
"monolog/monolog": "^2.0",
2929
"nesbot/carbon": "^2.53.1",
3030
"psr/container": "^1.1.1|^2.0.1",

src/Illuminate/Filesystem/FilesystemAdapter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use League\Flysystem\UnableToDeleteFile;
2525
use League\Flysystem\UnableToMoveFile;
2626
use League\Flysystem\UnableToReadFile;
27+
use League\Flysystem\UnableToRetrieveMetadata;
2728
use League\Flysystem\UnableToSetVisibility;
2829
use League\Flysystem\UnableToWriteFile;
2930
use League\Flysystem\Visibility;
@@ -548,7 +549,13 @@ public function size($path)
548549
*/
549550
public function mimeType($path)
550551
{
551-
return $this->driver->mimeType($path);
552+
try {
553+
return $this->driver->mimeType($path);
554+
} catch (UnableToRetrieveMetadata $e) {
555+
throw_if($this->throwsExceptions(), $e);
556+
}
557+
558+
return false;
552559
}
553560

554561
/**

tests/Filesystem/FilesystemAdapterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use League\Flysystem\Ftp\FtpAdapter;
1515
use League\Flysystem\Local\LocalFilesystemAdapter;
1616
use League\Flysystem\UnableToReadFile;
17+
use League\Flysystem\UnableToRetrieveMetadata;
1718
use League\Flysystem\UnableToWriteFile;
1819
use Mockery as m;
1920
use PHPUnit\Framework\TestCase;
@@ -145,6 +146,13 @@ public function testGetFileNotFound()
145146
$this->assertNull($filesystemAdapter->get('file.txt'));
146147
}
147148

149+
public function testMimeTypeNotDetected()
150+
{
151+
$this->filesystem->write('unknown.mime-type', '');
152+
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);
153+
$this->assertFalse($filesystemAdapter->mimeType('unknown.mime-type'));
154+
}
155+
148156
public function testPut()
149157
{
150158
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);
@@ -447,6 +455,23 @@ public function testThrowExceptionsForPut()
447455
$this->fail('Exception was not thrown.');
448456
}
449457

458+
public function testThrowExceptionsForMimeType()
459+
{
460+
$this->filesystem->write('unknown.mime-type', '');
461+
462+
$adapter = new FilesystemAdapter($this->filesystem, $this->adapter, ['throw' => true]);
463+
464+
try {
465+
$adapter->mimeType('unknown.mime-type');
466+
} catch (UnableToRetrieveMetadata $e) {
467+
$this->assertTrue(true);
468+
469+
return;
470+
}
471+
472+
$this->fail('Exception was not thrown.');
473+
}
474+
450475
public function testGetAllFiles()
451476
{
452477
$this->filesystem->write('body.txt', 'Hello World');

0 commit comments

Comments
 (0)