From 22aecf6b11638ef378fab25d6c5a2da8a31a1448 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 19 Apr 2019 14:48:43 +0200 Subject: [PATCH] [Mime] fix guessing mime-types of files with leading dash --- FileBinaryMimeTypeGuesser.php | 4 ++-- Tests/AbstractMimeTypeGuesserTest.php | 15 +++++++++++++++ Tests/Fixtures/mimetypes/-test | Bin 0 -> 35 bytes 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Tests/Fixtures/mimetypes/-test diff --git a/FileBinaryMimeTypeGuesser.php b/FileBinaryMimeTypeGuesser.php index e00ce65..59e55f7 100644 --- a/FileBinaryMimeTypeGuesser.php +++ b/FileBinaryMimeTypeGuesser.php @@ -33,7 +33,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface * * @param string $cmd The command to run to get the MIME type of a file */ - public function __construct(string $cmd = 'file -b --mime %s 2>/dev/null') + public function __construct(string $cmd = 'file -b --mime -- %s 2>/dev/null') { $this->cmd = $cmd; } @@ -76,7 +76,7 @@ public function guessMimeType(string $path): ?string ob_start(); // need to use --mime instead of -i. see #6641 - passthru(sprintf($this->cmd, escapeshellarg($path)), $return); + passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return); if ($return > 0) { ob_end_clean(); diff --git a/Tests/AbstractMimeTypeGuesserTest.php b/Tests/AbstractMimeTypeGuesserTest.php index 3ac9382..70e419c 100644 --- a/Tests/AbstractMimeTypeGuesserTest.php +++ b/Tests/AbstractMimeTypeGuesserTest.php @@ -27,6 +27,21 @@ public static function tearDownAfterClass(): void abstract protected function getGuesser(): MimeTypeGuesserInterface; + public function testGuessWithLeadingDash() + { + if (!$this->getGuesser()->isGuesserSupported()) { + $this->markTestSkipped('Guesser is not supported'); + } + + $cwd = getcwd(); + chdir(__DIR__.'/Fixtures/mimetypes'); + try { + $this->assertEquals('image/gif', $this->getGuesser()->guessMimeType('-test')); + } finally { + chdir($cwd); + } + } + public function testGuessImageWithoutExtension() { if (!$this->getGuesser()->isGuesserSupported()) { diff --git a/Tests/Fixtures/mimetypes/-test b/Tests/Fixtures/mimetypes/-test new file mode 100644 index 0000000000000000000000000000000000000000..b636f4b8df536b0a85e7cea1a6cf3f0bd3179b96 GIT binary patch literal 35 jcmZ?wbh9u|WMp7uXkcLY4+c66KmZb9U}AD%WUvMRyAlZ1 literal 0 HcmV?d00001