Factored out a small filebackend getContentType() function
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 23 Sep 2013 19:58:26 +0000 (12:58 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 25 Sep 2013 16:57:07 +0000 (09:57 -0700)
Change-Id: I221483ab4a0ed36ea34c19db1740dfaecf223677

includes/filebackend/FileBackendStore.php
includes/filebackend/SwiftFileBackend.php

index e976a7a..8ff383b 100644 (file)
 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() );
+       }
 }
 
 /**
index f3aa145..a620f88 100644 (file)
@@ -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'] );