Improve support for protected files on S3 (#4390)

Credit to @fansaien. S3 supports generating temporaryUrls to files that don't exist yet so this adds a check to make sure temp URLs are only generated for valid files.
This commit is contained in:
fansaien 2019-06-21 00:24:08 -06:00 committed by Luke Towers
parent ba0da07c46
commit b1b9141dcc

View File

@ -77,11 +77,18 @@ class Files extends Controller
if (empty($path)) {
$path = $file->getDiskPath();
}
$expires = now()->addSeconds(Config::get('cms.storage.uploads.temporaryUrlTTL', 3600));
$url = Cache::remember('backend.file:' . $path, $expires, function () use ($disk, $path, $expires) {
return $disk->temporaryUrl($path, $expires);
});
// Check to see if the URL has already been generated
$pathKey = 'backend.file:' . $path;
$url = Cache::get($pathKey);
// The AWS S3 storage drivers will return a valid temporary URL even if the file does not exist
if (is_null($url) && $disk->exists($path)) {
$expires = now()->addSeconds(Config::get('cms.storage.uploads.temporaryUrlTTL', 3600));
$url = Cache::remember($pathKey, $expires, function () use ($disk, $path, $expires) {
return $disk->temporaryUrl($path, $expires);
});
}
}
return $url;