diff options
author | Aaron Schulz <aschulz@wikimedia.org> | 2014-11-13 22:47:06 -0800 |
---|---|---|
committer | Aaron Schulz <aschulz@wikimedia.org> | 2015-11-02 00:36:54 +0000 |
commit | de290cd02db7150549da5cc66c9af3de6933a68b (patch) | |
tree | 1495248fb0de678116ccaca5f4dd6f95daf7d4ee /includes/filebackend/FileBackendStore.php | |
parent | 8f15bc8ec9e15bca8c19490ccaa45f805d54a035 (diff) | |
download | mediawikicore-de290cd02db7150549da5cc66c9af3de6933a68b.tar.gz mediawikicore-de290cd02db7150549da5cc66c9af3de6933a68b.zip |
Improve MIME detection in FileBackend
The content type detector will now inspect the file contents
to better handle extensionless files.
Also dependency inject the callback and make the default one
use FileInfo.
Change-Id: Iad59bf6c6a416b706f976a4c425763fd30e2debb
Diffstat (limited to 'includes/filebackend/FileBackendStore.php')
-rw-r--r-- | includes/filebackend/FileBackendStore.php | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/includes/filebackend/FileBackendStore.php b/includes/filebackend/FileBackendStore.php index e5ce968a9722..8113ec213baf 100644 --- a/includes/filebackend/FileBackendStore.php +++ b/includes/filebackend/FileBackendStore.php @@ -58,7 +58,7 @@ abstract class FileBackendStore extends FileBackend { /** * @see FileBackend::__construct() * Additional $config params include: - * - wanCache : WANOBjectCache object to use for persistent caching. + * - wanCache : WANObjectCache object to use for persistent caching. * - mimeCallback : Callback that takes (storage path, content, file system path) and * returns the MIME type of the file or 'unknown/unknown'. The file * system path parameter should be used if the content one is null. @@ -69,10 +69,7 @@ abstract class FileBackendStore extends FileBackend { parent::__construct( $config ); $this->mimeCallback = isset( $config['mimeCallback'] ) ? $config['mimeCallback'] - : function ( $storagePath, $content, $fsPath ) { - // @todo handle the case of extension-less files using the contents - return StreamFile::contentTypeFromPath( $storagePath ) ?: 'unknown/unknown'; - }; + : null; $this->memCache = WANObjectCache::newEmpty(); // disabled by default $this->cheapCache = new ProcessCacheLRU( self::CACHE_CHEAP_SIZE ); $this->expensiveCache = new ProcessCacheLRU( self::CACHE_EXPENSIVE_SIZE ); @@ -1823,7 +1820,18 @@ abstract class FileBackendStore extends FileBackend { * @return string MIME type */ protected function getContentType( $storagePath, $content, $fsPath ) { - return call_user_func_array( $this->mimeCallback, func_get_args() ); + if ( $this->mimeCallback ) { + return call_user_func_array( $this->mimeCallback, func_get_args() ); + } + + $mime = null; + if ( $fsPath !== null && function_exists( 'finfo_file' ) ) { + $finfo = finfo_open( FILEINFO_MIME_TYPE ); + $mime = finfo_file( $finfo, $fsPath ); + finfo_close( $finfo ); + } + + return is_string( $mime ) ? $mime : 'unknown/unknown'; } } |