aboutsummaryrefslogtreecommitdiffstats
path: root/includes/filebackend
diff options
context:
space:
mode:
authorAaron Schulz <aschulz@wikimedia.org>2013-09-23 12:58:26 -0700
committerAaron Schulz <aschulz@wikimedia.org>2013-09-25 09:57:07 -0700
commit91cab51cb5dc7362ed3462ed5a273a7bafd32a45 (patch)
tree3c3aa2942711c4e864f982be19cbb48404b76a13 /includes/filebackend
parent69eb259f38b5caa170e38c42dcc4b7d731ddf234 (diff)
downloadmediawikicore-91cab51cb5dc7362ed3462ed5a273a7bafd32a45.tar.gz
mediawikicore-91cab51cb5dc7362ed3462ed5a273a7bafd32a45.zip
Factored out a small filebackend getContentType() function
Change-Id: I221483ab4a0ed36ea34c19db1740dfaecf223677
Diffstat (limited to 'includes/filebackend')
-rw-r--r--includes/filebackend/FileBackendStore.php37
-rw-r--r--includes/filebackend/SwiftFileBackend.php10
2 files changed, 33 insertions, 14 deletions
diff --git a/includes/filebackend/FileBackendStore.php b/includes/filebackend/FileBackendStore.php
index e976a7aa9c8b..8ff383bd212c 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 f3aa14598191..a620f883b951 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'] );