mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Use temporaryUrls for protected files if the storage driver in use supports them
This commit is contained in:
parent
85c7ba34c9
commit
aea4857eba
@ -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,8 +93,14 @@ class Files extends Controller
|
||||
*/
|
||||
public static function getDownloadUrl($file)
|
||||
{
|
||||
$url = static::getTemporaryUrl($file);
|
||||
|
||||
if (!empty($url)) {
|
||||
return $url;
|
||||
} else {
|
||||
return Backend::url('backend/files/get/' . self::getUniqueCode($file));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for downloading a system file.
|
||||
@ -71,8 +112,14 @@ class Files extends Controller
|
||||
*/
|
||||
public static function getThumbUrl($file, $width, $height, $options)
|
||||
{
|
||||
$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'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a unique code used for masking the file identifier.
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user