-
Notifications
You must be signed in to change notification settings - Fork 0
#6 - listing image uploads backend #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
693bba1
f9f4403
d6405ce
431f486
20a3b45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Illuminate\Http\UploadedFile; | ||
| use Otoszroto\Models\Auction; | ||
|
|
||
| class AddImageToAuctionAction | ||
| { | ||
| public function execute(Auction $auction, UploadedFile $uploadedFile): bool | ||
| { | ||
| $stored = $uploadedFile->storeAs("", $auction->id . ".png", "auctionImage"); | ||
|
|
||
| return $stored !== false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class GetAuctionImageAction | ||
| { | ||
| public function execute(int $auctionId): ?string | ||
| { | ||
| $filename = $auctionId . ".png"; | ||
|
|
||
| if (Storage::disk("auctionImage")->exists($filename)) { | ||
| return Storage::disk("auctionImage")->get($filename); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class GetDefaultAuctionImageAction | ||
| { | ||
| public function execute(): ?string | ||
| { | ||
| $filename = "default.png"; | ||
|
|
||
| if (Storage::disk("auctionImage")->exists($filename)) { | ||
| return Storage::disk("auctionImage")->get($filename); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Helpers; | ||
|
|
||
| use Identicon\Identicon; | ||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class IdenticonHelper | ||
| { | ||
| private Identicon $identicon; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->identicon = new Identicon(); | ||
| } | ||
|
|
||
| public static function url(string|int $name): string | ||
| { | ||
| return url("/auctions/{$name}/image"); | ||
| } | ||
|
|
||
| public function create(string|int $filename, string $data): string | ||
| { | ||
| $image = $this->identicon->getImageData($data, 300); | ||
|
|
||
| return $this->save($filename, $image); | ||
| } | ||
|
|
||
| private function save(string|int $name, string $imageData): string | ||
| { | ||
| $path = $name . ".png"; | ||
|
|
||
| Storage::disk("avatars")->put($path, $imageData); | ||
|
|
||
| return Storage::disk("avatars")->url($path); | ||
| } | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,12 @@ | |
| use Illuminate\Http\Request; | ||
| use Inertia\Inertia; | ||
| use Inertia\Response; | ||
| use Otoszroto\Actions\Auction\AddImageToAuctionAction; | ||
| use Otoszroto\Actions\Auction\CancelAuctionAction; | ||
| use Otoszroto\Actions\Auction\CreateAuctionAction; | ||
| use Otoszroto\Actions\Auction\FinishAuctionAction; | ||
| use Otoszroto\Actions\Auction\GetAuctionImageAction; | ||
| use Otoszroto\Actions\Auction\GetDefaultAuctionImageAction; | ||
| use Otoszroto\Actions\Auction\UpdateAuctionAction; | ||
| use Otoszroto\Enums\AuctionState; | ||
| use Otoszroto\Helpers\SortHelper; | ||
|
|
@@ -41,11 +44,17 @@ public function edit(Auction $auction): Response | |
| return Inertia::render("Auction/EditAuction", ["auction" => $auction]); | ||
| } | ||
|
|
||
| public function store(CreateAuctionRequest $request, CreateAuctionAction $createAuctionAction): RedirectResponse | ||
| public function store(CreateAuctionRequest $request, CreateAuctionAction $createAuctionAction, AddImageToAuctionAction $addImageToAuctionAction): RedirectResponse | ||
| { | ||
| $user = $request->user(); | ||
| $validated = $request->validated(); | ||
| $createAuctionAction->execute($user, $validated); | ||
| $auction = $createAuctionAction->execute($user, $validated); | ||
|
||
|
|
||
| $photo = $validated["photo"]; | ||
|
|
||
| if ($photo) { | ||
| $addImageToAuctionAction->execute($auction, $photo); | ||
| } | ||
|
|
||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return redirect()->route("auctions.create")->with(["message" => "Aukcja została utworzona."]); | ||
| } | ||
|
|
@@ -110,6 +119,16 @@ public function index(SortHelper $sorter, Request $request): Response | |
| ]); | ||
| } | ||
|
|
||
| public function getImage(int $id, GetAuctionImageAction $getAuctionImageAction, GetDefaultAuctionImageAction $getDefaultAuctionImageAction): \Illuminate\Http\Response | ||
| { | ||
| $image = $getAuctionImageAction->execute($id); | ||
| $default = $getDefaultAuctionImageAction->execute(); | ||
|
|
||
| return response($image ?? $default) | ||
| ->header("Content-Type", "image/png") | ||
| ->header("Cache-Control", "max-age=31536000, public"); | ||
| } | ||
|
|
||
| private function filterCategory(Builder $query, Request $request): Builder | ||
| { | ||
| $category_id = $request->query("category", null); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,8 @@ | |||||
| "laravel/framework": "^12.20.0", | ||||||
| "laravel/sanctum": "^4.1.2", | ||||||
| "laravel/tinker": "^2.10.1", | ||||||
| "spatie/laravel-permission": "^6.24" | ||||||
| "spatie/laravel-permission": "^6.24", | ||||||
| "yzalis/identicon": "^2.0" | ||||||
|
||||||
| "yzalis/identicon": "^2.0" | |
| "bitverse/identicon": "^2.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The disk configuration references a disk named "auctionImage" in the filesystem config, but the IdenticonHelper class incorrectly uses the disk name "avatars". This will cause a runtime error when trying to save identicon images.