-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from ninja5pizza/download-images
Add option to download JPG/PNG/WEBP files
- Loading branch information
Showing
4 changed files
with
136 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters