Phase 3 asset storage management

This commit is contained in:
Codex
2026-07-10 13:13:25 -05:00
parent a72d2aa164
commit 084b945e75
13 changed files with 225 additions and 20 deletions
+30
View File
@@ -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/');
}
}