From d9f19b42f1688b13d1c3b6a5b0da7051546dc3ff Mon Sep 17 00:00:00 2001 From: ncoronel Date: Tue, 22 Sep 2026 13:54:44 -0300 Subject: [PATCH] refactor(storage): centralize temporary URL generation --- app/Shared/Attachable/Models/Attachment.php | 8 ++++---- .../StorageTest/Services/S3TestService.php | 16 +++++++--------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/Shared/Attachable/Models/Attachment.php b/app/Shared/Attachable/Models/Attachment.php index df4ad7d..3710f7f 100644 --- a/app/Shared/Attachable/Models/Attachment.php +++ b/app/Shared/Attachable/Models/Attachment.php @@ -3,12 +3,12 @@ namespace App\Shared\Attachable\Models; use App\Shared\Attachable\Enums\AttachmentType; +use App\Shared\Storage\Services\TemporaryUrlService; use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; -use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; #[Fillable([ @@ -70,9 +70,9 @@ class Attachment extends Model */ public function getTemporaryUrl(int $expiresInMinutes = 10): string { - return Storage::disk('s3')->temporaryUrl( + return app(TemporaryUrlService::class)->generate( $this->path, - now()->addMinutes($expiresInMinutes) - ); + $expiresInMinutes, + )['temporary_url']; } } diff --git a/app/Shared/StorageTest/Services/S3TestService.php b/app/Shared/StorageTest/Services/S3TestService.php index 00e1c6a..0505314 100644 --- a/app/Shared/StorageTest/Services/S3TestService.php +++ b/app/Shared/StorageTest/Services/S3TestService.php @@ -2,6 +2,7 @@ namespace App\Shared\StorageTest\Services; +use App\Shared\Storage\Services\TemporaryUrlService; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; @@ -10,6 +11,10 @@ use RuntimeException; class S3TestService { + public function __construct( + protected TemporaryUrlService $temporaryUrlService, + ) {} + /** * @return array */ @@ -44,8 +49,7 @@ class S3TestService 'mime_type' => $file->getClientMimeType(), 'extension' => $file->extension(), 'size' => $file->getSize(), - 'temporary_url' => $disk->temporaryUrl($path, now()->addMinutes($expiresInMinutes)), - 'temporary_url_expires_at' => now()->addMinutes($expiresInMinutes)->toIso8601String(), + ...$this->temporaryUrlService->generate($path, $expiresInMinutes), ]; } @@ -58,16 +62,10 @@ class S3TestService 'disk' => 's3', 'key' => $path, 'path' => $path, - 'temporary_url' => $this->temporaryUrlForPath($path, $expiresInMinutes), - 'temporary_url_expires_at' => now()->addMinutes($expiresInMinutes)->toIso8601String(), + ...$this->temporaryUrlService->generate($path, $expiresInMinutes), ]; } - protected function temporaryUrlForPath(string $path, int $expiresInMinutes): string - { - return Storage::disk('s3')->temporaryUrl($path, now()->addMinutes($expiresInMinutes)); - } - protected function normalizeDirectory(?string $directory): string { $directory = trim((string) $directory, '/');