File manager - Edit - /var/www/html/pcadigital/api/app/Http/Services/CommentService.php
Back
<?php namespace App\Http\Services; use App\Classes\BaseServiceResponse; use App\Http\Requests\CommentRequest; use App\Http\Resources\CommentResource; use App\Models\Comment; use App\Models\CommentImage; use App\Models\Occurrence; use Carbon\Carbon; use Exception; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Storage; class CommentService extends BaseService { private NotificationService $notificationService; public function __construct() { $this->notificationService = new NotificationService(); } public function index(): BaseServiceResponse { $comments = Comment::all(); return $this->success(CommentResource::collection($comments)); } public function show(Comment $comment): BaseServiceResponse { return $this->success(new CommentResource($comment)); } public function store(CommentRequest $request): BaseServiceResponse { try { $data = $request->all(); $occurrence = Occurrence::find($data['occurrence_id']); $comment = new Comment(); $comment->description = $data['description']; $comment->created_by = $data['created_by']; $comment->occurrence()->associate($occurrence); $comment->save(); foreach ($data['images'] as $fileBase64) { $isBase64 = isBase64($fileBase64); if ($isBase64) { $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) { throw new Exception('Image cannot be size more than 10mb.', JsonResponse::HTTP_NOT_ACCEPTABLE); } $types = ['jpg', 'png', 'jpeg']; $extension = explode('/', explode(':', substr($fileBase64, 0, strpos($fileBase64, ';')))[1])[1]; if (in_array($extension, $types) == false) { throw new Exception('Only jpg, jpeg and png images can be saved.', JsonResponse::HTTP_NOT_ACCEPTABLE); } $replace = substr($fileBase64, 0, strpos($fileBase64, ',') + 1); $file = str_replace($replace, '', $fileBase64); $file = str_replace(' ', '+', $file); $fileName = Carbon::now()->format('d-m-Y') . '_' . uniqid() . '.' . $extension; Storage::disk('public')->put($fileName, base64_decode($file), ['visibility' => 'public', 'directory_visibility' => 'public']); $commentImage = new CommentImage(); $commentImage->image_url = "/storage/" . $fileName; $commentImage->name = $fileName; $commentImage->comment()->associate($comment); $commentImage->save(); } else { return $this->error( [ 'message' => __('validation.validation_error'), 'data' => 'Image is not a base64', ], JsonResponse::HTTP_NOT_FOUND ); } } } catch (\Exception $e) { return $this->error( [ 'message' => __('validation.validation_error'), 'data' => 'Error when trying to generate comment', ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } $this->notificationService->createWhenHasNewComment($occurrence); return $this->success(new CommentResource($comment)); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings