Skip to content

Commit a0fe369

Browse files
committed
Add option to Etag file downloads
1 parent 9c25d75 commit a0fe369

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/Response/FileDownloadResponse.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class FileDownloadResponse implements ResponseInterface
3737
*/
3838
private $filename;
3939

40+
/**
41+
* @var string|null
42+
*/
43+
private $cache_hash;
44+
4045
/**
4146
* @var string|null
4247
*/
@@ -46,10 +51,18 @@ class FileDownloadResponse implements ResponseInterface
4651
* @param string $file
4752
* @param string $content_type
4853
* @param bool $inline
49-
* @param string $filename
54+
* @param string|null $filename
55+
* @param string|null $cache_hash
5056
* @param string|null $x_type
5157
*/
52-
public function __construct($file, $content_type, $inline = false, $filename = null, $x_type = null)
58+
public function __construct(
59+
$file,
60+
$content_type,
61+
$inline = false,
62+
$filename = null,
63+
$cache_hash = null,
64+
$x_type = null
65+
)
5366
{
5467
if (!is_file($file)) {
5568
throw new RuntimeException('Download file not found');
@@ -59,6 +72,7 @@ public function __construct($file, $content_type, $inline = false, $filename = n
5972
$this->content_type = $content_type;
6073
$this->inline = $inline;
6174
$this->filename = $filename;
75+
$this->cache_hash = $cache_hash;
6276
$this->x_type = $x_type;
6377
}
6478

@@ -81,6 +95,15 @@ public function createPsrResponse()
8195
->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
8296
->withHeader('Pragma', 'public');
8397

98+
if ($this->cache_hash) {
99+
$response = $response->withHeader('Etag', '"' . trim($this->cache_hash, '"\'') . '"');
100+
} else {
101+
$response = $response
102+
->withHeader('Expires', '0')
103+
->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
104+
->withHeader('Pragma', 'public');
105+
}
106+
84107
if ($this->x_type) {
85108
$response = $response->withHeader('X-Type', $this->x_type);
86109
}

0 commit comments

Comments
 (0)