From aea4857ebaaa132499033014309bd5a6032dd66d Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Thu, 30 May 2019 17:47:01 -0600 Subject: [PATCH] Use temporaryUrls for protected files if the storage driver in use supports them --- modules/backend/controllers/Files.php | 51 +++++++++++++++++++++++++-- modules/system/models/File.php | 13 ++++--- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/modules/backend/controllers/Files.php b/modules/backend/controllers/Files.php index fce5fa7ed..220b26aab 100644 --- a/modules/backend/controllers/Files.php +++ b/modules/backend/controllers/Files.php @@ -1,6 +1,7 @@ getDisk(); + $adapter = $disk->getAdapter(); + if (is_a($adapter, 'League\Flysystem\Cached\CachedAdapter')) { + $adapter = $adapter->getAdapter(); + } + + if (is_a($adapter, 'League\Flysystem\AwsS3v3\AwsS3Adapter') || + is_a($adapter, 'League\Flysystem\Rackspace\RackspaceAdapter') || + method_exists($adapter, 'getTemporaryUrl') + ) { + if (empty($path)) { + $path = $file->getDiskPath(); + } + $expires = now()->addMinutes(60); + + $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 +93,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 +112,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')); } }