From: Aaron Schulz Date: Sat, 13 Oct 2012 00:24:31 +0000 (-0700) Subject: [FileRepo] Allow different file URLs for media based on file extension. X-Git-Tag: 1.31.0-rc.0~21712 X-Git-Url: http://git.cyclocoop.org/%24self?a=commitdiff_plain;h=33385876042d4aeaccd690fcfc9c1aa866b36752;p=lhc%2Fweb%2Fwiklou.git [FileRepo] Allow different file URLs for media based on file extension. Change-Id: Ib647377312c93c8ed046f7b9510d0e656788cdc3 --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 16cae7d2c5..e57b9d7568 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -361,7 +361,9 @@ $wgImgAuthPublicTest = true; * container : backend container name the zone is in * directory : root path within container for the zone * url : base URL to the root of the zone - * handlerUrl : base script handled URL to the root of the zone + * urlsByExt : map of file extension types to base URLs + * (useful for using a different cache for videos) + * handlerUrl : base script-handled URL to the root of the zone * (see FileRepo::getZoneHandlerUrl() function) * Zones default to using "-" as the container name * and default to using the container root as the zone's root directory. diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 6cd93cc146..651ee27127 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -127,6 +127,9 @@ class FileRepo { if ( !isset( $this->zones[$zone]['directory'] ) ) { $this->zones[$zone]['directory'] = ''; } + if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) { + $this->zones[$zone]['urlsByExt'] = array(); + } } } @@ -196,14 +199,17 @@ class FileRepo { /** * Get the URL corresponding to one of the four basic zones * - * @param $zone String: one of: public, deleted, temp, thumb + * @param $zone String One of: public, deleted, temp, thumb + * @param $ext String|null Optional file extension * @return String or false */ - public function getZoneUrl( $zone ) { - if ( isset( $this->zones[$zone]['url'] ) - && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) ) - { - return $this->zones[$zone]['url']; // custom URL + public function getZoneUrl( $zone, $ext = null ) { + if ( in_array( $zone, array( 'public', 'temp', 'thumb' ) ) ) { // standard public zones + if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) { + return $this->zones[$zone]['urlsByExt'][$ext]; // custom URL for extension/zone + } elseif ( isset( $this->zones[$zone]['url'] ) ) { + return $this->zones[$zone]['url']; // custom URL for zone + } } switch ( $zone ) { case 'public': diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index 557609d4b0..9a080ae04b 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -316,7 +316,8 @@ abstract class File { public function getUrl() { if ( !isset( $this->url ) ) { $this->assertRepoDefined(); - $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel(); + $ext = $this->getExtension(); + $this->url = $this->repo->getZoneUrl( 'public', $ext ) . '/' . $this->getUrlRel(); } return $this->url; } @@ -1253,7 +1254,8 @@ abstract class File { */ function getArchiveUrl( $suffix = false ) { $this->assertRepoDefined(); - $path = $this->repo->getZoneUrl( 'public' ) . '/archive/' . $this->getHashPath(); + $ext = $this->getExtension(); + $path = $this->repo->getZoneUrl( 'public', $ext ) . '/archive/' . $this->getHashPath(); if ( $suffix === false ) { $path = substr( $path, 0, -1 ); } else { @@ -1272,7 +1274,8 @@ abstract class File { */ function getArchiveThumbUrl( $archiveName, $suffix = false ) { $this->assertRepoDefined(); - $path = $this->repo->getZoneUrl( 'thumb' ) . '/archive/' . + $ext = $this->getExtension(); + $path = $this->repo->getZoneUrl( 'thumb', $ext ) . '/archive/' . $this->getHashPath() . rawurlencode( $archiveName ) . "/"; if ( $suffix === false ) { $path = substr( $path, 0, -1 ); @@ -1291,7 +1294,8 @@ abstract class File { */ function getThumbUrl( $suffix = false ) { $this->assertRepoDefined(); - $path = $this->repo->getZoneUrl( 'thumb' ) . '/' . $this->getUrlRel(); + $ext = $this->getExtension(); + $path = $this->repo->getZoneUrl( 'thumb', $ext ) . '/' . $this->getUrlRel(); if ( $suffix !== false ) { $path .= '/' . rawurlencode( $suffix ); } diff --git a/thumb.php b/thumb.php index b0d72f3df7..aa23335345 100644 --- a/thumb.php +++ b/thumb.php @@ -305,10 +305,11 @@ function wfStreamThumb( array $params ) { function wfExtractThumbParams( $uriPath ) { $repo = RepoGroup::singleton()->getLocalRepo(); + $ext = FileBackend::extensionFromPath( $uriPath ); // Zone URL might be relative ("/images") or protocol-relative ("//lang.site/image") $zoneUriPath = $repo->getZoneHandlerUrl( 'thumb' ) ? $repo->getZoneHandlerUrl( 'thumb' ) // custom URL - : $repo->getZoneUrl( 'thumb' ); // default to main URL + : $repo->getZoneUrl( 'thumb', $ext ); // default to main URL $bits = wfParseUrl( wfExpandUrl( $zoneUriPath, PROTO_INTERNAL ) ); if ( $bits && isset( $bits['path'] ) ) { $zoneUriPath = $bits['path'];