prefixGallery = env("GALLERY_PREFIX", "/gallery/"); } /** * Создает галерею, ее имя и будет являться ключем для галереи * @return JsonResponse * * @OA\Post( * path="/api/admin/gallery/create", * tags={"admin"}, * operationId="createGallery", * description="Create new gallery", * @OA\Response( * response=400, * description="Invalid input" * ), * @OA\Response( * response=200, * description="successful operation", * @OA\JsonContent(ref="#/components/schemas/Gallery"), * ), * ) */ public function createGallery(): JsonResponse { $folderName = md5(uniqid()); Storage::makeDirectory($this->prefixGallery . $folderName); $model = new \App\DTO\Gallery(); $model->name = $folderName; return response()->json($model); } /** * Удаляет галерею вместе с содержимым * @param $id string * @return Response * * @OA\Delete( * path="/api/admin/gallery/remove/{id}", * tags={"admin"}, * operationId="removeGallery", * description="Remove gallery with all photo", * @OA\Parameter( * name="id", * in="path", * required=true, * description="Идентификатор галереи", * @OA\Schema( * type="string" * ) * ), * @OA\Response( * response=400, * description="Invalid input" * ), * @OA\Response( * response=200, * description="Success delete" * ), * ) */ public function removeGallery(string $id): Response { $result = Storage::deleteDirectory($this->prefixGallery . $id); return response('',$result ? 200 : 400); } /** * Возвращает список галерей * @return JsonResponse * * @OA\Get( * path="/api/admin/gallery/list", * tags={"admin"}, * operationId="getGalleryList", * description="Get list gallery", * @OA\Response( * response=400, * description="Invalid input" * ), * @OA\Response( * response=200, * description="Список существующих галерей, без файлов.", * @OA\JsonContent( * type="array", * @OA\Items(ref="#/components/schemas/Gallery") * ) * ), * ) */ public function listGallery(): JsonResponse { $list = Storage::allDirectories($this->prefixGallery); $list=array_map(static function ($item){ $gal=new \App\DTO\Gallery(); $gal->name=explode('/',$item)[1]; return $gal; },$list); return response()->json($list); } /** * Загрузка файлов * @param Request $request * @param string $id * @return JsonResponse * * @OA\Post( * path="/api/admin/file/upload/{galleryId}", * tags={"admin"}, * operationId="uploadFile", * description="Upload file in gallery", * @OA\Parameter( * name="galleryId", * in="path", * required=true, * description="Идентификатор галереи", * @OA\Schema( * type="string" * ) * ), * @OA\RequestBody( * required=true, * @OA\MediaType( * mediaType="multipart/form-data", * @OA\Schema( * @OA\Property( * description="Photo file", * property="file", * type="string", * format="binary", * ), * required={"file"} * ) * ) * ), * @OA\Response( * response=400, * description="Invalid input" * ), * @OA\Response( * response=200, * description="successful operation", * @OA\JsonContent( * @OA\Property( * property="fileId", * type="string", * example="1bc29b36f623ba82aaf6724fd3b16718", * description="Идентификатор фотографии" * ), * @OA\Property( * property="galleryId", * type="string", * example="1bc29b36f623ba82aaf6724fd3b16718", * description="Идентификатор галереи" * ), * ), * ), * ) */ public function uploadFile(Request $request, string $id): JsonResponse { $file = $request->file('file'); $list = explode(',', env("ALLOW_MIME_TYPES", "jpeg,png")); $list = array_map(static function ($item) { return 'image/' . $item; }, $list); if (empty($file) || !in_array($file->getMimeType(), $list, true)) { abort(400, 'Only jpg/png files!'); } $fileName = implode(".",[md5(uniqid()),$file->extension()]); $result = Storage::put($this->prefixGallery . $id . "/".$fileName,$file->getContent() ); if ($result) { return response()->json(['galleryId' => $id, 'fileId' => $fileName]); } else { abort(400, 'File not found or not write'); } } /** * Удаляет фотографию из галереи * @param string $galleryId * @param string $fileId * @return Response * * @OA\Delete( * path="/api/admin/file/remove/{galleryId}/{fileId} ", * tags={"admin"}, * operationId="removeFile", * description="Remove file in gallery", * @OA\Parameter( * name="galleryId", * in="path", * required=true, * description="Идентификатор галереи", * @OA\Schema( * type="string" * ) * ), * @OA\Parameter( * name="fileId", * in="path", * required=true, * description="Идентификатор фотографии", * @OA\Schema( * type="string" * ) * ), * @OA\Response( * response=400, * description="Invalid input" * ), * @OA\Response( * response=200, * description="Success delete" * ), * ) */ public function removeFile(string $galleryId, string $fileId): Response { $result = Storage::delete($this->prefixGallery . $galleryId . "/" . $fileId); return response('',$result ? 200 : 400); } }