2023-05-24 01:22:03 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
use common\classes\Debug;
|
|
|
|
use common\models\File;
|
2023-06-21 17:22:35 +03:00
|
|
|
use frontend\modules\api\models\FileEntity;
|
2023-06-30 18:13:40 +03:00
|
|
|
use frontend\modules\api\models\Timer;
|
2023-05-24 01:22:03 +03:00
|
|
|
use yii\helpers\FileHelper;
|
2023-06-21 17:22:35 +03:00
|
|
|
use yii\web\NotFoundHttpException;
|
2023-05-24 01:22:03 +03:00
|
|
|
use yii\web\ServerErrorHttpException;
|
|
|
|
use yii\web\UploadedFile;
|
|
|
|
|
|
|
|
class FileController extends ApiController
|
|
|
|
{
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'upload' => ['post'],
|
2023-06-21 17:22:35 +03:00
|
|
|
'attach' => ['post'],
|
2023-06-30 18:13:40 +03:00
|
|
|
'detach' => ['delete'],
|
|
|
|
'get-by-entity' => ['get'],
|
2023-05-24 01:22:03 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:22:35 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @OA\Post(path="/file/upload",
|
|
|
|
* summary="Загрузить файл",
|
|
|
|
* description="Метод для загрузки файлов",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"File"},
|
|
|
|
*
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="multipart/form-data",
|
|
|
|
* @OA\Schema(
|
|
|
|
* required={"uploadFile"},
|
|
|
|
* @OA\Property(
|
|
|
|
* property="uploadFile",
|
|
|
|
* type="file",
|
|
|
|
* description="Файл который необходимо загрузить",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект Файла",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/FileExample"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
* @throws \yii\base\Exception
|
|
|
|
*/
|
2023-05-24 01:22:03 +03:00
|
|
|
public function actionUpload()
|
|
|
|
{
|
|
|
|
$uploads = UploadedFile::getInstancesByName("uploadFile");
|
|
|
|
if (empty($uploads)) {
|
|
|
|
throw new ServerErrorHttpException("Must upload at least 1 file in uploadFile form-data POST");
|
|
|
|
}
|
|
|
|
|
|
|
|
$savedFiles = [];
|
|
|
|
foreach ($uploads as $file) {
|
|
|
|
$md5 = md5($file->baseName);
|
|
|
|
$path = \Yii::getAlias("@frontend/web/files/") . $md5[0] . $md5[1] . "/" . $md5[2] . $md5[3] . "/";
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
FileHelper::createDirectory($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$path .= $md5 . '.' . $file->getExtension();
|
|
|
|
$file->saveAs($path);
|
|
|
|
|
|
|
|
if (!$file->hasError) {
|
|
|
|
$fileModel = new File();
|
|
|
|
$fileModel->name = $file->name;
|
|
|
|
$fileModel->path = $path;
|
2023-06-21 17:22:35 +03:00
|
|
|
$fileModel->url = "/files/" . $md5[0] . $md5[1] . "/" . $md5[2] . $md5[3] . "/" . $md5 . '.' . $file->getExtension();
|
2023-05-24 01:22:03 +03:00
|
|
|
$fileModel->type = $file->getExtension();
|
|
|
|
$fileModel->{'mime-type'} = $file->type;
|
|
|
|
|
2023-06-21 17:22:35 +03:00
|
|
|
if (!$fileModel->validate()) {
|
2023-05-24 01:22:03 +03:00
|
|
|
throw new ServerErrorHttpException(json_encode($fileModel->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileModel->save();
|
|
|
|
$savedFiles[] = $fileModel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $savedFiles;
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:22:35 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @OA\Post(path="/file/attach",
|
|
|
|
* summary="Прикрепить файл",
|
|
|
|
* description="Метод для прикрепления файлов",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"File"},
|
|
|
|
*
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="multipart/form-data",
|
|
|
|
* @OA\Schema(
|
|
|
|
* required={"file_id", "entity_type", "entity_id"},
|
|
|
|
* @OA\Property(
|
|
|
|
* property="file_id",
|
|
|
|
* type="intager",
|
|
|
|
* example=232,
|
|
|
|
* description="Идентификатор файла",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="entity_type",
|
|
|
|
* type="intager",
|
|
|
|
* example=2,
|
|
|
|
* description="Идентификатор типа сущности",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="entity_id",
|
|
|
|
* type="intager",
|
|
|
|
* example=234,
|
|
|
|
* description="Идентификатор сущности",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="status",
|
|
|
|
* type="intager",
|
|
|
|
* example=1,
|
|
|
|
* description="Статус",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект прикрепления",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/FileEntity"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return FileEntity
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
2023-06-30 18:13:40 +03:00
|
|
|
public function actionAttach(): FileEntity
|
2023-06-21 17:22:35 +03:00
|
|
|
{
|
|
|
|
$request = \Yii::$app->request->post();
|
|
|
|
$file = File::findOne($request['file_id']);
|
|
|
|
if (!$file) {
|
|
|
|
throw new NotFoundHttpException('File bot found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileEntity = new FileEntity();
|
|
|
|
$fileEntity->load($request, '');
|
|
|
|
|
2023-06-30 18:13:40 +03:00
|
|
|
if (!$fileEntity->validate()) {
|
2023-06-21 17:22:35 +03:00
|
|
|
throw new ServerErrorHttpException(json_encode($fileEntity->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileEntity->save();
|
|
|
|
|
|
|
|
return $fileEntity;
|
|
|
|
}
|
|
|
|
|
2023-06-30 18:13:40 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @OA\Delete (path="/file/detach",
|
|
|
|
* summary="Открепить файл",
|
|
|
|
* description="Метод для открепления файлов",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"File"},
|
|
|
|
*
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\MediaType(
|
2023-09-30 23:37:02 +03:00
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
2023-06-30 18:13:40 +03:00
|
|
|
* @OA\Schema(
|
|
|
|
* required={"file_id", "entity_type", "entity_id"},
|
|
|
|
* @OA\Property(
|
|
|
|
* property="file_id",
|
|
|
|
* type="intager",
|
|
|
|
* example=232,
|
|
|
|
* description="Идентификатор файла",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="entity_type",
|
|
|
|
* type="intager",
|
|
|
|
* example=2,
|
|
|
|
* description="Идентификатор типа сущности",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="entity_id",
|
|
|
|
* type="intager",
|
|
|
|
* example=234,
|
|
|
|
* description="Идентификатор сущности",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="status",
|
|
|
|
* type="intager",
|
|
|
|
* example=1,
|
|
|
|
* description="Статус",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект прикрепления",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/FileEntity"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return FileEntity
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
|
|
|
public function actionDetach(): FileEntity
|
|
|
|
{
|
2023-09-30 23:37:02 +03:00
|
|
|
$request = \Yii::$app->request->getBodyParams();
|
2023-06-30 18:13:40 +03:00
|
|
|
$file = File::findOne($request['file_id']);
|
|
|
|
if (!$file) {
|
|
|
|
throw new NotFoundHttpException('File bot found');
|
|
|
|
}
|
|
|
|
|
2023-09-30 23:37:02 +03:00
|
|
|
$fileEntity = FileEntity::findOne(['file_id' => $request['file_id'], 'entity_type' => $request['entity_type'], 'entity_id' => $request['entity_id']]);
|
|
|
|
//$fileEntity->load($request, '');
|
2023-06-30 18:13:40 +03:00
|
|
|
$fileEntity->status = FileEntity::STATUS_DISABLE;
|
|
|
|
|
|
|
|
if (!$fileEntity->validate()) {
|
|
|
|
throw new ServerErrorHttpException(json_encode($fileEntity->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileEntity->save();
|
|
|
|
|
|
|
|
return $fileEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @OA\Get(path="/file/get-by-entity",
|
|
|
|
* summary="Получить файл по идентификатору сущности",
|
|
|
|
* description="Метод для получения файла по идентификатору сущности.",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"File"},
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="entity_id",
|
|
|
|
* in="query",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* default=null
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="entity_type",
|
|
|
|
* in="query",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* default=null
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает массив объектов Файлов",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/FileEntity"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param int $entity_type
|
|
|
|
* @param int $entity_id
|
|
|
|
* @return array
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetByEntity(int $entity_type, int $entity_id): array
|
|
|
|
{
|
|
|
|
$model = FileEntity::find()->where(['entity_type' => $entity_type, 'entity_id' => $entity_id, 'status' => FileEntity::STATUS_ACTIVE])->all();
|
|
|
|
|
|
|
|
if (!$model) {
|
|
|
|
throw new NotFoundHttpException('The file not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-05-24 01:22:03 +03:00
|
|
|
}
|