From 29fc82c364e2e90330d9858528e50d785e5e66bd Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 13 Feb 2012 23:35:33 +0100 Subject: [PATCH] Send gallery album thumbnail with OC_Response::sendFile --- apps/gallery/ajax/galleryOp.php | 6 +++--- lib/image.php | 5 +++++ lib/response.php | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php index 9273de2599d..5ac6d295108 100644 --- a/apps/gallery/ajax/galleryOp.php +++ b/apps/gallery/ajax/galleryOp.php @@ -41,9 +41,9 @@ function handleRemove($name) { function handleGetThumbnails($albumname) { OC_Response::enableCaching(3600 * 24); // 24 hour - $photo = new OC_Image(); - $photo->loadFromFile(OC::$CONFIG_DATADIRECTORY.'/../gallery/'.$albumname.'.png'); - $photo->show(); + $thumbnail = OC::$CONFIG_DATADIRECTORY.'/../gallery/'.$albumname.'.png'; + header('Content-Type: '.OC_Image::getMimeTypeForFile($thumbnail)); + OC_Response::sendFile($thumbnail); } function handleGalleryScanning() { diff --git a/lib/image.php b/lib/image.php index df8f76352a0..b1d3a14f415 100644 --- a/lib/image.php +++ b/lib/image.php @@ -48,6 +48,11 @@ class OC_Image { protected $imagetype = IMAGETYPE_PNG; // Default to png if file type isn't evident. protected $filepath = null; + static public function getMimeTypeForFile($filepath) { + $imagetype = exif_imagetype($filepath); + return $imagetype ? image_type_to_mime_type($imagetype) : ''; + } + /** * @brief Constructor. * @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function. diff --git a/lib/response.php b/lib/response.php index a768366b02c..f47534aeefb 100644 --- a/lib/response.php +++ b/lib/response.php @@ -10,6 +10,7 @@ class OC_Response { const STATUS_FOUND = 304; const STATUS_NOT_MODIFIED = 304; const STATUS_TEMPORARY_REDIRECT = 307; + const STATUS_NOT_FOUND = 404; static public function enableCaching($cache_time = null) { if (is_numeric($cache_time)) { @@ -47,6 +48,9 @@ class OC_Response { case self::STATUS_FOUND; $status = $status . ' Found'; break; + case self::STATUS_NOT_FOUND; + $status = $status . ' Not Found'; + break; } header($protocol.' '.$status); } @@ -85,6 +89,9 @@ class OC_Response { if (empty($lastModified)) { return; } + if (is_int($lastModified)) { + $lastModified = gmdate(DateTime::RFC2822, $lastModified); + } if ($lastModified instanceof DateTime) { $lastModified = $lastModified->format(DateTime::RFC2822); } @@ -95,4 +102,18 @@ class OC_Response { } header('Last-Modified: '.$lastModified); } + + static public function sendFile($filepath=null) { + $fp = fopen($filepath, 'rb'); + if ($fp) { + self::setLastModifiedHeader(filemtime($filepath)); + self::setETagHeader(md5_file($filepath)); + + header('Content-Length: '.filesize($filepath)); + fpassthru($fp); + } + else { + self::setStatus(self::STATUS_NOT_FOUND); + } + } } -- 2.39.5