Skip to content
Permalink
Browse files

[Mime] fix guessing mime-types of files with leading dash

  • Loading branch information
nicolas-grekas committed Apr 19, 2019
1 parent 227e73d commit 77ddabf2e785ea85860d2720cc86f7c5d8967ed5
@@ -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();

@@ -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()) {
Binary file not shown.

0 comments on commit 77ddabf

Please sign in to comment.
You can’t perform that action at this time.