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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user