AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StoreLogoController.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Ai\AiException;
  4. use App\Models\Store;
  5. use App\Storefront\Logos\StoreLogos;
  6. use Illuminate\Http\RedirectResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Validation\Rule;
  9. use Inertia\Inertia;
  10. use RuntimeException;
  11. /**
  12. * The Logo box in the Design tab: upload a logo, pick a ready design, or have Claude draw some.
  13. * The logo belongs to the store, so a live store shows it right away (no "Publish changes" needed).
  14. */
  15. class StoreLogoController extends Controller
  16. {
  17. public function upload(Request $request, string $slug, StoreLogos $logos): RedirectResponse
  18. {
  19. $store = $this->ownedStore($request, $slug);
  20. $request->validate([
  21. 'logo' => [
  22. 'required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:2048',
  23. 'dimensions:min_width='.StoreLogos::MIN_SIDE.',min_height='.StoreLogos::MIN_SIDE.',max_width=6000,max_height=6000',
  24. ],
  25. ], [
  26. 'logo.max' => __('The logo must be 2 MB or smaller.'),
  27. 'logo.mimes' => __('Use a PNG, JPG or WebP picture. PNG keeps a see-through background.'),
  28. 'logo.dimensions' => __('Use a picture at least :size pixels on each side.', ['size' => StoreLogos::MIN_SIDE]),
  29. ]);
  30. try {
  31. $logos->upload($store, $request->file('logo'));
  32. } catch (RuntimeException) {
  33. return back()->withErrors(['logo' => __('That picture could not be read. Try saving it as PNG.')]);
  34. }
  35. Inertia::flash('toast', ['type' => 'success', 'message' => __('Logo uploaded. It shows on your store now.')]);
  36. return back();
  37. }
  38. /**
  39. * Use a ready design, or one of the logos Claude drew.
  40. */
  41. public function choose(Request $request, string $slug, StoreLogos $logos): RedirectResponse
  42. {
  43. $store = $this->ownedStore($request, $slug);
  44. $validated = $request->validate([
  45. 'design' => ['required_without:drawn', 'nullable', Rule::in(StoreLogos::DESIGNS)],
  46. 'drawn' => ['required_without:design', 'nullable', 'integer', 'min:0'],
  47. ]);
  48. if (filled($validated['design'] ?? null)) {
  49. $logos->useDesign($store, $validated['design']);
  50. } elseif (! $logos->useDrawn($store, (int) $validated['drawn'])) {
  51. return back()->withErrors(['logo' => __('That logo is no longer available.')]);
  52. }
  53. Inertia::flash('toast', ['type' => 'success', 'message' => __('Logo saved. It shows on your store now.')]);
  54. return back();
  55. }
  56. /**
  57. * Ask Claude for new logos. Each store has a lifetime number of tries.
  58. */
  59. public function draw(Request $request, string $slug, StoreLogos $logos): RedirectResponse
  60. {
  61. $store = $this->ownedStore($request, $slug);
  62. try {
  63. $made = $logos->draw($store);
  64. } catch (AiException $e) {
  65. report($e);
  66. return back()->withErrors(['logo' => __('The AI could not draw logos just now. This try was not counted. Please try again.')]);
  67. }
  68. if ($made === null) {
  69. return back()->withErrors(['logo' => __('You have used all :count AI logo tries for this store. You can still pick any logo drawn before, a ready design, or upload your own.', ['count' => $logos->tryLimit()])]);
  70. }
  71. Inertia::flash('toast', ['type' => 'success', 'message' => __('The AI drew :count new logos. Pick one below.', ['count' => $made])]);
  72. return back();
  73. }
  74. public function destroy(Request $request, string $slug, StoreLogos $logos): RedirectResponse
  75. {
  76. $logos->remove($this->ownedStore($request, $slug));
  77. Inertia::flash('toast', ['type' => 'success', 'message' => __('Logo removed. Your store shows its name instead.')]);
  78. return back();
  79. }
  80. private function ownedStore(Request $request, string $slug): Store
  81. {
  82. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  83. }
  84. }