Skip to content

Commit 22aecf6

Browse files
[Mime] fix guessing mime-types of files with leading dash
1 parent 3c0e197 commit 22aecf6

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

FileBinaryMimeTypeGuesser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
3333
*
3434
* @param string $cmd The command to run to get the MIME type of a file
3535
*/
36-
public function __construct(string $cmd = 'file -b --mime %s 2>/dev/null')
36+
public function __construct(string $cmd = 'file -b --mime -- %s 2>/dev/null')
3737
{
3838
$this->cmd = $cmd;
3939
}
@@ -76,7 +76,7 @@ public function guessMimeType(string $path): ?string
7676
ob_start();
7777

7878
// need to use --mime instead of -i. see #6641
79-
passthru(sprintf($this->cmd, escapeshellarg($path)), $return);
79+
passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return);
8080
if ($return > 0) {
8181
ob_end_clean();
8282

Tests/AbstractMimeTypeGuesserTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ public static function tearDownAfterClass(): void
2727

2828
abstract protected function getGuesser(): MimeTypeGuesserInterface;
2929

30+
public function testGuessWithLeadingDash()
31+
{
32+
if (!$this->getGuesser()->isGuesserSupported()) {
33+
$this->markTestSkipped('Guesser is not supported');
34+
}
35+
36+
$cwd = getcwd();
37+
chdir(__DIR__.'/Fixtures/mimetypes');
38+
try {
39+
$this->assertEquals('image/gif', $this->getGuesser()->guessMimeType('-test'));
40+
} finally {
41+
chdir($cwd);
42+
}
43+
}
44+
3045
public function testGuessImageWithoutExtension()
3146
{
3247
if (!$this->getGuesser()->isGuesserSupported()) {

Tests/Fixtures/mimetypes/-test

35 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)