From 91cab51cb5dc7362ed3462ed5a273a7bafd32a45 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 23 Sep 2013 12:58:26 -0700 Subject: [PATCH] Factored out a small filebackend getContentType() function Change-Id: I221483ab4a0ed36ea34c19db1740dfaecf223677 --- includes/filebackend/FileBackendStore.php | 37 +++++++++++++++++++---- includes/filebackend/SwiftFileBackend.php | 10 ++---- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/includes/filebackend/FileBackendStore.php b/includes/filebackend/FileBackendStore.php index e976a7aa9c..8ff383bd21 100644 --- a/includes/filebackend/FileBackendStore.php +++ b/includes/filebackend/FileBackendStore.php @@ -38,13 +38,16 @@ abstract class FileBackendStore extends FileBackend { /** @var BagOStuff */ protected $memCache; - /** @var ProcessCacheLRU */ - protected $cheapCache; // Map of paths to small (RAM/disk) cache items - /** @var ProcessCacheLRU */ - protected $expensiveCache; // Map of paths to large (RAM/disk) cache items + /** @var ProcessCacheLRU Map of paths to small (RAM/disk) cache items */ + protected $cheapCache; + /** @var ProcessCacheLRU Map of paths to large (RAM/disk) cache items */ + protected $expensiveCache; - /** @var Array Map of container names to sharding settings */ - protected $shardViaHashLevels = array(); // (container name => config array) + /** @var Array Map of container names to sharding config */ + protected $shardViaHashLevels = array(); + + /** @var callback Method to get the MIME type of files */ + protected $mimeCallback; protected $maxFileSize = 4294967296; // integer bytes (4GiB) @@ -54,11 +57,21 @@ abstract class FileBackendStore extends FileBackend { /** * @see FileBackend::__construct() + * Additional $config params include: + * - 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. * * @param array $config */ public function __construct( array $config ) { 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'; + }; $this->memCache = new EmptyBagOStuff(); // disabled by default $this->cheapCache = new ProcessCacheLRU( self::CACHE_CHEAP_SIZE ); $this->expensiveCache = new ProcessCacheLRU( self::CACHE_EXPENSIVE_SIZE ); @@ -1583,6 +1596,18 @@ abstract class FileBackendStore extends FileBackend { } return $opts; } + + /** + * Get the content type to use in HEAD/GET requests for a file + * + * @param string $storagePath + * @param string|null $content File data + * @param string|null $fsPath File system path + * @return MIME type + */ + protected function getContentType( $storagePath, $content, $fsPath ) { + return call_user_func_array( $this->mimeCallback, func_get_args() ); + } } /** diff --git a/includes/filebackend/SwiftFileBackend.php b/includes/filebackend/SwiftFileBackend.php index f3aa145981..a620f883b9 100644 --- a/includes/filebackend/SwiftFileBackend.php +++ b/includes/filebackend/SwiftFileBackend.php @@ -252,10 +252,7 @@ class SwiftFileBackend extends FileBackendStore { // The MD5 here will be checked within Swift against its own MD5. $obj->set_etag( md5( $params['content'] ) ); // Use the same content type as StreamFile for security - $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] ); - if ( !strlen( $obj->content_type ) ) { // special case - $obj->content_type = 'unknown/unknown'; - } + $obj->content_type = $this->getContentType( $params['dst'], $params['content'], null ); // Set any other custom headers if requested if ( isset( $params['headers'] ) ) { $obj->headers += $this->sanitizeHdrs( $params['headers'] ); @@ -329,10 +326,7 @@ class SwiftFileBackend extends FileBackendStore { // The MD5 here will be checked within Swift against its own MD5. $obj->set_etag( md5_file( $params['src'] ) ); // Use the same content type as StreamFile for security - $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] ); - if ( !strlen( $obj->content_type ) ) { // special case - $obj->content_type = 'unknown/unknown'; - } + $obj->content_type = $this->getContentType( $params['dst'], null, $params['src'] ); // Set any other custom headers if requested if ( isset( $params['headers'] ) ) { $obj->headers += $this->sanitizeHdrs( $params['headers'] ); -- 2.20.1