shopit-back/app/Domains/Attachable/Models/Attachment.php

54 lines
1.2 KiB
PHP

<?php
namespace App\Domains\Attachable\Models;
use App\Domains\Attachable\Enums\AttachmentType;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
#[Fillable([
'key',
'path',
'filename',
'type',
'mime_type',
'extension',
'size',
])]
class Attachment extends Model
{
use HasFactory;
protected $table = 'attachments';
protected static function booted(): void
{
static::creating(function (self $attachment): void {
if (! $attachment->key) {
$attachment->key = (string) Str::uuid();
}
});
}
protected function casts(): array
{
return [
'type' => AttachmentType::class,
'size' => 'integer',
];
}
/**
* Get the pre-signed temporary S3 URL for this attachment.
*/
public function getTemporaryUrl(int $expiresInMinutes = 10): string
{
return \Illuminate\Support\Facades\Storage::disk('s3')->temporaryUrl(
$this->path,
now()->addMinutes($expiresInMinutes)
);
}
}