[ticket/11912] Supply filename to content_guesser for guessing on windows

The filename of the files sent to the guesser by plupload do not contain
the file extension. Therefore, it's impossible to guess the mimetype if
only the content_guesser is available and the function mime_content_type()
doesn't exist. By supplying the filename we can circumvent this issue.

PHPBB3-11912
This commit is contained in:
Marc Alexander 2013-10-24 12:05:00 +02:00
parent 24099583a3
commit bc7ff47537
4 changed files with 17 additions and 8 deletions

View file

@ -492,7 +492,7 @@ class content_guesser
*
* @return string Guess for mimetype of file
*/
public function guess($file)
public function guess($file, $file_name = '')
{
$mimetype = null;
if (function_exists('mime_content_type'))
@ -501,14 +501,22 @@ class content_guesser
}
else
{
$mimetype = $this->map_extension_to_type($file);
$file_name = (empty($file_name)) ? $file : $file_name;
$mimetype = $this->map_extension_to_type($file_name);
}
return $mimetype;
}
protected function map_extension_to_type($file)
/**
* Map extension of supplied file_name to mime type
*
* @param string $file_name Path to file or filename
*
* @return string|null Mimetype if known or null if not
*/
protected function map_extension_to_type($file_name)
{
$extension = pathinfo($file, PATHINFO_EXTENSION);
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
if (isset($this->extension_map[$extension]))
{

View file

@ -77,7 +77,7 @@ class guesser
*
* @return string Guess for mimetype of file
*/
public function guess($file)
public function guess($file, $file_name = '')
{
if (!is_file($file))
{
@ -91,7 +91,7 @@ class guesser
foreach ($this->guessers as $guesser)
{
$mimetype = $guesser->guess($file);
$mimetype = $guesser->guess($file, $file_name);
// Try to guess something that is not the fallback application/octet-stream
if ($mimetype !== null && $mimetype !== 'application/octet-stream')

View file

@ -135,7 +135,7 @@ class plupload
'tmp_name' => $file_path,
'name' => $this->request->variable('real_filename', ''),
'size' => filesize($file_path),
'type' => $this->mimetype_guesser->guess($file_path),
'type' => $this->mimetype_guesser->guess($file_path, $file_name),
);
}
else

View file

@ -119,8 +119,9 @@ class guesser_test extends \phpbb_test_case
public function test_content_guesser($expected, $overload = false)
{
self::$function_exists = ($overload) ? false : true;
$guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\content_guesser($this->phpbb_root_path, new \phpbb\php\ini)));
$guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\content_guesser));
$this->assertEquals($expected[0], $guesser->guess($this->jpg_file));
$this->assertEquals($expected[1], $guesser->guess($this->jpg_file, $this->jpg_file . '.jpg'));
@copy($this->jpg_file, $this->jpg_file . '.jpg');
$this->assertEquals($expected[1], $guesser->guess($this->jpg_file . '.jpg'));
@unlink($this->jpg_file . '.jpg');