Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Add stream download response #22777

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Illuminate/Contracts/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 39 additions & 2 deletions src/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down