[FileRepo] Added option to shorten long file names.
authorAaron <aschulz@wikimedia.org>
Fri, 31 Aug 2012 21:21:39 +0000 (14:21 -0700)
committerAaron <aschulz@wikimedia.org>
Fri, 31 Aug 2012 23:33:59 +0000 (16:33 -0700)
Change-Id: Id36e787192b32b72d3b49e2afbe41335b81ae732

includes/DefaultSettings.php
includes/filerepo/FileRepo.php
includes/filerepo/file/File.php

index 6aa15be..c65d133 100644 (file)
@@ -399,7 +399,9 @@ $wgImgAuthPublicTest = true;
  *
  *   - articleUrl        Equivalent to $wgArticlePath, e.g. http://en.wikipedia.org/wiki/$1
  *   - fetchDescription  Fetch the text of the remote file description page. Equivalent to
- *                      $wgFetchCommonsDescriptions.
+ *                       $wgFetchCommonsDescriptions.
+ *   - abbrvThreshold    File names over this size will use the short form of thumbnail names.
+ *                       Short thumbnail names only have the width, parameters, and the extension.
  *
  * ForeignDBRepo:
  *   - dbType, dbServer, dbUser, dbPassword, dbName, dbFlags
index bef2642..30d6825 100644 (file)
@@ -51,6 +51,7 @@ class FileRepo {
        var $pathDisclosureProtection = 'simple'; // 'paranoid'
        var $descriptionCacheExpiry, $url, $thumbUrl;
        var $hashLevels, $deletedHashLevels;
+       protected $abbrvThreshold;
 
        /**
         * Factory functions for creating new files
@@ -113,6 +114,9 @@ class FileRepo {
                        ? $info['deletedHashLevels']
                        : $this->hashLevels;
                $this->transformVia404 = !empty( $info['transformVia404'] );
+               $this->abbrvThreshold = isset( $info['abbrvThreshold'] )
+                       ? $info['abbrvThreshold']
+                       : 255;
                $this->isPrivate = !empty( $info['isPrivate'] );
                // Give defaults for the basic zones...
                $this->zones = isset( $info['zones'] ) ? $info['zones'] : array();
@@ -839,10 +843,11 @@ class FileRepo {
         *
         * @param $src string File system path
         * @param $dst string Virtual URL or storage path
+        * @param $disposition string|null Content-Disposition if given and supported
         * @return FileRepoStatus
         */
-       final public function quickImport( $src, $dst ) {
-               return $this->quickImportBatch( array( array( $src, $dst ) ) );
+       final public function quickImport( $src, $dst, $disposition = null ) {
+               return $this->quickImportBatch( array( array( $src, $dst, $disposition ) ) );
        }
 
        /**
@@ -878,7 +883,9 @@ class FileRepo {
         * This function can be used to write to otherwise read-only foreign repos.
         * This is intended for copying generated thumbnails into the repo.
         *
-        * @param $pairs Array List of tuples (file system path, virtual URL or storage path)
+        * When "dispositions" are given they are used as Content-Disposition if supported.
+        *
+        * @param $pairs Array List of tuples (file system path, virtual URL/storage path, disposition)
         * @return FileRepoStatus
         */
        public function quickImportBatch( array $pairs ) {
@@ -888,9 +895,10 @@ class FileRepo {
                        list ( $src, $dst ) = $pair;
                        $dst = $this->resolveToStoragePath( $dst );
                        $operations[] = array(
-                               'op'        => 'store',
-                               'src'       => $src,
-                               'dst'       => $dst
+                               'op'          => 'store',
+                               'src'         => $src,
+                               'dst'         => $dst,
+                               'disposition' => isset( $pair[2] ) ? $pair[2] : null
                        );
                        $status->merge( $this->initDirectory( dirname( $dst ) ) );
                }
@@ -1549,6 +1557,21 @@ class FileRepo {
                return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
        }
 
+       /**
+        * Get the portion of the file that contains the origin file name.
+        * If that name is too long, then the name "thumbnail.<ext>" will be given.
+        *
+        * @param $name string
+        * @return string
+        */
+       public function nameForThumb( $name ) {
+               if ( strlen( $name ) > $this->abbrvThreshold ) {
+                       $ext  = FileBackend::extensionFromPath( $name );
+                       $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
+               }
+               return $name;
+       }
+
        /**
         * Returns true if this the local file repository.
         *
index dd54455..4cc47f0 100644 (file)
@@ -767,7 +767,8 @@ abstract class File {
         * @return string
         */
        function thumbName( $params ) {
-               return $this->generateThumbName( $this->getName(), $params );
+               $name = $this->repo ? $this->repo->nameForThumb( $this->getName() ) : $this->getName();
+               return $this->generateThumbName( $name, $params );
        }
 
        /**
@@ -942,7 +943,8 @@ abstract class File {
                                }
                        } elseif ( $this->repo && $thumb->hasFile() && !$thumb->fileIsSource() ) {
                                // Copy the thumbnail from the file system into storage...
-                               $status = $this->repo->quickImport( $tmpThumbPath, $thumbPath );
+                               $disposition = FileBackend::makeContentDisposition( 'inline', $this->name );
+                               $status = $this->repo->quickImport( $tmpThumbPath, $thumbPath, $disposition );
                                if ( $status->isOK() ) {
                                        $thumb->setStoragePath( $thumbPath );
                                } else {