Skip to content

Commit

Permalink
Make falling back to inconclusive mime-type configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Dec 4, 2023
1 parent d18526e commit 03fcf05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Local/FallbackMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace League\Flysystem\Local;

use League\MimeTypeDetection\MimeTypeDetector;

use function in_array;

class FallbackMimeTypeDetector implements MimeTypeDetector
Expand All @@ -20,7 +19,8 @@ class FallbackMimeTypeDetector implements MimeTypeDetector

public function __construct(
private MimeTypeDetector $detector,
private array $inconclusiveMimetypes = self::INCONCLUSIVE_MIME_TYPES
private array $inconclusiveMimetypes = self::INCONCLUSIVE_MIME_TYPES,
private bool $useInconclusiveMimeTypeFallback = false,
) {
}

Expand All @@ -47,6 +47,6 @@ public function detectMimeTypeFromFile(string $path): ?string
return $mimeType;
}

return $this->detector->detectMimeTypeFromPath($path) ?? $mimeType;
return $this->detector->detectMimeTypeFromPath($path) ?? ($this->useInconclusiveMimeTypeFallback ? $mimeType : null);
}
}
6 changes: 5 additions & 1 deletion src/Local/LocalFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ public function __construct(
private int $linkHandling = self::DISALLOW_LINKS,
MimeTypeDetector $mimeTypeDetector = null,
bool $lazyRootCreation = false,
bool $useInconclusiveMimeTypeFallback = false,
) {
$this->prefixer = new PathPrefixer($location, DIRECTORY_SEPARATOR);
$visibility ??= new PortableVisibilityConverter();
$this->visibility = $visibility;
$this->rootLocation = $location;
$this->mimeTypeDetector = $mimeTypeDetector ?? new FallbackMimeTypeDetector(new FinfoMimeTypeDetector());
$this->mimeTypeDetector = $mimeTypeDetector ?? new FallbackMimeTypeDetector(
detector: new FinfoMimeTypeDetector(),
useInconclusiveMimeTypeFallback: $useInconclusiveMimeTypeFallback,
);

if ( ! $lazyRootCreation) {
$this->ensureRootDirectoryExists();
Expand Down
35 changes: 35 additions & 0 deletions src/Local/LocalFilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,41 @@ public function getting_mimetype(): void
$this->assertStringStartsWith('image/svg+xml', $adapter->mimeType('flysystem.svg')->mimeType());
}

/**
* @test
*/
public function failing_to_get_the_mimetype(): void
{
$adapter = new LocalFilesystemAdapter(static::ROOT);
$adapter->write(
'file.unknown',
'',
new Config()
);

$this->expectException(UnableToRetrieveMetadata::class);

$adapter->mimeType('file.unknown');
}

/**
* @test
*/
public function allowing_inconclusive_mime_type(): void
{
$adapter = new LocalFilesystemAdapter(
location: static::ROOT,
useInconclusiveMimeTypeFallback: true,
);
$adapter->write(
'file.unknown',
'',
new Config()
);

$this->assertEquals('application/x-empty', $adapter->mimeType('file.unknown')->mimeType());
}

/**
* @test
*/
Expand Down

0 comments on commit 03fcf05

Please sign in to comment.