Skip to content

Commit

Permalink
[Mime] fix guessing mime-types of files with leading dash
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 12, 2019
1 parent 3c0e197 commit 22aecf6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions FileBinaryMimeTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();

Expand Down
15 changes: 15 additions & 0 deletions Tests/AbstractMimeTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Binary file added Tests/Fixtures/mimetypes/-test
Binary file not shown.

0 comments on commit 22aecf6

Please sign in to comment.