53 lines
1.2 KiB
PHP
53 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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
|
|
{
|
|
public function attachable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
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/');
|
|
}
|
|
}
|