From aa3462611e6db3701cce7768295a5da3d4985193 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Fri, 11 Feb 2011 19:10:31 +0000 Subject: [PATCH] * Remove code duplication for UploadStash: Move all thumbName generation code to its own function File::generateThumbName which accepts a $name parameter which can be called by File::thumbName() and any other functions that require it. The essentially duplicate function StashFile::getParamThumbName() has been removed. * Add a small url encoding fix --- includes/filerepo/File.php | 12 +++++++++- includes/specials/SpecialUploadStash.php | 10 +++++--- includes/upload/UploadStash.php | 29 ++++-------------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index ae4d2fed1a..809fa830d4 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -476,12 +476,22 @@ abstract class File { * @private -ish */ function thumbName( $params ) { + return $this->getNamedThumbName( $this->getName(), $params ); + } + + /** + * Generate a thumbnail file name from a name and specified parameters + * + * @param string $name + * @param array $params Parameters which will be passed to MediaHandler::makeParamString + */ + function generateThumbName( $name, $params ) { if ( !$this->getHandler() ) { return null; } $extension = $this->getExtension(); list( $thumbExt, $thumbMime ) = $this->handler->getThumbType( $extension, $this->getMimeType(), $params ); - $thumbName = $this->handler->makeParamString( $params ) . '-' . $this->getName(); + $thumbName = $this->handler->makeParamString( $params ) . '-' . $name; if ( $thumbExt != $extension ) { $thumbName .= ".$thumbExt"; } diff --git a/includes/specials/SpecialUploadStash.php b/includes/specials/SpecialUploadStash.php index 0c6d0d6096..c3c1bdc342 100644 --- a/includes/specials/SpecialUploadStash.php +++ b/includes/specials/SpecialUploadStash.php @@ -215,8 +215,12 @@ class SpecialUploadStash extends UnlistedSpecialPage { // do not use trailing slash global $wgUploadStashScalerBaseUrl; - $scalerThumbName = $file->getParamThumbName( $file->name, $params ); - $scalerThumbUrl = $wgUploadStashScalerBaseUrl . '/' . $file->getRel() . '/' . $scalerThumbName; + // We need to use generateThumbName() instead of thumbName(), because + // the suffix needs to match the file name for the remote thumbnailer + // to work + $scalerThumbName = $file->generateThumbName( $file->getName(), $params ); + $scalerThumbUrl = $wgUploadStashScalerBaseUrl . '/' . $file->getUrlRel() . + '/' . rawurlencode( $scalerThumbName ); // make a curl call to the scaler to create a thumbnail $httpOptions = array( @@ -226,7 +230,7 @@ class SpecialUploadStash extends UnlistedSpecialPage { $req = MWHttpRequest::factory( $scalerThumbUrl, $httpOptions ); $status = $req->execute(); if ( ! $status->isOK() ) { - $errors = $status->getErrorsArray(); + $errors = $status->getWikiTextArray( $status->getErrorsArray() ); throw new MWException( "Fetching thumbnail failed: " . join( ", ", $errors ) ); } $contentType = $req->getResponseHeader( "content-type" ); diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php index ece13eacf8..ce73b8ba67 100644 --- a/includes/upload/UploadStash.php +++ b/includes/upload/UploadStash.php @@ -301,36 +301,15 @@ class UploadStashFile extends UnregisteredLocalFile { } /** - * Return the file/url base name of a thumbnail with the specified parameters + * Return the file/url base name of a thumbnail with the specified parameters. + * We override this because we want to use the pretty url name instead of the + * ugly file name. * * @param $params Array: handler-specific parameters * @return String: base name for URL, like '120px-12345.jpg', or null if there is no handler */ function thumbName( $params ) { - return $this->getParamThumbName( $this->getUrlName(), $params ); - } - - - /** - * Given the name of the original, i.e. Foo.jpg, and scaling parameters, returns filename with appropriate extension - * This is abstracted from getThumbName because we also use it to calculate the thumbname the file should have on - * remote image scalers - * - * @param String $urlName: A filename, like MyMovie.ogx - * @param Array $parameters: scaling parameters, like array( 'width' => '120' ); - * @return String|null parameterized thumb name, like 120px-MyMovie.ogx.jpg, or null if no handler found - */ - function getParamThumbName( $urlName, $params ) { - if ( !$this->getHandler() ) { - return null; - } - $extension = $this->getExtension(); - list( $thumbExt, ) = $this->handler->getThumbType( $extension, $this->getMimeType(), $params ); - $thumbName = $this->getHandler()->makeParamString( $params ) . '-' . $urlName; - if ( $thumbExt != $extension ) { - $thumbName .= ".$thumbExt"; - } - return $thumbName; + return $this->generateThumbName( $this->getUrlName(), $params ); } /** -- 2.20.1