refactor(catalog): Complete catalog refactor to simplify its data model and its querying.
source commits: refactor/catalog
This commit is contained in:
@@ -66,6 +66,42 @@ class AttachmentService
|
||||
$attachment->delete();
|
||||
}
|
||||
|
||||
public function copy(Attachment $source, string $path): Attachment
|
||||
{
|
||||
$normalizedPath = $this->normalizeDirectory($path);
|
||||
|
||||
if ($normalizedPath === '') {
|
||||
throw new AttachmentStorageException('The attachment path cannot be empty.');
|
||||
}
|
||||
|
||||
$key = (string) Str::uuid();
|
||||
$storedPath = $normalizedPath.'/'.$this->buildStoredFilename($key, (string) $source->extension);
|
||||
$copied = Storage::disk('s3')->copy($source->path, $storedPath);
|
||||
|
||||
if (! $copied) {
|
||||
throw new AttachmentStorageException('No se pudo copiar el archivo en el disco s3.');
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var Attachment $attachment */
|
||||
$attachment = Attachment::query()->create([
|
||||
'key' => $key,
|
||||
'path' => $storedPath,
|
||||
'filename' => $source->filename,
|
||||
'type' => $source->type,
|
||||
'mime_type' => $source->mime_type,
|
||||
'extension' => $source->extension,
|
||||
'size' => $source->size,
|
||||
]);
|
||||
|
||||
return $attachment;
|
||||
} catch (Throwable $throwable) {
|
||||
Storage::disk('s3')->delete($storedPath);
|
||||
|
||||
throw $throwable;
|
||||
}
|
||||
}
|
||||
|
||||
protected function normalizeDirectory(string $path): string
|
||||
{
|
||||
return trim($path, '/');
|
||||
|
||||
Reference in New Issue
Block a user