file and some fix
This commit is contained in:
@ -4,7 +4,9 @@ namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\classes\Debug;
|
||||
use common\models\File;
|
||||
use frontend\modules\api\models\FileEntity;
|
||||
use yii\helpers\FileHelper;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\web\ServerErrorHttpException;
|
||||
use yii\web\UploadedFile;
|
||||
|
||||
@ -14,9 +16,47 @@ class FileController extends ApiController
|
||||
{
|
||||
return [
|
||||
'upload' => ['post'],
|
||||
'attach' => ['post'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public function actionUpload()
|
||||
{
|
||||
$uploads = UploadedFile::getInstancesByName("uploadFile");
|
||||
@ -39,11 +79,11 @@ class FileController extends ApiController
|
||||
$fileModel = new File();
|
||||
$fileModel->name = $file->name;
|
||||
$fileModel->path = $path;
|
||||
$fileModel->url = "/files/". $md5[0] . $md5[1] . "/" . $md5[2] . $md5[3] . "/" . $md5 . '.' . $file->getExtension();
|
||||
$fileModel->url = "/files/" . $md5[0] . $md5[1] . "/" . $md5[2] . $md5[3] . "/" . $md5 . '.' . $file->getExtension();
|
||||
$fileModel->type = $file->getExtension();
|
||||
$fileModel->{'mime-type'} = $file->type;
|
||||
|
||||
if (!$fileModel->validate()){
|
||||
if (!$fileModel->validate()) {
|
||||
throw new ServerErrorHttpException(json_encode($fileModel->errors));
|
||||
}
|
||||
|
||||
@ -55,4 +95,80 @@ class FileController extends ApiController
|
||||
return $savedFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public function actionAttach()
|
||||
{
|
||||
$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, '');
|
||||
|
||||
if(!$fileEntity->validate()){
|
||||
throw new ServerErrorHttpException(json_encode($fileEntity->errors));
|
||||
}
|
||||
|
||||
$fileEntity->save();
|
||||
|
||||
return $fileEntity;
|
||||
}
|
||||
|
||||
}
|
@ -141,7 +141,7 @@ class ProjectController extends ApiController
|
||||
}
|
||||
if (!empty($user_id)) {
|
||||
$projectIdList = ProjectUser::find()->where(['user_id' => $user_id])->select('project_id')->column();
|
||||
$query = Project::find()->where(['IN', 'id', $projectIdList])->andWhere(['status' => Project::STATUS_OTHER])->orWhere(['owner_id' => $user_id]);
|
||||
$query = Project::find()->where(['IN', 'id', $projectIdList])->orWhere(['owner_id' => $user_id, 'status' => Project::STATUS_OTHER]);
|
||||
} else {
|
||||
$query = Project::find();
|
||||
}
|
||||
|
66
frontend/modules/api/models/File.php
Normal file
66
frontend/modules/api/models/File.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\models;
|
||||
|
||||
/**
|
||||
*
|
||||
* @OA\Schema(
|
||||
* schema="File",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="int",
|
||||
* example=1,
|
||||
* description="Идентификатор файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="name",
|
||||
* type="string",
|
||||
* example="image.png",
|
||||
* description="Имя файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="path",
|
||||
* type="string",
|
||||
* example="/frontend/web/files/c3/8b/c38bf511e3082e3021bed1572c2c8144.png",
|
||||
* description="Путь к файлу"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="url",
|
||||
* type="string",
|
||||
* example="/files/c3/8b/c38bf511e3082e3021bed1572c2c8144.png",
|
||||
* description="URL файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="type",
|
||||
* type="string",
|
||||
* example="png",
|
||||
* description="Расширение файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="mime-type",
|
||||
* type="string",
|
||||
* example="image/png",
|
||||
* description="Mime-type файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="status",
|
||||
* type="integer",
|
||||
* example=1,
|
||||
* description="Статус файла"
|
||||
* ),
|
||||
*)
|
||||
*
|
||||
*
|
||||
* @OA\Schema(
|
||||
* schema="FileExample",
|
||||
* type="array",
|
||||
* @OA\Items(
|
||||
* ref="#/components/schemas/File",
|
||||
* ),
|
||||
*)
|
||||
*
|
||||
*/
|
||||
class File extends \common\models\File
|
||||
{
|
||||
|
||||
}
|
45
frontend/modules/api/models/FileEntity.php
Normal file
45
frontend/modules/api/models/FileEntity.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\models;
|
||||
|
||||
/**
|
||||
*
|
||||
* @OA\Schema(
|
||||
* schema="FileEntity",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="int",
|
||||
* example=1,
|
||||
* description="Идентификатор файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="file_id",
|
||||
* type="integer",
|
||||
* example=232,
|
||||
* description="Идентификатор файла"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="entity_type",
|
||||
* type="integer",
|
||||
* example=2,
|
||||
* description="Идентификатор типа сущности"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="entity_id",
|
||||
* type="integer",
|
||||
* example=24,
|
||||
* description="Идентификатор сущности"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="status",
|
||||
* type="integer",
|
||||
* example=1,
|
||||
* description="Статус прикрепления"
|
||||
* ),
|
||||
*)
|
||||
*
|
||||
*/
|
||||
class FileEntity extends \common\models\FileEntity
|
||||
{
|
||||
|
||||
}
|
Reference in New Issue
Block a user