From 327b2bcd1b9ee17d6ac9b142743d228e42c365c8 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Sat, 26 May 2012 12:33:25 -0500 Subject: [PATCH] Remove file_transfer and convert all uses of it to StreamedResponse. --- core/includes/file.inc | 36 ++------------------------------- core/modules/image/image.module | 23 +++++++++++++++++---- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/core/includes/file.inc b/core/includes/file.inc index 91e7ecaf136..1d2f54dc10c 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -1967,39 +1967,6 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX return file_unmanaged_move($temp_name, $destination, $replace); } -/** - * Transfers a file to the client using HTTP. - * - * Pipes a file through Drupal to the client. - * - * @param $uri - * String specifying the file URI to transfer. - * @param $headers - * An array of HTTP headers to send along with file. - */ -function file_transfer($uri, $headers) { - if (ob_get_level()) { - ob_end_clean(); - } - - foreach ($headers as $name => $value) { - drupal_add_http_header($name, $value); - } - drupal_send_headers(); - $scheme = file_uri_scheme($uri); - // Transfer file in 1024 byte chunks to save memory usage. - if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) { - while (!feof($fd)) { - print fread($fd, 1024); - } - fclose($fd); - } - else { - drupal_not_found(); - } - drupal_exit(); -} - /** * Page callback: Handles private file transfers. * @@ -2529,7 +2496,8 @@ function file_directory_temp() { * A file object. * * @return - * An associative array of headers, as expected by file_transfer(). + * An associative array of headers, as expected by + * \Symfony\Component\HttpFoundation\StreamedResponse. */ function file_get_content_headers($file) { $name = mime_header_encode($file->filename); diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 3edf83c389a..2a445cfc9b7 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -5,6 +5,9 @@ * Exposes global functionality for creating image styles. */ +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\StreamedResponse; + /** * Image style constant for user presets in the database. */ @@ -726,13 +729,25 @@ function image_style_deliver($style, $scheme) { if ($success) { $image = image_load($derivative_uri); - file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size'])); + $uri = $image->source; + $headers = array( + 'Content-Type' => $image->info['mime_type'], + 'Content-Length' => $image->info['file_size'], + ); + return new StreamedResponse(function() use ($uri) { + $scheme = file_uri_scheme($uri); + // Transfer file in 1024 byte chunks to save memory usage. + if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) { + while (!feof($fd)) { + print fread($fd, 1024); + } + fclose($fd); + } + }, 200, $headers); } else { watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri)); - drupal_add_http_header('Status', '500 Internal Server Error'); - print t('Error generating image.'); - drupal_exit(); + return new Response(t('Error generating image.'), 500); } }