Skip to content

Commit

Permalink
Merge pull request #20 from ninja5pizza/download-images
Browse files Browse the repository at this point in the history
Add option to download JPG/PNG/WEBP files
  • Loading branch information
mvdnbrk authored Dec 16, 2024
2 parents a29b489 + 2dd9655 commit 8b804b6
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 55 deletions.
109 changes: 109 additions & 0 deletions app/Http/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Http\Controllers;

use App\Models\Inscription;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class DownloadController extends Controller
{
protected $file_name;

protected $cloudflare_path;

protected $download_file_name;

protected $allowed_formats = [
'png',
'jpg',
'svg',
'webp',
];

public function __invoke(Inscription $inscription, string $format)
{
$this->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',
]
);
}
}
39 changes: 0 additions & 39 deletions app/Http/Controllers/DownloadSvgController.php

This file was deleted.

35 changes: 23 additions & 12 deletions resources/views/inscription.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
<div class="flex absolute top-2 right-2 space-x-2">
<a
class="text-orange-200 hover:text-orange-100"
href="{{ route('download-svg', $inscription) }}"
>
<x-icon-download class="w-6 h-6 hover:scale-110 ease-out duration-300"/>
</a>
<a
class="text-orange-200 hover:text-orange-100"
href="{{ Str::of('https://ordiscan.com/content/')->append($inscription->inscription_id) }}"
Expand All @@ -70,12 +64,29 @@ class="text-orange-200 hover:text-orange-100"
</a>
</div>
</div>
<a
href="{{ route('download-svg', $inscription) }}"
class="mt-1 text-center rounded-md bg-white px-2.5 py-1.5 text-sm font-medium text-neutral-500 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-orange-200 hover:text-neutral-600"
>
Download SVG
</a>
<div class="mt-2 flex flex-col">
<label
class="block mb-1 px-2 text-sm font-medium text-orange-100"
>
Download this Pizza Ninja:
</label>
<div class="flex space-x-1">
@foreach (['jpg', 'png', 'webp'] as $format)
<a
class="mt-1 text-center rounded-md bg-white px-2.5 py-1.5 text-sm font-normal text-neutral-500 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-orange-200 hover:text-neutral-600"
href="{{ route('download-pfp', ['inscription' => $inscription, 'format' => $format]) }}"
>
{{ strtoupper($format) }}
</a>
@endforeach
<a
class="flex-grow mt-1 text-center rounded-md bg-white px-2.5 py-1.5 text-sm font-medium text-neutral-500 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-orange-200 hover:text-neutral-600"
href="{{ route('download-pfp', ['inscription' => $inscription, 'format' => 'svg']) }}"
>
SVG
</a>
</div>
</div>
@endif
<share-ninja data-initial-url="{{ Str::of('https://pizza.ninja/')->append($inscription->getInternalCollectionId()) }}"></share-ninja>
</div>
Expand Down
8 changes: 4 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use App\Http\Controllers\CollectionController;
use App\Http\Controllers\ContentController;
use App\Http\Controllers\DownloadSvgController;
use App\Http\Controllers\DownloadController;
use App\Http\Controllers\HomepageController;
use App\Http\Controllers\InscriptionController;
use App\Http\Controllers\PizzaNinjaController;
Expand Down Expand Up @@ -45,10 +45,10 @@
->name('inscription');

Route::get(
'/download/{inscription:inscription_id}',
DownloadSvgController::class
'/download/{inscription:inscription_id}/{format}',
DownloadController::class
)
->name('download-svg');
->name('download-pfp');

Route::post('/search', SearchController::class)->name('search');

Expand Down

0 comments on commit 8b804b6

Please sign in to comment.