From a7501fee3561869652bc072501b2b43c37950301 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 24 May 2023 01:22:03 +0300 Subject: [PATCH] some fix and file upload --- .gitignore | 2 + common/models/File.php | 74 +++++++++++++++++++ common/services/TaskService.php | 4 + .../m230523_205626_create_file_table.php | 35 +++++++++ .../api/controllers/FileController.php | 58 +++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 common/models/File.php create mode 100644 console/migrations/m230523_205626_create_file_table.php create mode 100644 frontend/modules/api/controllers/FileController.php diff --git a/.gitignore b/.gitignore index b096df8..36a5fb0 100755 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ phpunit.phar # vagrant runtime /.vagrant + +frontend/web/files diff --git a/common/models/File.php b/common/models/File.php new file mode 100644 index 0000000..4aa34c6 --- /dev/null +++ b/common/models/File.php @@ -0,0 +1,74 @@ + TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new Expression('NOW()'), + ], + ]; + } + + /** + * {@inheritdoc} + */ + public static function tableName() + { + return 'file'; + } + + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['path'], 'string'], + [['created_at', 'updated_at'], 'safe'], + [['status'], 'integer'], + [['name', 'type', 'mime-type', 'url'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'name' => 'Name', + 'path' => 'Path', + 'url' => 'URL', + 'type' => 'Type', + 'mime-type' => 'Mime Type', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + 'status' => 'Status', + ]; + } +} diff --git a/common/services/TaskService.php b/common/services/TaskService.php index 88f0d33..ad2719b 100644 --- a/common/services/TaskService.php +++ b/common/services/TaskService.php @@ -42,6 +42,10 @@ class TaskService { $modelTask = ProjectTask::findOne($task_params['task_id']); + if ($task_params['executor_id'] == 0){ + $task_params['executor_id'] = null; + } + $modelTask->load($task_params, ''); $modelTask->save(); diff --git a/console/migrations/m230523_205626_create_file_table.php b/console/migrations/m230523_205626_create_file_table.php new file mode 100644 index 0000000..ba74446 --- /dev/null +++ b/console/migrations/m230523_205626_create_file_table.php @@ -0,0 +1,35 @@ +createTable('{{%file}}', [ + 'id' => $this->primaryKey(), + 'name' => $this->string(255), + 'path' => $this->text(), + 'url' => $this->string(255), + 'type' => $this->string(255), + 'mime-type' => $this->string(255), + 'created_at' => $this->dateTime(), + 'updated_at' => $this->dateTime(), + 'status' => $this->integer()->defaultValue(1), + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('{{%file}}'); + } +} diff --git a/frontend/modules/api/controllers/FileController.php b/frontend/modules/api/controllers/FileController.php new file mode 100644 index 0000000..ed8a123 --- /dev/null +++ b/frontend/modules/api/controllers/FileController.php @@ -0,0 +1,58 @@ + ['post'], + ]; + } + + 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; + $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()){ + throw new ServerErrorHttpException(json_encode($fileModel->errors)); + } + + $fileModel->save(); + $savedFiles[] = $fileModel; + } + } + + return $savedFiles; + } + +} \ No newline at end of file