diff --git a/config/cms.php b/config/cms.php index 7d176c68f..75189b297 100644 --- a/config/cms.php +++ b/config/cms.php @@ -278,14 +278,20 @@ return [ | folder - a folder prefix for storing all generated files inside. | path - the public path relative to the application base URL, | 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' => [ 'uploads' => [ - 'disk' => 'local', - 'folder' => 'uploads', - 'path' => '/storage/app/uploads', + 'disk' => 'local', + 'folder' => 'uploads', + 'path' => '/storage/app/uploads', + 'temporaryUrlTTL' => 3600, ], 'media' => [ diff --git a/modules/backend/controllers/Files.php b/modules/backend/controllers/Files.php index fce5fa7ed..2ac62a722 100644 --- a/modules/backend/controllers/Files.php +++ b/modules/backend/controllers/Files.php @@ -1,6 +1,8 @@ getDisk(); + $adapter = $disk->getAdapter(); + if (class_exists('\League\Flysystem\Cached\CachedAdapter') && $adapter instanceof \League\Flysystem\Cached\CachedAdapter) { + $adapter = $adapter->getAdapter(); + } + + if ((class_exists('\League\Flysystem\AwsS3v3\AwsS3Adapter') && $adapter instanceof \League\Flysystem\AwsS3v3\AwsS3Adapter) || + (class_exists('\League\Flysystem\Rackspace\RackspaceAdapter') && $adapter instanceof \League\Flysystem\Rackspace\RackspaceAdapter) || + method_exists($adapter, 'getTemporaryUrl') + ) { + 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); + }); + } + + return $url; + } + /** * Returns the URL for downloading a system file. * @param $file System\Models\File @@ -58,7 +94,13 @@ class Files extends Controller */ public static function getDownloadUrl($file) { - return Backend::url('backend/files/get/' . self::getUniqueCode($file)); + $url = static::getTemporaryUrl($file); + + if (!empty($url)) { + return $url; + } else { + return Backend::url('backend/files/get/' . self::getUniqueCode($file)); + } } /** @@ -71,7 +113,13 @@ class Files extends Controller */ public static function getThumbUrl($file, $width, $height, $options) { - return Backend::url('backend/files/thumb/' . self::getUniqueCode($file)) . '/' . $width . '/' . $height . '/' . $options['mode'] . '/' . $options['extension']; + $url = static::getTemporaryUrl($file, $file->getDiskPath($file->getThumbFilename($width, $height, $options))); + + if (!empty($url)) { + return $url; + } else { + return Backend::url('backend/files/thumb/' . self::getUniqueCode($file)) . '/' . $width . '/' . $height . '/' . $options['mode'] . '/' . $options['extension']; + } } /** diff --git a/modules/system/models/File.php b/modules/system/models/File.php index 03ce1ca17..5b01a8f96 100644 --- a/modules/system/models/File.php +++ b/modules/system/models/File.php @@ -42,13 +42,13 @@ class File extends FileBase /** * {@inheritDoc} */ - public function getPath() + public function getPath($fileName = null) { $url = ''; if (!$this->isPublic() && class_exists(Files::class)) { $url = Files::getDownloadUrl($this); } else { - $url = parent::getPath(); + $url = parent::getPath($fileName); } return $url; @@ -103,12 +103,11 @@ class File extends FileBase } /** - * Copy the local file to Storage - * @return bool True on success, false on failure. + * Returns the storage disk the file is stored on + * @return FilesystemAdapter */ - protected function copyLocalToStorage($localPath, $storagePath) + public function getDisk() { - $disk = Storage::disk(Config::get('cms.storage.uploads.disk')); - return $disk->put($storagePath, FileHelper::get($localPath), $this->isPublic() ? 'public' : null); + return Storage::disk(Config::get('cms.storage.uploads.disk')); } }