From 41a585dc9bfeec8bce442bc7ee35f351c54a0768 Mon Sep 17 00:00:00 2001 From: Theo Kouzelis Date: Sat, 13 Jan 2018 20:41:53 +0000 Subject: [PATCH] Add stream download response --- .../Contracts/Routing/ResponseFactory.php | 13 +++++- src/Illuminate/Routing/ResponseFactory.php | 41 ++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Contracts/Routing/ResponseFactory.php b/src/Illuminate/Contracts/Routing/ResponseFactory.php index 5dd4ee08c955..1d28e2e87d9c 100644 --- a/src/Illuminate/Contracts/Routing/ResponseFactory.php +++ b/src/Illuminate/Contracts/Routing/ResponseFactory.php @@ -58,11 +58,22 @@ public function jsonp($callback, $data = [], $status = 200, array $headers = [], */ public function stream($callback, $status = 200, array $headers = []); + /** + * Return a new streamed response as a file download from the application. + * + * @param \Closure $callback + * @param string|null $name + * @param array $headers + * @param string|null $disposition + * @return \Symfony\Component\HttpFoundation\StreamedResponse + */ + public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment'); + /** * Create a new file download response. * * @param \SplFileInfo|string $file - * @param string $name + * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\BinaryFileResponse diff --git a/src/Illuminate/Routing/ResponseFactory.php b/src/Illuminate/Routing/ResponseFactory.php index 0fe015122d05..48fad1e3bd02 100644 --- a/src/Illuminate/Routing/ResponseFactory.php +++ b/src/Illuminate/Routing/ResponseFactory.php @@ -111,11 +111,37 @@ public function stream($callback, $status = 200, array $headers = []) return new StreamedResponse($callback, $status, $headers); } + /** + * Return a new streamed response as a file download from the application. + * + * @param \Closure $callback + * @param string|null $name + * @param array $headers + * @param string|null $disposition + * @return \Symfony\Component\HttpFoundation\StreamedResponse + */ + public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment') + { + $response = new StreamedResponse($callback, 200, $headers); + + if (! is_null($name)) { + $dispositionHeader = $response->headers->makeDisposition( + $disposition, + $name, + $this->fallbackName($name) + ); + + $response->headers->set('Content-Disposition', $dispositionHeader); + } + + return $response; + } + /** * Create a new file download response. * * @param \SplFileInfo|string $file - * @param string $name + * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\BinaryFileResponse @@ -125,12 +151,23 @@ public function download($file, $name = null, array $headers = [], $disposition $response = new BinaryFileResponse($file, 200, $headers, true, $disposition); if (! is_null($name)) { - return $response->setContentDisposition($disposition, $name, str_replace('%', '', Str::ascii($name))); + return $response->setContentDisposition($disposition, $name, $this->fallbackName($name)); } return $response; } + /** + * Returns a string containing only ASCII characters that is semantically equivalent to $name. + * + * @param string $name + * @return string + */ + protected function fallbackName($name) + { + return str_replace('%', '', Str::ascii($name)); + } + /** * Return the raw contents of a binary file. *