Skip to content

Commit

Permalink
[HttpFoundation] 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 11, 2019
1 parent a5d46a3 commit 9e4b3ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions File/MimeType/FileBinaryMimeTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
*
* @param string $cmd The command to run to get the mime type of a file
*/
public function __construct($cmd = 'file -b --mime %s 2>/dev/null')
public function __construct($cmd = 'file -b --mime -- %s 2>/dev/null')
{
$this->cmd = $cmd;
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public function guess($path)
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
Binary file added Tests/File/Fixtures/-test
Binary file not shown.
11 changes: 10 additions & 1 deletion Tests/File/MimeType/MimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
*/
class MimeTypeTest extends TestCase
{
protected $path;
public function testGuessWithLeadingDash()
{
$cwd = getcwd();
chdir(__DIR__.'/../Fixtures');
try {
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess('-test'));
} finally {
chdir($cwd);
}
}

public function testGuessImageWithoutExtension()
{
Expand Down

0 comments on commit 9e4b3ac

Please sign in to comment.