From 25157f7cf9ba7948543412bfa56c472ec6851f46 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 3 Oct 2008 02:23:11 +0000 Subject: [PATCH] API CHANGE: Deprecated HTTP::sendFileToBrowser() in favour of more testable HTTPRequest::send_file() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63579 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/HTTP.php | 24 +++--------------------- core/control/HTTPRequest.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/core/HTTP.php b/core/HTTP.php index f75b8ee23..da5bda927 100644 --- a/core/HTTP.php +++ b/core/HTTP.php @@ -133,27 +133,9 @@ class HTTP { * exits() after the call, so that no further output is given */ static function sendFileToBrowser($fileData, $fileName, $mimeType = false) { - if(!$mimeType) $mimeType = self::getMimeType($fileName); - $ext = strtolower(substr($fileName,strrpos($fileName,'.')+1)); - $inlineExtensions = array('pdf','png','jpg','jpe','gif','swf','htm','html','txt','text','avi','wmv','mov','mpe','mpg','mp3','mpeg'); - - if(in_array($ext, $inlineExtensions)) $inline = true; - - header("Content-Type: $mimeType; name=\"" . addslashes($fileName) . "\""); - //header("Content-Type: $mimeType" ); - // Downloadable - //if(!$inline) - $dispHeader = "Content-disposition: attachment; filename=" . addslashes($fileName) . ""; - - // Debug::message('CD: ' . strlen( $dispHeader ) ); - - - header( $dispHeader ); - header("Content-Length: " . strlen($fileData)); - - echo $fileData; - - exit(); + user_error("HTTP::sendFileToBrowser() deprecated; return a HTTPResponse::send_file() object instead", E_USER_NOTICE); + HTTPRequest::send_file($fileData, $fileName, $mimeType)->output(); + exit(0); } /* diff --git a/core/control/HTTPRequest.php b/core/control/HTTPRequest.php index fa15651c9..fdc5b3900 100644 --- a/core/control/HTTPRequest.php +++ b/core/control/HTTPRequest.php @@ -196,6 +196,20 @@ class HTTPRequest extends Object implements ArrayAccess { parent::__construct(); } + /** + * Construct an HTTPResponse that will deliver a file to the client + */ + static function send_file($fileData, $fileName, $mimeType = null) { + if(!$mimeType) $mimeType = HTTP::getMimeType($fileName); + + $response = new HTTPResponse($fileData); + $response->addHeader("Content-Type", "$mimeType; name=\"" . addslashes($fileName) . "\""); + $response->addHeader("Content-disposition", "attachment; filename=" . addslashes($fileName)); + $response->addHeader("Content-Length", strlen($fileData)); + + return $response; + } + /** * Matches a URL pattern * The pattern can contain a number of segments, separted by / (and an extension indicated by a .)