Generate multiple avatar sizes for better performance loading and frugal traffic

This commit is contained in:
Čarodej
2021-11-03 16:28:14 +01:00
parent dc8ec5f20b
commit f139dbae08
30 changed files with 280 additions and 152 deletions

View File

@@ -32,7 +32,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
* @property string name
* @property string mimetype
* @property string author
* @property string author_id
* @property string created_at
* @property string updated_at
* @property string deleted_at
@@ -51,6 +50,7 @@ class File extends Model
];
protected $appends = [
'thumbnail',
'file_url',
];
@@ -58,10 +58,6 @@ class File extends Model
'metadata' => 'array',
];
protected $hidden = [
'author_id',
];
public array $sortable = [
'name',
'created_at',
@@ -92,23 +88,38 @@ class File extends Model
/**
* Format thumbnail url
*/
public function getThumbnailAttribute(): string | null
public function getThumbnailAttribute(): array | null
{
// Get thumbnail from external storage
if ($this->attributes['thumbnail'] && ! is_storage_driver(['local'])) {
return Storage::temporaryUrl("files/$this->user_id/{$this->attributes['thumbnail']}", now()->addHour());
}
$links = [];
// Get thumbnail from local storage
if ($this->attributes['thumbnail']) {
// Thumbnail route
$route = route('thumbnail', ['name' => $this->attributes['thumbnail']]);
// Generate thumbnail link for external storage service
if ($this->type === 'image' && ! is_storage_driver(['local'])) {
if ($this->public_access) {
return "$route/$this->public_access";
foreach (config('vuefilemanager.image_sizes') as $item) {
$filePath = "files/{$this->user_id}/{$item['name']}-{$this->basename}";
$links[$item['name']] = Storage::temporaryUrl($filePath, now()->addHour());
}
return $route;
return $links;
}
// Generate thumbnail link for local storage
if ($this->type === 'image') {
foreach (config('vuefilemanager.image_sizes') as $item) {
$route = route('thumbnail', ['name' => $item['name'] . '-' . $this->basename]);
if ($this->public_access) {
$route .= "/$this->public_access";
}
$links[$item['name']] = $route;
}
return $links;
}
return null;