Skip to content

Commit c01418c

Browse files
authored
Merge pull request #9 from srdjanmarjanovic/master
Added ability to instruct nginx to take over serving of a file with X-Accel-Redirect header
2 parents 9c25d75 + 9b614f7 commit c01418c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Response/FileDownloadResponse.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,20 @@ class FileDownloadResponse implements ResponseInterface
4242
*/
4343
private $x_type;
4444

45+
/**
46+
* @var string|null
47+
*/
48+
private $x_accel_redirect_to;
49+
4550
/**
4651
* @param string $file
4752
* @param string $content_type
4853
* @param bool $inline
4954
* @param string $filename
5055
* @param string|null $x_type
56+
* @param string|null $x_accel_redirect_to
5157
*/
52-
public function __construct($file, $content_type, $inline = false, $filename = null, $x_type = null)
58+
public function __construct($file, $content_type, $inline = false, $filename = null, $x_type = null, $x_accel_redirect_to = null)
5359
{
5460
if (!is_file($file)) {
5561
throw new RuntimeException('Download file not found');
@@ -60,14 +66,24 @@ public function __construct($file, $content_type, $inline = false, $filename = n
6066
$this->inline = $inline;
6167
$this->filename = $filename;
6268
$this->x_type = $x_type;
69+
$this->x_accel_redirect_to = $x_accel_redirect_to;
6370
}
6471

6572
public function createPsrResponse()
6673
{
74+
$response = new Response();
6775
$filename = $this->filename ?: basename($this->file);
6876
$disposition = $this->inline ? 'inline' : 'attachment';
6977

70-
$response = new Response();
78+
if (!empty($this->x_accel_redirect_to)) {
79+
$response = $response
80+
->withHeader('Content-Disposition', $disposition . '; filename="' . $filename . '"')
81+
->withHeader('Content-Length', filesize($this->file))
82+
->withHeader('X-Accel-Redirect', $this->x_accel_redirect_to . $filename);
83+
84+
return $response;
85+
}
86+
7187
$stream = new Stream(fopen($this->file, 'rb'));
7288

7389
/** @var Response $response */

0 commit comments

Comments
 (0)