File manager - Edit - /var/www/html/spdc/api/app/Http/Services/GeologicHazardLayerService.php
Back
<?php namespace App\Http\Services; use App\Models\VehicleType; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Illuminate\Http\JsonResponse; use App\Http\Requests\VehicleTypeRequest; use App\Classes\BaseServiceResponse; use App\Http\Requests\GeologicHazardLayerRequest; use App\Http\Requests\GeologicHazardLayerUpdateRequest; use App\Http\Resources\GeologicHazardLayerResource; use App\Models\GeologicHazardLayer; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; class GeologicHazardLayerService extends BaseService { public function index(): BaseServiceResponse { $layers = GeologicHazardLayer::all(); return $this->response( GeologicHazardLayerResource::collection($layers), JsonResponse::HTTP_OK ); } public function store(GeologicHazardLayerRequest $request): BaseServiceResponse { try{ DB::beginTransaction(); $data = $request->all(); $fileBase64 = $data['file']; $size_in_bytes = (int) (strlen(rtrim($fileBase64, '=')) * 3 / 4); $size_in_kb = $size_in_bytes / 1024; $size_in_mb = $size_in_kb / 1024; if ($size_in_mb > 10) { DB::rollBack(); return $this->response( ["error" => 'File cannot be size more than 10mb'], JsonResponse::HTTP_BAD_REQUEST ); } $types = ['vnd.google-earth.kml+xml']; $extension = explode('/', explode(':', substr($fileBase64, 0, strpos($fileBase64, ';')))[1])[1]; if (in_array($extension, $types) == false) { DB::rollBack(); return $this->response( ["error" => 'Only kml files can be saved'], JsonResponse::HTTP_BAD_REQUEST ); } $fileName = $this->processAndSaveFile($fileBase64); $layer = new GeologicHazardLayer(); $layer->name = $data['name']; $layer->url = "/storage/" . $fileName; $layer->save(); DB::commit(); } catch(\Exception $e){ DB::rollBack(); return $this->response( null, JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } return $this->response( new GeologicHazardLayerResource($layer), JsonResponse::HTTP_CREATED ); } public function show(string $id): BaseServiceResponse { try{ $layer = GeologicHazardLayer::findOrFail($id)->first(); } catch(\Exception $e){ return $this->response( null, JsonResponse::HTTP_NOT_FOUND ); } return $this->response( new GeologicHazardLayerResource($layer), JsonResponse::HTTP_OK ); } public function update(GeologicHazardLayerUpdateRequest $request, string $id): BaseServiceResponse { try{ DB::beginTransaction(); $layer = GeologicHazardLayer::findOrFail($id)->first(); $data = $request->all(); if (isset($data['file'])) { Storage::disk('public')->delete($layer->url); $fileName = $this->processAndSaveFile($data['file']); $layer->url = "/storage/" . $fileName; } $layer->name = $data['name']; $layer->save(); DB::commit(); }catch(\Exception $e){ DB::rollBack(); return $this->response( null, JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } return $this->response( new GeologicHazardLayerResource($layer), JsonResponse::HTTP_OK ); } public function destroy(string $id): BaseServiceResponse { try{ $layer = GeologicHazardLayer::findOrFail($id)->first(); Storage::disk('public')->delete($layer->url); $layer->delete(); } catch(\Exception $e){ return $this->response( null, JsonResponse::HTTP_NOT_FOUND ); } return $this->response( null, JsonResponse::HTTP_NO_CONTENT ); } private function processAndSaveFile($fileBase64){ $replace = substr($fileBase64, 0, strpos($fileBase64, ',') + 1); $file = str_replace($replace, '', $fileBase64); $file = str_replace(' ', '+', $file); $fileName = Carbon::now()->format('d-m-Y') . '_' . uniqid() . '.' . 'kml'; Storage::disk('public')->put($fileName, base64_decode($file), ['visibility' => 'public', 'directory_visibility' => 'public']); return $fileName; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings