From 303d8be8ee4e39792eac388c171ef0e870ad621d Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 31 Aug 2012 14:21:39 -0700 Subject: [PATCH] [FileRepo] Added option to shorten long file names. Change-Id: Id36e787192b32b72d3b49e2afbe41335b81ae732 --- includes/DefaultSettings.php | 4 +++- includes/filerepo/FileRepo.php | 35 +++++++++++++++++++++++++++------ includes/filerepo/file/File.php | 6 ++++-- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 6aa15be9e4..c65d13338a 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index bef2642f73..30d6825af2 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -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." 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. * diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index dd54455233..4cc47f0041 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -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 { -- 2.20.1