Have ?download parameter trigger Content-Disposition: attachment
authorGilles Dubuc <gdubuc@wikimedia.org>
Mon, 24 Mar 2014 23:31:21 +0000 (00:31 +0100)
committerGilles Dubuc <gdubuc@wikimedia.org>
Tue, 25 Mar 2014 10:04:50 +0000 (11:04 +0100)
This parameter triggers Content-Disposition: Attachment which
makes the browser download the image instead of displaying it.

This is needed by Media Viewer to allow users to click a button in
order to download an image at a given resolution or the original.

Change-Id: I470a24a09139ac65588312104995e34d97a89b0f

img_auth.php
includes/filerepo/file/File.php
thumb.php

index 391fb29..a0976af 100644 (file)
@@ -150,6 +150,10 @@ function wfImageAuthMain() {
                return;
        }
 
+       if ( $wgRequest->getCheck( 'download' ) ) {
+               header( 'Content-Disposition: attachment' );
+       }
+
        // Stream the requested file
        wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
        $repo->streamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
index 950baea..c19a51d 100644 (file)
@@ -1066,16 +1066,17 @@ abstract class File {
 
        /**
         * @param string $thumbName Thumbnail name
+        * @param string $dispositionType Type of disposition (either "attachment" or "inline")
         * @return string Content-Disposition header value
         */
-       function getThumbDisposition( $thumbName ) {
+       function getThumbDisposition( $thumbName, $dispositionType = 'inline' ) {
                $fileName = $this->name; // file name to suggest
                $thumbExt = FileBackend::extensionFromPath( $thumbName );
                if ( $thumbExt != '' && $thumbExt !== $this->getExtension() ) {
                        $fileName .= ".$thumbExt";
                }
 
-               return FileBackend::makeContentDisposition( 'inline', $fileName );
+               return FileBackend::makeContentDisposition( $dispositionType, $fileName );
        }
 
        /**
index 88aecbd..b0d9f10 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -289,8 +289,10 @@ function wfStreamThumb( array $params ) {
                }
        }
 
+       $dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline';
+
        // Suggest a good name for users downloading this thumbnail
-       $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName )}";
+       $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
 
        if ( count( $varyHeader ) ) {
                $headers[] = 'Vary: ' . implode( ', ', $varyHeader );