Файловый менеджер - Редактировать - /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Controllers/Api/V1/User/HomePageApiController.php
Назад
<?php namespace App\Http\Controllers\Api\V1\User; use App\Http\Controllers\Controller; use App\Http\Controllers\Traits\MediaUploadingTrait; use App\Http\Resources\User\BestSellingResource; use App\Http\Resources\User\CartResource; use App\Http\Resources\User\CategoryResource; use App\Http\Resources\User\CategoryTreeResource; use App\Http\Resources\User\PartnerResource; use App\Http\Resources\User\WishlistResource; use App\Models\Cart; use App\Models\Category; use App\Models\OrderItems; use App\Models\OurPartner; use App\Models\Product; use App\Models\SiteSetting; use App\Models\Wishlist; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Process\Process; use Symfony\Component\Process\Exception\ProcessFailedException; class HomePageApiController extends Controller { use MediaUploadingTrait; public function index() { $data['user'] = $this->getUser(); $data['site_info'] = $this->getSiteSettings(); $data['categories'] = $this->getCategoryTree(); $data['navbar_categories'] = $this->getNavbarCategories(); $data['partner_slider'] = $this->getPartnerSlider(); $data['wishlist'] = $this->getWishlist(); $data['cart'] = $this->getCart(); $data['best_selling_products'] = $this->getBestSellingProducts(); $data['top_ranking_categories'] = $this->getTopRankingCategories(); return response() ->json(['data' => $data], Response::HTTP_OK); } public function getTopRankingAndBestSelling() { $data['best_selling_products'] = $this->getBestSellingProducts(); $data['top_ranking_categories'] = $this->getTopRankingCategories(); return response() ->json($data, Response::HTTP_OK); } public function getSiteSettings() { $site_email = SiteSetting::where('key', 'Site Email')->first(); $site_setting['site_email'] = $site_email->value ?? ''; $site_phone = SiteSetting::where('key', 'Site Phone')->first(); $site_setting['site_phone'] = $site_phone->value ?? ''; $site_phone_2 = SiteSetting::where('key', 'Site Phone 2')->first(); $site_setting['site_phone_2'] = $site_phone_2->value ?? ''; $site_fax = SiteSetting::where('key', 'Fax')->first(); $site_setting['site_fax'] = $site_fax->value ?? ''; $site_location = SiteSetting::where('key', 'Location')->first(); $site_setting['site_location'] = $site_location->value ?? ''; $site_location_2 = SiteSetting::where('key', 'Location 2')->first(); $site_setting['site_location_2'] = $site_location_2->value ?? ''; $cancellation_charges = SiteSetting::where('key', 'Cancel Order Fee')->first(); $site_setting['cancellation_charges'] = $cancellation_charges->value ?? ''; return $site_setting; } public function getCategoryTree() { $categories = Category::whereNull('parent_id') ->with('categoryTree') ->active() ->orderBy('name') ->get(); CategoryTreeResource::withoutWrapping(); return CategoryTreeResource::collection($categories); } public function getPartnerSlider() { $partners = OurPartner::active()->get(); PartnerResource::withoutWrapping(); return PartnerResource::collection($partners); } public function getNavbarCategories() { $categories = Category::where('show_in_navigation', 1) ->active() ->orderBy('name') ->get(); CategoryTreeResource::withoutWrapping(); return CategoryTreeResource::collection($categories); } public function getWishlist() { if (!auth('sanctum')->check()) return []; $wishlist = Wishlist::where('user_id', auth('sanctum')->id())->get(); WishlistResource::withoutWrapping(); return WishlistResource::collection($wishlist); } public function getCart() { if (!auth('sanctum')->check()) return []; $cart_items = Cart::where('user_id', auth('sanctum')->id())->get(); CartResource::withoutWrapping(); return CartResource::collection($cart_items); } public function getUser() { if (!auth('sanctum')->check()) return []; $user = auth('sanctum')->user(); return [ 'id' => $user->id, 'name' => $user->name, 'email' => $user->email, 'phone_number' => $user->phone_number, 'server_timezone' => date_default_timezone_get(), 'status' => $user->status, 'profile_image' => $user->profile_image ? [ "url" => $user->profile_image->url, "thumbnail" => $user->profile_image->thumbnail, "preview" => $user->profile_image->preview, ] : null, ]; } public function getBestSellingProducts() { $products = Product::whereIn('id', $this->getIdArray()) ->orderBy('name') ->limit(6) ->get(); BestSellingResource::withoutWrapping(); return BestSellingResource::collection($products); } public function getTopRankingCategories() { $categories = Category::whereIn('id', $this->getIdArray())->orderBy('name') ->withCount('products') ->limit(8) ->get(); CategoryResource::withoutWrapping(); return CategoryResource::collection($categories); } public function getIdArray() { $results = OrderItems::select('products.*', DB::raw('COUNT(order_items.product_id) AS total')) ->join('products', 'order_items.product_id', '=', 'products.id') ->groupBy('product_id') ->orderBy('total', 'desc') ->get(); return $results->pluck('category_id')->toArray(); } public function convertImage(Request $request) { $request->validate([ 'product_id' => 'required|exists:products,id', 'image' => 'required', ]); if ($request->hasFile('image')) { $request->validate([ 'image' => 'mimes:jpg,jpeg,png', ]); $mime_type = $request->file('image')->getClientOriginalExtension(); $image = $request->file('image')->getRealPath(); } else if (preg_match('/^data:image\/(jpeg|png|jpg);base64,/', $request->input('image'), $matches)) { $mime_type = $matches[1]; // the request contains a base64 encoded image $image = $request->input('image'); } else { return \response()->json([ 'message' => 'Invalid image. Image should be jpg, jpeg, png or base64.', 'errors' => ["error" => ["Image should be image/base64 of type jpg, jpeg, png."]] ]) ->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); } $product = Product::find($request->get('product_id')); if ($product->engraving == 0){ return response() ->json([ 'message' => "Engraving not supported.", 'errors' => ["error" => ["This product does not support engraving."]] ], Response::HTTP_UNPROCESSABLE_ENTITY); } $file_name = 'template-'.time().'.'.$mime_type; try { if ($product->color_engraving == 0){ \Image::make($image)->greyscale()->save(public_path('images/converted/'.$file_name)); $stored_image = public_path('images/converted/'.$file_name); switch($mime_type) { case "png": $im = imagecreatefrompng($stored_image); imagefilter($im, IMG_FILTER_NEGATE); imagepng($im, public_path('images/converted/'.$file_name)); break; case "jpg": case "jpeg": $im = imagecreatefromjpeg($stored_image); imagefilter($im, IMG_FILTER_NEGATE); imagejpeg($im, public_path('images/converted/'.$file_name)); break; } }else{ \Image::make($image)->save(public_path('images/converted/'.$file_name)); } return response()->json([ 'data' => asset('images/converted/'.$file_name), ], Response::HTTP_OK); }catch (\Exception $exception){ \Log::info('Image Conversion'); \Log::info($exception->getMessage()); return response() ->json([ 'message' => "Oops! Something went wrong.", 'errors' => ["error" => ["Something went wrong. Can not convert image."]] ], Response::HTTP_UNPROCESSABLE_ENTITY); } } public function convertImagePython(Request $request) { $request->validate([ 'product_id' => 'required|exists:products,id', 'image' => 'required|mimes:jpg,jpeg,png|max:5120', ]); $image = $request->file('image')->getRealPath(); $file_name = $request->file('image')->getClientOriginalName(); $product = Product::find($request->get('product_id')); if ($product->engraving == 0){ return response() ->json([ 'message' => "Engraving not supported.", 'errors' => ["error" => ["This product does not support engraving."]] ], Response::HTTP_UNPROCESSABLE_ENTITY); } try { if ($product->color_engraving == 0){ //Run python script here $process = new Process(['python3', app_path('scripts/convert_image.py'), request()->image]); $process->run(); if (!$process->isSuccessful()) { \Log::info('Python failed to execute "'.app_path('scripts/convert_image.py').'"'); \Log::info(new ProcessFailedException($process)); return response() ->json([ 'message' => "Oops! Something went wrong.", 'errors' => ["error" => ["Something went wrong. Can not convert image."]] ], Response::HTTP_UNPROCESSABLE_ENTITY); } else { $file_name = $process->getOutput(); $image = '/var/www/phase2/web/assets/customization/converted/'.$file_name; } } if (request()->has('file_url') && request()->get('file_url')) { $b64 = '/assets/customization/converted/'.$file_name; } else { $b64 = "data:image/jpeg;base64,".base64_encode(file_get_contents($image)); } return response()->json([ 'data' => $b64, ], Response::HTTP_OK); }catch (\Exception $exception){ \Log::info($exception->getMessage()); return response() ->json([ 'message' => "Oops! Something went wrong.", 'errors' => ["error" => ["Something went wrong. Can not convert image."]] ], Response::HTTP_UNPROCESSABLE_ENTITY); } } public function convertPdfToBase64(Request $request) { $request->validate([ 'url' => 'required', ]); $path = explode('variation/', $request->get("url")); $file = \Storage::disk('variation')->get($path[1]); $encoded_svg = "data:image/svg+xml;base64,".base64_encode($file); return response()->json([ 'data' => $encoded_svg, ], Response::HTTP_OK); } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка