Re-added and simplified file_transfer() by reusing the logic of StreamedResponse.

8.0.x
Dave Reid 2012-05-26 13:18:48 -05:00
parent 327b2bcd1b
commit 6340061ba8
3 changed files with 27 additions and 30 deletions

View File

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

View File

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

View File

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