You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.7 KiB
125 lines
3.7 KiB
2 years ago
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
use Illuminate\Http\JsonResponse;
|
||
|
use Illuminate\Support\Facades\Storage;
|
||
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
|
|
||
|
class Gallery extends Controller {
|
||
|
/**
|
||
|
* Префикс галерей на диске
|
||
|
* @var string
|
||
|
*/
|
||
|
private $prefixGallery = null;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->prefixGallery = env("GALLERY_PREFIX","/gallery/");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Возвращает список файлов в галереи
|
||
|
* @param string $id
|
||
|
* @return JsonResponse
|
||
|
*
|
||
|
* @OA\Get(
|
||
|
* path="/api/access/gallery/{id}",
|
||
|
* tags={"use"},
|
||
|
* operationId="getGallery",
|
||
|
* description="Get list files in gallery",
|
||
|
* @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="successful operation",
|
||
|
* @OA\JsonContent(ref="#/components/schemas/Gallery"),
|
||
|
* ),
|
||
|
* )
|
||
|
*/
|
||
|
public function listPhotoInGallery(string $id): JsonResponse
|
||
|
{
|
||
|
$this->checkFilePath($this->prefixGallery . $id,"Id is not valid");
|
||
|
$model = new \App\DTO\Gallery();
|
||
|
$model->name = $id;
|
||
|
$model->files = Storage::allFiles($this->prefixGallery . $id);
|
||
|
|
||
|
$model->files=array_map(function ($item){
|
||
|
$ar=explode('/',$item);
|
||
|
return $ar[count($ar)-1];
|
||
|
},$model->files);
|
||
|
return response()->json($model);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Отдает запрошенный файл из указанной галереи
|
||
|
* @param string $galleryId
|
||
|
* @param string $fileId
|
||
|
* @return StreamedResponse
|
||
|
*
|
||
|
* @OA\Get(
|
||
|
* path="/api/access/gallery/file/{galleryId}/{fileId}",
|
||
|
* tags={"use"},
|
||
|
* operationId="getPhoto",
|
||
|
* description="Get file from gallery",
|
||
|
* @OA\Parameter(
|
||
|
* name="galleryId",
|
||
|
* in="path",
|
||
|
* description="Идентификатор галереи",
|
||
|
* required=true,
|
||
|
* @OA\Schema(
|
||
|
* type="string"
|
||
|
* )
|
||
|
* ),
|
||
|
* @OA\Parameter(
|
||
|
* name="fileId",
|
||
|
* in="path",
|
||
|
* description="Идентификатор файла",
|
||
|
* required=true,
|
||
|
* @OA\Schema(
|
||
|
* type="string"
|
||
|
* )
|
||
|
* ),
|
||
|
* @OA\Response(
|
||
|
* response=400,
|
||
|
* description="Invalid input"
|
||
|
* ),
|
||
|
* @OA\Response(
|
||
|
* response=200,
|
||
|
* description="Download file"
|
||
|
* ),
|
||
|
* )
|
||
|
*/
|
||
|
public function getFile(string $galleryId, string $fileId): StreamedResponse
|
||
|
{
|
||
|
$this->checkFilePath($this->prefixGallery . $galleryId,"GalleryId is not valid");
|
||
|
$this->checkFilePath($this->prefixGallery . $galleryId."/".$fileId,"FileId is not valid");
|
||
|
return Storage::download($this->prefixGallery . $galleryId."/".$fileId,null,[]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Проверка на наличие файла, выброс ошибки в случае отсутствия
|
||
|
* @param string $path
|
||
|
* @param string $textError
|
||
|
* @return void
|
||
|
*/
|
||
|
private function checkFilePath(string $path, string $textError): void
|
||
|
{
|
||
|
$result=Storage::exists($path);
|
||
|
if (!$result) {
|
||
|
abort(400, $textError);
|
||
|
}
|
||
|
}
|
||
|
}
|