From 73c08454c5730ff108a929d696b76f0e1a724f4e Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Mon, 26 Feb 2024 19:36:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=94=B6=E9=9B=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD(3/5)=20=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=B6=E9=9B=86=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/CollectionController.php | 65 +++++++++++++++++-- .../collection/{gateway.php => _gateway.php} | 0 views/collection/access.php | 27 ++++++-- 3 files changed, 83 insertions(+), 9 deletions(-) rename views/collection/{gateway.php => _gateway.php} (100%) diff --git a/controllers/CollectionController.php b/controllers/CollectionController.php index 4ac87bf..08efee3 100644 --- a/controllers/CollectionController.php +++ b/controllers/CollectionController.php @@ -10,6 +10,8 @@ use Yii; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use yii\web\ServerErrorHttpException; +use yii\web\UploadedFile; /** * CollectionController implements the CRUD actions for CollectionTasks model. @@ -28,6 +30,7 @@ class CollectionController extends Controller 'class' => VerbFilter::className(), 'actions' => [ '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 $secret @@ -136,22 +139,76 @@ class CollectionController extends Controller $model = CollectionTasks::findOne(['id' => $id]); if ($model === null) { throw new NotFoundHttpException('请求的文件收集任务不存在'); + } elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path)) { + throw new NotFoundHttpException('收集任务的目标路径不存在'); } elseif ($secret === null) { - return $this->render('gateway', [ + return $this->render('_gateway', [ 'model' => new CollectionTasks(), ]); } elseif ($model->secret !== $secret) { Yii::$app->session->setFlash('error', '拒绝访问,凭证不正确'); - return $this->render('gateway', [ + return $this->render('_gateway', [ 'model' => new CollectionTasks(), ]); } else { $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', [ 'model' => $model, '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('上传失败,验证错误'); + } } diff --git a/views/collection/gateway.php b/views/collection/_gateway.php similarity index 100% rename from views/collection/gateway.php rename to views/collection/_gateway.php diff --git a/views/collection/access.php b/views/collection/access.php index 7647f40..52af2dd 100644 --- a/views/collection/access.php +++ b/views/collection/access.php @@ -1,6 +1,8 @@ params['breadcrumbs'][] = $this->title;

title) ?>

- 收集任务ID: id) ?> + 这是文件收集任务id) ?>,上传的文件将会保存到预先设定的位置。

- 访问密钥: secret) ?> + 上传者UUID: subfolder_name) ?>

-

- 收集目标文件夹: subfolder_name) ?> -

+ Url::to(['collection/upload']), + 'method' => 'post', + 'options' => ['enctype' => 'multipart/form-data'] + ]); ?> + + + + + field($model, 'id')->hiddenInput()->label(false) ?> + + field($model2, 'subfolder_name')->hiddenInput()->label(false) ?> + +
+ 'btn btn-primary']) ?> +
+ +