some fix and file upload

This commit is contained in:
Kavalar 2023-05-24 01:22:03 +03:00
parent 57daa93406
commit a7501fee35
5 changed files with 173 additions and 0 deletions

2
.gitignore vendored
View File

@ -33,3 +33,5 @@ phpunit.phar
# vagrant runtime # vagrant runtime
/.vagrant /.vagrant
frontend/web/files

74
common/models/File.php Normal file
View File

@ -0,0 +1,74 @@
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "file".
*
* @property int $id
* @property string $name
* @property string $path
* @property string $url
* @property string $type
* @property string $mime-type
* @property string $created_at
* @property string $updated_at
* @property int $status
*/
class File extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
[
'class' => 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',
];
}
}

View File

@ -42,6 +42,10 @@ class TaskService
{ {
$modelTask = ProjectTask::findOne($task_params['task_id']); $modelTask = ProjectTask::findOne($task_params['task_id']);
if ($task_params['executor_id'] == 0){
$task_params['executor_id'] = null;
}
$modelTask->load($task_params, ''); $modelTask->load($task_params, '');
$modelTask->save(); $modelTask->save();

View File

@ -0,0 +1,35 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%file}}`.
*/
class m230523_205626_create_file_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->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}}');
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\File;
use yii\helpers\FileHelper;
use yii\web\ServerErrorHttpException;
use yii\web\UploadedFile;
class FileController extends ApiController
{
public function verbs(): array
{
return [
'upload' => ['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;
}
}