diff --git a/app/Http/Controllers/DownloadController.php b/app/Http/Controllers/DownloadController.php new file mode 100644 index 0000000..eceac4b --- /dev/null +++ b/app/Http/Controllers/DownloadController.php @@ -0,0 +1,109 @@ +boot($inscription, $format); + $this->validateFormat($format); + + if ($format === 'svg') { + return $this->downloadSVG($inscription); + } + + return $this->downloadImage($inscription, $format); + } + + private function validateFormat(string $format): void + { + if (! in_array($format, $this->allowed_formats, true)) { + abort(404); + } + } + + private function boot(Inscription $inscription, string $format): void + { + $this->file_name = $this->getFileName($inscription, $format); + $this->cloudflare_path = $this->getCloudflarePath($inscription, $format); + $this->download_file_name = $this->getDownloadFileName($inscription, $format); + } + + private function getFileName(Inscription $inscription, string $format): string + { + return Str::of($inscription->getInternalCollectionId()) + ->append('.'.$format) + ->toString(); + } + + private function getCloudflarePath(Inscription $inscription, string $format): string + { + return Str::of('images/pfp/') + ->append($format) + ->append('/') + ->append($inscription->getInternalCollectionId()) + ->append('.') + ->append($format) + ->toString(); + } + + private function getDownloadFileName(Inscription $inscription, string $format): string + { + return Str::of($inscription->name) + ->slug() + ->append('.'.$format) + ->toString(); + } + + private function downloadImage(Inscription $inscription, string $format) + { + if (! Storage::disk('cloudflare')->exists($this->cloudflare_path)) { + abort(404); + } + + $mimeType = Storage::disk('cloudflare')->mimeType($this->cloudflare_path); + + return Storage::disk('cloudflare') + ->download( + $this->cloudflare_path, + $this->download_file_name, + [ + 'Content-Type' => $mimeType, + ] + ); + } + + private function downloadSVG(Inscription $inscription) + { + if (! Storage::disk('ninjas')->exists($this->file_name)) { + abort(404); + } + + return Storage::disk('ninjas') + ->download( + $this->file_name, + $this->download_file_name, + [ + 'Content-Type' => 'image/svg+xml', + ] + ); + } +} diff --git a/app/Http/Controllers/DownloadSvgController.php b/app/Http/Controllers/DownloadSvgController.php deleted file mode 100644 index b0fd847..0000000 --- a/app/Http/Controllers/DownloadSvgController.php +++ /dev/null @@ -1,39 +0,0 @@ -file_name = Str::of($inscription->getInternalCollectionId()) - ->append('.svg') - ->toString(); - - $this->download_file_name = Str::of($inscription->name) - ->slug() - ->append('.svg') - ->toString(); - - if (! Storage::disk('ninjas')->exists($this->file_name)) { - abort(404); - } - - return Storage::disk('ninjas') - ->download( - $this->file_name, - $this->download_file_name, - [ - 'Content-Type' => 'image/svg+xml', - ] - ); - } -} diff --git a/resources/views/inscription.blade.php b/resources/views/inscription.blade.php index 394eb43..5f73623 100644 --- a/resources/views/inscription.blade.php +++ b/resources/views/inscription.blade.php @@ -55,12 +55,6 @@ class="mt-1 flex items-center text-orange-200 hover:text-white" 'mt-4 md:mt-0 w-full md:w-64 border border-2 border-orange-400 rounded-lg' )