Phase 3 asset storage management
This commit is contained in:
@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
#[Fillable(['user_id', 'kind', 'disk', 'path', 'original_name', 'mime_type', 'size', 'caption'])]
|
||||
class Attachment extends Model
|
||||
@@ -19,4 +20,33 @@ class Attachment extends Model
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function publicUrl(): string
|
||||
{
|
||||
if ($this->disk === 'public') {
|
||||
return asset('storage/'.$this->path);
|
||||
}
|
||||
|
||||
return Storage::disk($this->disk)->url($this->path);
|
||||
}
|
||||
|
||||
public function formattedSize(): string
|
||||
{
|
||||
$bytes = max(0, (int) $this->size);
|
||||
|
||||
if ($bytes >= 1048576) {
|
||||
return round($bytes / 1048576, 1).' MB';
|
||||
}
|
||||
|
||||
if ($bytes >= 1024) {
|
||||
return round($bytes / 1024, 1).' KB';
|
||||
}
|
||||
|
||||
return $bytes.' B';
|
||||
}
|
||||
|
||||
public function isImage(): bool
|
||||
{
|
||||
return str_starts_with((string) $this->mime_type, 'image/');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user