From 4d9e07056ed0185183d9f80ab3b0353b503edfbe Mon Sep 17 00:00:00 2001 From: Gilles Dubuc Date: Tue, 25 Mar 2014 00:31:21 +0100 Subject: [PATCH] Have ?download parameter trigger Content-Disposition: attachment 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 | 4 ++++ includes/filerepo/file/File.php | 5 +++-- thumb.php | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/img_auth.php b/img_auth.php index 391fb291b8..a0976af3b0 100644 --- a/img_auth.php +++ b/img_auth.php @@ -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' ) ); diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index 950baea11b..c19a51df2c 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -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 ); } /** diff --git a/thumb.php b/thumb.php index 88aecbdbf9..b0d9f10edd 100644 --- 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 ); -- 2.20.1