Skip to content

Commit

Permalink
Add stream download response
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoKouzelis committed Jan 13, 2018
1 parent 33ea741 commit 41a585d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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

0 comments on commit 41a585d

Please sign in to comment.