From 6340061ba825a6810636b1618facf10b3dceb14c Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Sat, 26 May 2012 13:18:48 -0500 Subject: [PATCH] Re-added and simplified file_transfer() by reusing the logic of StreamedResponse. --- core/includes/file.inc | 33 +++++++++++++------ core/modules/image/image.module | 11 +------ .../modules/update_test/update_test.module | 13 ++------ 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/core/includes/file.inc b/core/includes/file.inc index 1d2f54dc10c..f6cab86758b 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -1967,6 +1967,28 @@ 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) { + return new StreamedResponse(function() use ($uri) { + // Transfer file in 1024 byte chunks to save memory usage. + if (file_exists($uri) && $fd = fopen($uri, 'rb')) { + while (!feof($fd)) { + print fread($fd, 1024); + } + fclose($fd); + } + }, 200, $headers); +} + /** * Page callback: Handles private file transfers. * @@ -2003,16 +2025,7 @@ function file_download() { } } if (count($headers)) { - 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); + return file_transfer($uri, $headers); } throw new AccessDeniedHttpException(); } diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 2a445cfc9b7..9d94ac23688 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -734,16 +734,7 @@ function image_style_deliver($style, $scheme) { '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); + return file_transfer($uri, $headers); } else { watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri)); diff --git a/core/modules/update/tests/modules/update_test/update_test.module b/core/modules/update/tests/modules/update_test/update_test.module index b40f2740f98..6254d251e7d 100644 --- a/core/modules/update/tests/modules/update_test/update_test.module +++ b/core/modules/update/tests/modules/update_test/update_test.module @@ -115,19 +115,12 @@ function update_test_mock_page($project_name) { $path = drupal_get_path('module', 'update_test'); $file = "$path/$project_name.$availability_scenario.xml"; + $headers = array('Content-Type' => 'text/xml; charset=utf-8'); if (!is_file($file)) { // Return an empty response. - return new Response('', 200, array('Content-Type' => 'text/xml; charset=utf-8')); + return new Response('', 200, $headers); } - return new StreamedResponse(function() use ($file) { - // Transfer file in 1024 byte chunks to save memory usage. - if ($fd = fopen($file, 'rb')) { - while (!feof($fd)) { - print fread($fd, 1024); - } - fclose($fd); - } - }, 200, array('Content-Type' => 'text/xml; charset=utf-8')); + return file_transfer($file, $headers); } /**