Closed (fixed)
Project:
Drupal core
Version:
8.0.x-dev
Component:
base system
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
26 May 2012 at 16:55 UTC
Updated:
29 Jul 2014 at 20:44 UTC
Based on the discussion around file_transfer(): #1463656: Add a Drupal kernel; leverage HttpFoundation and HttpKernel.
8.x: file_transfer() is used by image_style_deliver() and file_download().
The kernel patch introduces StreamedResponses there to transfer files, inlining the code for streaming the files.
diff --git a/core/includes/file.inc b/core/includes/file.inc
index f204989..91e7eca 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.incundefined
@@ -5,6 +5,9 @@
* API for handling file uploads and server file management.
*/
+use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use Symfony\Component\HttpFoundation\StreamedResponse;
use Drupal\Core\StreamWrapper\LocalStream;
/**
@@ -2026,18 +2029,27 @@ function file_download() {
$function = $module . '_file_download';
$result = $function($uri);
if ($result == -1) {
- return drupal_access_denied();
+ throw new AccessDeniedHttpException();
}
if (isset($result) && is_array($result)) {
$headers = array_merge($headers, $result);
}
}
if (count($headers)) {
- file_transfer($uri, $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 drupal_access_denied();
+ throw new AccessDeniedHttpException();
}
- return drupal_not_found();
+ throw new NotFoundHttpException();
}- readfile("$path/$project_name.$availability_scenario.xml");
+ $file = "$path/$project_name.$availability_scenario.xml";
+ if (!is_file($file)) {
+ // Return an empty response.
+ return new Response('', 200, array('Content-Type' => 'text/xml; charset=utf-8'));
+ }
+ 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'));Also note #1561362: Change file_transfer() to use BinaryFileResponse that is about making a StreamedFileResponse or another replacement, or something in Symfony upstream.
Comments
Comment #1
Niklas Fiekas commentedComment #1.0
Niklas Fiekas commentedUpdated issue summary.
Comment #2
dave reidI don't understand why we can't simplify the kernel branch right now with this change? We're making more work and the kernel patch harder to review, only to undo it later.
Comment #3
dave reidResolved with http://drupalcode.org/sandbox/Crell/1260830.git/commit/6340061 on the wscci kernel branch.
Comment #4.0
(not verified) commented.