Added `temporaryUrlTTL` configuration option, switched is_a() to instanceof

This commit is contained in:
Luke Towers 2019-05-31 00:53:27 -06:00
parent aea4857eba
commit ef4f1e49ee
2 changed files with 14 additions and 7 deletions

View File

@ -252,14 +252,20 @@ return [
| folder - a folder prefix for storing all generated files inside. | folder - a folder prefix for storing all generated files inside.
| path - the public path relative to the application base URL, | path - the public path relative to the application base URL,
| or you can specify a full URL path. | or you can specify a full URL path.
|
| Optionally, you can specify how long temporary URLs to protected files
| in cloud storage (ex. AWS, RackSpace) are valid for by setting
| temporaryUrlTTL to a value in seconds to define a validity period. This
| is only used for the 'uploads' config when using a supported cloud disk
*/ */
'storage' => [ 'storage' => [
'uploads' => [ 'uploads' => [
'disk' => 'local', 'disk' => 'local',
'folder' => 'uploads', 'folder' => 'uploads',
'path' => '/storage/app/uploads', 'path' => '/storage/app/uploads',
'temporaryUrlTTL' => 3600,
], ],
'media' => [ 'media' => [

View File

@ -2,6 +2,7 @@
use View; use View;
use Cache; use Cache;
use Config;
use Backend; use Backend;
use Response; use Response;
use System\Models\File as FileModel; use System\Models\File as FileModel;
@ -65,18 +66,18 @@ class Files extends Controller
$url = null; $url = null;
$disk = $file->getDisk(); $disk = $file->getDisk();
$adapter = $disk->getAdapter(); $adapter = $disk->getAdapter();
if (is_a($adapter, 'League\Flysystem\Cached\CachedAdapter')) { if ($adapter instanceof \League\Flysystem\Cached\CachedAdapter) {
$adapter = $adapter->getAdapter(); $adapter = $adapter->getAdapter();
} }
if (is_a($adapter, 'League\Flysystem\AwsS3v3\AwsS3Adapter') || if (($adapter instanceof \League\Flysystem\AwsS3v3\AwsS3Adapter) ||
is_a($adapter, 'League\Flysystem\Rackspace\RackspaceAdapter') || ($adapter instanceof \League\Flysystem\Rackspace\RackspaceAdapter) ||
method_exists($adapter, 'getTemporaryUrl') method_exists($adapter, 'getTemporaryUrl')
) { ) {
if (empty($path)) { if (empty($path)) {
$path = $file->getDiskPath(); $path = $file->getDiskPath();
} }
$expires = now()->addMinutes(60); $expires = now()->addSeconds(Config::get('cms.storage.uploads.temporaryUrlTTL', 3600));
$url = Cache::remember('backend.file:' . $path, $expires, function () use ($disk, $path, $expires) { $url = Cache::remember('backend.file:' . $path, $expires, function () use ($disk, $path, $expires) {
return $disk->temporaryUrl($path, $expires); return $disk->temporaryUrl($path, $expires);