fix(s3): serve files without cache if dummy cache handler + add http referer header to redirect

This commit is contained in:
Yassine Doghri 2023-06-14 17:20:14 +00:00
parent 4c1a3e5015
commit 30db9f0667
1 changed files with 16 additions and 6 deletions

View File

@ -188,6 +188,8 @@ class S3 implements FileManagerInterface
public function serve(string $key): Response public function serve(string $key): Response
{ {
$cacheName = 'object_presigned_uri_' . str_replace('/', '-', $key) . '_' . $this->config->s3['bucket']; $cacheName = 'object_presigned_uri_' . str_replace('/', '-', $key) . '_' . $this->config->s3['bucket'];
/** @var string $found */
if (! $found = cache($cacheName)) { if (! $found = cache($cacheName)) {
try { try {
$cmd = $this->s3->getCommand('GetObject', [ $cmd = $this->s3->getCommand('GetObject', [
@ -206,14 +208,20 @@ class S3 implements FileManagerInterface
->save($cacheName, $found, DAY); ->save($cacheName, $found, DAY);
} }
$lastModifiedTimestamp = cache()
->getMetaData($cacheName)['mtime'];
$lastModified = new DateTime();
$lastModified->setTimestamp($lastModifiedTimestamp);
/** @var Response $response */ /** @var Response $response */
$response = service('response'); $response = service('response');
if (cache()->getMetaData($cacheName) === null) {
return $response->setHeader('HTTP_REFERER', previous_url())
->redirect($found);
}
$lastModifiedTimestamp = cache()
->getMetaData($cacheName)['mtime'];
$lastModified = new DateTime();
$lastModified->setTimestamp($lastModifiedTimestamp);
// Remove Cache-Control header before redefining it // Remove Cache-Control header before redefining it
header_remove('Cache-Control'); header_remove('Cache-Control');
@ -222,7 +230,9 @@ class S3 implements FileManagerInterface
'last-modified' => $lastModified->format(DATE_RFC7231), 'last-modified' => $lastModified->format(DATE_RFC7231),
'etag' => md5($cacheName), 'etag' => md5($cacheName),
'public' => true, 'public' => true,
])->redirect($found); ])
->setHeader('HTTP_REFERER', previous_url())
->redirect($found);
} }
private function prefixKey(string $key): string private function prefixKey(string $key): string