Use temporaryUrls for protected files if the storage driver in use supports them

This commit is contained in:
Luke Towers 2019-05-30 17:47:01 -06:00
parent 85c7ba34c9
commit aea4857eba
2 changed files with 55 additions and 9 deletions

View File

@ -1,6 +1,7 @@
<?php namespace Backend\Controllers;
use View;
use Cache;
use Backend;
use Response;
use System\Models\File as FileModel;
@ -51,6 +52,40 @@ class Files extends Controller
return Response::make(View::make('backend::404'), 404);
}
/**
* Attempt to return a redirect to a temporary URL to the asset instead of streaming the asset - if supported
*
* @param System|Models\File $file
* @param string|null $path Optional, defaults to the getDiskPath() of the file
* @return string|null
*/
protected static function getTemporaryUrl($file, $path = null)
{
// Get the disk and adapter used
$url = null;
$disk = $file->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'];
}
}
/**

View File

@ -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'));
}
}