feat(attachments): ensure 'key' is set as UUID on attachment creation and update tests for key validation
This commit is contained in:
@@ -12,6 +12,7 @@ use Illuminate\Support\Str;
|
||||
#[Fillable([
|
||||
'attachable_type',
|
||||
'attachable_id',
|
||||
'key',
|
||||
'path',
|
||||
'filename',
|
||||
'type',
|
||||
@@ -28,7 +29,9 @@ class Attachment extends Model
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (self $attachment): void {
|
||||
$attachment->key = (string) Str::uuid();
|
||||
if (! $attachment->key) {
|
||||
$attachment->key = (string) Str::uuid();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Domains\Attachable\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class AttachmentService
|
||||
@@ -32,10 +33,11 @@ class AttachmentService
|
||||
throw new AttachmentStorageException('The attachment filename cannot be empty.');
|
||||
}
|
||||
|
||||
$key = (string) Str::uuid();
|
||||
$storedPath = Storage::disk('s3')->putFileAs(
|
||||
$this->directoryFromPath($normalizedPath),
|
||||
$file,
|
||||
$filename,
|
||||
$key,
|
||||
);
|
||||
|
||||
if (! is_string($storedPath) || $storedPath === '') {
|
||||
@@ -45,6 +47,7 @@ class AttachmentService
|
||||
try {
|
||||
/** @var Attachment $attachment */
|
||||
$attachment = $attachable->attachments()->create([
|
||||
'key' => $key,
|
||||
'path' => $storedPath,
|
||||
'filename' => $filename,
|
||||
'type' => $type,
|
||||
|
||||
Reference in New Issue
Block a user