'live-music.avif', '[Prueba] Festival de Sabores' => 'food-festival.avif', '[Prueba] Stand Up en el Centro' => 'stand-up.avif', ]; public function run(): void { $tenant = Tenant::query()->where('codigo', 'onticket')->first(); if ($tenant === null) { return; } $this->replaceAttachment( $tenant->headerBackgroundImage, 'onticket_header_background.avif', ); $carousel = $tenant->websiteExtras() ->whereHas( 'websiteTypeExtra', fn ($query) => $query->where('codigo', 'immersiveHeroCarousel') ) ->first(); $carouselIds = is_array($carousel?->config) ? array_values($carousel->config) : []; foreach (self::CAROUSEL_IMAGES as $index => $filename) { $attachmentId = $carouselIds[$index] ?? null; if (is_int($attachmentId)) { $this->replaceAttachment( Attachment::query()->find($attachmentId), 'onticket_tennant_hero_carousel/'.$filename, ); } } foreach (self::EVENT_IMAGES as $title => $filename) { $event = $tenant->events()->where('title', $title)->first(); $this->replaceAttachment( $event?->attachment, 'test-events/'.$filename, ); } } private function replaceAttachment(?Attachment $attachment, string $relativePath): void { if ($attachment === null) { return; } $sourcePath = public_path(self::IMAGE_DIRECTORY.'/'.$relativePath); if (! is_file($sourcePath)) { throw new RuntimeException("Image not found at path: {$sourcePath}"); } $contents = file_get_contents($sourcePath); if (! is_string($contents) || $contents === '') { throw new RuntimeException("Could not read image at path: {$sourcePath}"); } $filename = basename($relativePath); $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); $mimeType = match ($extension) { 'avif' => 'image/avif', 'svg' => 'image/svg+xml', default => 'image/png', }; $directory = trim(str_replace('\\', '/', dirname($attachment->path)), './'); $storedFilename = $attachment->key.($extension === '' ? '' : '.'.$extension); $newPath = $directory === '' ? $storedFilename : $directory.'/'.$storedFilename; $disk = Storage::disk('s3'); if ( $attachment->path === $newPath && $attachment->filename === $filename && $attachment->mime_type === $mimeType && $attachment->size === strlen($contents) && $disk->exists($newPath) ) { return; } $oldPath = $attachment->path; $oldContents = $disk->exists($oldPath) ? $disk->get($oldPath) : null; if (! $disk->put($newPath, $contents)) { throw new RuntimeException("Could not store optimized image at path: {$newPath}"); } try { DB::transaction(function () use ( $attachment, $extension, $filename, $mimeType, $newPath, $contents, ): void { $attachment->update([ 'path' => $newPath, 'filename' => $filename, 'type' => 'image', 'mime_type' => $mimeType, 'extension' => $extension, 'size' => strlen($contents), ]); }); } catch (Throwable $throwable) { if ($newPath === $oldPath && is_string($oldContents)) { $disk->put($oldPath, $oldContents); } else { $disk->delete($newPath); } throw $throwable; } if ($oldPath !== $newPath && $disk->exists($oldPath)) { $disk->delete($oldPath); } } }