yii2-netdisk/controllers/CollectionController.php

253 lines
8.9 KiB
PHP
Raw Normal View History

<?php
namespace app\controllers;
use app\models\CollectionTasks;
use app\models\CollectionSearch;
2024-02-26 17:33:53 +08:00
use app\models\CollectionUploaded;
use Ramsey\Uuid\Uuid;
2024-03-05 15:08:11 +08:00
use RuntimeException;
use Yii;
2024-03-05 15:08:11 +08:00
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
2024-03-05 15:08:11 +08:00
use yii\web\Request;
use yii\web\Response;
use yii\web\ServerErrorHttpException;
use yii\web\UploadedFile;
/**
* CollectionController implements the CRUD actions for CollectionTasks model.
*/
class CollectionController extends Controller
{
/**
* @inheritDoc
*/
2024-03-05 15:08:11 +08:00
public function behaviors(): array
{
return array_merge(
parent::behaviors(),
[
2024-03-05 15:08:11 +08:00
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view', 'create', 'delete'],
'roles' => ['user'],
],
[
'allow' => true,
'actions' => ['access', 'upload'],
'roles' => ['?', '@'], // everyone can access public share
]
],
],
'verbs' => [
2024-03-05 15:08:11 +08:00
'class' => VerbFilter::class,
'actions' => [
2024-03-05 15:08:11 +08:00
'index' => ['GET'],
'view' => ['GET'],
'create' => ['POST'],
'delete' => ['POST'],
2024-03-05 15:08:11 +08:00
'access' => ['GET'],
'upload' => ['POST'],
],
],
]
);
}
/**
* Lists all CollectionTasks models.
*
* @return string
*/
2024-03-05 15:08:11 +08:00
public function actionIndex(): string
{
$searchModel = new CollectionSearch();
2024-03-05 15:08:11 +08:00
if ($this->request instanceof Request) {
$dataProvider = $searchModel->search($this->request->queryParams);
$dataProvider->query->andWhere(['!=', 'status', 0]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} else {
throw new RuntimeException('Invalid request type');
}
}
/**
* Displays a single CollectionTasks model.
* @param int $id ID
* @return string
* @throws NotFoundHttpException if the model cannot be found
*/
2024-03-05 15:08:11 +08:00
public function actionView(int $id): string
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new CollectionTasks model.
* If creation is successful, the browser will be redirected to the 'view' page.
2024-03-05 15:08:11 +08:00
* @return string|Response
*/
2024-03-05 15:08:11 +08:00
public function actionCreate(): Response|string
{
$model = new CollectionTasks();
$model->scenario = 'create'; // 设置场景
2024-03-05 15:08:11 +08:00
if ($this->request instanceof Request && $this->request->isPost) {
if ($model->load($this->request->post())) {
$model->user_id = Yii::$app->user->id; // 手动设置user_id字段的值
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
}
}
2024-03-05 15:08:11 +08:00
//不允许get所以无视下面这个return
return $this->render('create', [
'model' => $model,
]);
}
/**
* Deletes an existing CollectionTasks model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param int $id ID
2024-03-05 15:08:11 +08:00
* @return Response
* @throws NotFoundHttpException if the model cannot be found
*/
2024-03-05 15:08:11 +08:00
public function actionDelete(int $id): Response
{
// 获取模型
$model = $this->findModel($id);
// 设置状态为禁用
$model->status = 0;
// 保存模型
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Task delete successfully.');
} else {
Yii::$app->session->setFlash('error', 'Failed to delete task.');
}
return $this->redirect(['index']);
}
/**
* Finds the CollectionTasks model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param int $id ID
* @return CollectionTasks the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
2024-03-05 15:08:11 +08:00
protected function findModel(int $id, bool $is_public = false): CollectionTasks
{
2024-03-05 15:08:11 +08:00
// Not Allow to access other user's collection manage page
// public collection can be accessed by anyone
if (($model = CollectionTasks::findOne(['id' => $id])) !== null && ($is_public || $model->user_id === Yii::$app->user->id)) {
return $model;
}
2024-03-05 15:08:11 +08:00
throw new NotFoundHttpException('没有权限访问这个页面或请求的页面不存在');
}
2024-02-26 17:33:53 +08:00
/**
* 外部收集文件访问接口接受参数id:收集文件任务id,secret:访问密钥,CollectionTasks[secret]:访问密钥(另一种形式)
2024-02-26 17:33:53 +08:00
*
* @param $id
* @param $secret
* @return string
* @throws NotFoundHttpException
*/
2024-03-05 15:08:11 +08:00
public function actionAccess($id = null, $secret = null): string
2024-02-26 17:33:53 +08:00
{
$receive_secret = Yii::$app->request->get('CollectionTasks')['secret'] ?? null;
if (!is_null($receive_secret)) {
$secret = $receive_secret;
}
2024-03-05 15:08:11 +08:00
$model = $this->findModel($id, true);
if ($model === null | $model->status === 0) {
throw new NotFoundHttpException('请求的文件收集任务已失效或不存在');
} elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path)) {
throw new NotFoundHttpException('收集任务的目标路径不存在');
2024-02-26 17:33:53 +08:00
} elseif ($secret === null) {
return $this->render('_gateway', [
2024-02-26 17:33:53 +08:00
'model' => new CollectionTasks(),
]);
} elseif ($model->secret !== $secret) {
2024-03-05 15:08:11 +08:00
Yii::$app->session->setFlash('error', '访问凭证不正确');
return $this->render('_gateway', [
2024-02-26 17:33:53 +08:00
'model' => new CollectionTasks(),
]);
} else {
$model2 = new CollectionUploaded();
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));
2024-02-26 17:33:53 +08:00
return $this->render('access', [
'model' => $model,
'model2' => $model2,
]);
}
}
/**
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
2024-03-05 15:08:11 +08:00
public function actionUpload(): Response
{
$request = Yii::$app->request;
// 获取POST请求中的参数
$taskId = $request->post('CollectionTasks')['id'];
$subfolderName = $request->post('CollectionUploaded')['subfolder_name'];
// 获取发送POST请求的用户的IP地址
$uploaderIp = $request->userIP;
2024-03-05 15:08:11 +08:00
$task = $this->findModel($taskId, true); //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('上传失败,验证错误');
}
}