diff options
author | Tristan Darricau <github@nicofuma.fr> | 2014-09-06 13:54:16 +0200 |
---|---|---|
committer | Tristan Darricau <github@nicofuma.fr> | 2014-09-06 13:54:16 +0200 |
commit | e0711b417fb9e9106af52dcde51ca20dd7b056f2 (patch) | |
tree | 4a866d9599bc802800fdb17420efd23bfc640f53 /phpBB/phpbb | |
parent | bc38730ab5e7d59e8e0b1b13c0cd8f3fb32f271a (diff) | |
parent | 6387bf8d1399b9ccf995b8eb5a4b93425d27e873 (diff) | |
download | forums-e0711b417fb9e9106af52dcde51ca20dd7b056f2.tar forums-e0711b417fb9e9106af52dcde51ca20dd7b056f2.tar.gz forums-e0711b417fb9e9106af52dcde51ca20dd7b056f2.tar.bz2 forums-e0711b417fb9e9106af52dcde51ca20dd7b056f2.tar.xz forums-e0711b417fb9e9106af52dcde51ca20dd7b056f2.zip |
Merge branch 'develop-ascraeus' into develop
* develop-ascraeus:
[ticket/13031] Slightly change behavior of choose_mime_type and add unit tests
[ticket/13031] Guess with all mimetype guessers and pick best guess
[ticket/13031] Only use mimetype guesser guess if it helps us
Diffstat (limited to 'phpBB/phpbb')
-rw-r--r-- | phpBB/phpbb/mimetype/guesser.php | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/phpBB/phpbb/mimetype/guesser.php b/phpBB/phpbb/mimetype/guesser.php index 773a1f822a..8baa77089b 100644 --- a/phpBB/phpbb/mimetype/guesser.php +++ b/phpBB/phpbb/mimetype/guesser.php @@ -115,17 +115,42 @@ class guesser return false; } + $mimetype = 'application/octet-stream'; + foreach ($this->guessers as $guesser) { - $mimetype = $guesser->guess($file, $file_name); + $mimetype_guess = $guesser->guess($file, $file_name); - // Try to guess something that is not the fallback application/octet-stream - if ($mimetype !== null && $mimetype !== 'application/octet-stream') - { - return $mimetype; - } + $mimetype = $this->choose_mime_type($mimetype, $mimetype_guess); } // Return any mimetype if we got a result or the fallback value - return (!empty($mimetype)) ? $mimetype : 'application/octet-stream'; + return $mimetype; + } + + /** + * Choose the best mime type based on the current mime type and the guess + * If a guesser returns nulls or application/octet-stream, we will keep + * the current guess. Guesses with a slash inside them will be favored over + * already existing ones. However, any guess that will pass the first check + * will always overwrite the default application/octet-stream. + * + * @param string $mime_type The current mime type + * @param string $guess The current mime type guess + * + * @return string The best mime type based on current mime type and guess + */ + public function choose_mime_type($mime_type, $guess) + { + if ($guess === null || $guess == 'application/octet-stream') + { + return $mime_type; + } + + if ($mime_type == 'application/octet-stream' || strpos($guess, '/') !== false) + { + $mime_type = $guess; + } + + return $mime_type; } } |