文件收集功能(3/5)
基本完成文件收集任务
This commit is contained in:
parent
d9573b122f
commit
73c08454c5
@ -10,6 +10,8 @@ use Yii;
|
|||||||
use yii\web\Controller;
|
use yii\web\Controller;
|
||||||
use yii\web\NotFoundHttpException;
|
use yii\web\NotFoundHttpException;
|
||||||
use yii\filters\VerbFilter;
|
use yii\filters\VerbFilter;
|
||||||
|
use yii\web\ServerErrorHttpException;
|
||||||
|
use yii\web\UploadedFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CollectionController implements the CRUD actions for CollectionTasks model.
|
* CollectionController implements the CRUD actions for CollectionTasks model.
|
||||||
@ -28,6 +30,7 @@ class CollectionController extends Controller
|
|||||||
'class' => VerbFilter::className(),
|
'class' => VerbFilter::className(),
|
||||||
'actions' => [
|
'actions' => [
|
||||||
'delete' => ['POST'],
|
'delete' => ['POST'],
|
||||||
|
'upload' => ['POST']
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
@ -120,7 +123,7 @@ class CollectionController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 外部访问接口,接受参数id:收集文件任务id,secret:访问密钥,CollectionTasks[secret]:访问密钥(另一种形式)
|
* 外部收集文件访问接口,接受参数id:收集文件任务id,secret:访问密钥,CollectionTasks[secret]:访问密钥(另一种形式)
|
||||||
*
|
*
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $secret
|
* @param $secret
|
||||||
@ -136,22 +139,76 @@ class CollectionController extends Controller
|
|||||||
$model = CollectionTasks::findOne(['id' => $id]);
|
$model = CollectionTasks::findOne(['id' => $id]);
|
||||||
if ($model === null) {
|
if ($model === null) {
|
||||||
throw new NotFoundHttpException('请求的文件收集任务不存在');
|
throw new NotFoundHttpException('请求的文件收集任务不存在');
|
||||||
|
} elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path)) {
|
||||||
|
throw new NotFoundHttpException('收集任务的目标路径不存在');
|
||||||
} elseif ($secret === null) {
|
} elseif ($secret === null) {
|
||||||
return $this->render('gateway', [
|
return $this->render('_gateway', [
|
||||||
'model' => new CollectionTasks(),
|
'model' => new CollectionTasks(),
|
||||||
]);
|
]);
|
||||||
} elseif ($model->secret !== $secret) {
|
} elseif ($model->secret !== $secret) {
|
||||||
Yii::$app->session->setFlash('error', '拒绝访问,凭证不正确');
|
Yii::$app->session->setFlash('error', '拒绝访问,凭证不正确');
|
||||||
return $this->render('gateway', [
|
return $this->render('_gateway', [
|
||||||
'model' => new CollectionTasks(),
|
'model' => new CollectionTasks(),
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
$model2 = new CollectionUploaded();
|
$model2 = new CollectionUploaded();
|
||||||
$model2->subfolder_name = Uuid::uuid4()->toString();
|
do {
|
||||||
|
$model2->subfolder_name = Uuid::uuid4()->toString();
|
||||||
|
$path = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path . '/' . $model2->subfolder_name;
|
||||||
|
} while (file_exists($path));
|
||||||
return $this->render('access', [
|
return $this->render('access', [
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
'model2' => $model2,
|
'model2' => $model2,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
* @throws ServerErrorHttpException
|
||||||
|
*/
|
||||||
|
public function actionUpload()
|
||||||
|
{
|
||||||
|
$request = Yii::$app->request;
|
||||||
|
|
||||||
|
// 获取POST请求中的参数
|
||||||
|
$taskId = $request->post('CollectionTasks')['id'];
|
||||||
|
$subfolderName = $request->post('CollectionUploaded')['subfolder_name'];
|
||||||
|
|
||||||
|
// 获取发送POST请求的用户的IP地址
|
||||||
|
$uploaderIp = $request->userIP;
|
||||||
|
|
||||||
|
$task = CollectionTasks::findOne($taskId);
|
||||||
|
$userId = $task->user_id;
|
||||||
|
$folderPath = $task->folder_path;
|
||||||
|
|
||||||
|
// 创建一个新的CollectionUploaded模型实例,并设置其属性值
|
||||||
|
$model = new CollectionUploaded();
|
||||||
|
$model->task_id = $taskId;
|
||||||
|
$model->uploader_ip = $uploaderIp;
|
||||||
|
$model->uploaded_at = date('Y-m-d H:i:s'); // 设置上传时间为当前时间
|
||||||
|
$model->subfolder_name = $subfolderName;
|
||||||
|
if ($model->validate()) {
|
||||||
|
// 进行文件上传
|
||||||
|
$targetDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $userId . '/' . $folderPath . '/' . $subfolderName;
|
||||||
|
if (!is_dir($targetDirectory)) {
|
||||||
|
mkdir($targetDirectory, 0777, true);
|
||||||
|
}
|
||||||
|
$uploadedFiles = UploadedFile::getInstancesByName('files');
|
||||||
|
foreach ($uploadedFiles as $file) {
|
||||||
|
$filePath = $targetDirectory . '/' . $file->name;
|
||||||
|
if (!$file->saveAs($filePath)) {
|
||||||
|
throw new NotFoundHttpException('文件上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($model->save()) {
|
||||||
|
Yii::$app->session->setFlash('success', '上传完成');
|
||||||
|
return $this->redirect(['access', 'id' => $taskId, 'secret' => $task->secret]);
|
||||||
|
} else {
|
||||||
|
// 如果保存失败,可以抛出一个异常,或者渲染一个错误页面
|
||||||
|
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new NotFoundHttpException('上传失败,验证错误');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use yii\bootstrap5\ActiveForm;
|
||||||
use yii\helpers\Html;
|
use yii\helpers\Html;
|
||||||
|
use yii\helpers\Url;
|
||||||
|
|
||||||
/** @var yii\web\View $this */
|
/** @var yii\web\View $this */
|
||||||
/** @var app\models\CollectionTasks $model */
|
/** @var app\models\CollectionTasks $model */
|
||||||
@ -13,14 +15,29 @@ $this->params['breadcrumbs'][] = $this->title;
|
|||||||
|
|
||||||
<h1><?= Html::encode($this->title) ?></h1>
|
<h1><?= Html::encode($this->title) ?></h1>
|
||||||
<p>
|
<p>
|
||||||
收集任务ID: <?= Html::encode($model->id) ?>
|
这是文件收集任务<?= Html::encode($model->id) ?>,上传的文件将会保存到预先设定的位置。
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
访问密钥: <?= Html::encode($model->secret) ?>
|
上传者UUID: <?= Html::encode($model2->subfolder_name) ?>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<?php $form = ActiveForm::begin([
|
||||||
收集目标文件夹: <?= Html::encode($model2->subfolder_name) ?>
|
'action' => Url::to(['collection/upload']),
|
||||||
</p>
|
'method' => 'post',
|
||||||
|
'options' => ['enctype' => 'multipart/form-data']
|
||||||
|
]); ?>
|
||||||
|
|
||||||
|
<label for="uploader">要上传的文件:</label>
|
||||||
|
<input type="file" multiple name="files[]" id="uploader">
|
||||||
|
|
||||||
|
<?= $form->field($model, 'id')->hiddenInput()->label(false) ?>
|
||||||
|
|
||||||
|
<?= $form->field($model2, 'subfolder_name')->hiddenInput()->label(false) ?>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<?= Html::submitButton('上传文件', ['class' => 'btn btn-primary']) ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php ActiveForm::end(); ?>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user