Remove file_transfer and convert all uses of it to StreamedResponse.

8.0.x
Larry Garfield 2012-05-26 12:33:25 -05:00
parent b02b51cce1
commit 327b2bcd1b
2 changed files with 21 additions and 38 deletions

View File

@ -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);

View File

@ -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);
}
}