2024-02-23 14:37:36 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\controllers;
|
|
|
|
|
|
|
|
|
|
use app\models\CollectionTasks;
|
|
|
|
|
use app\models\CollectionSearch;
|
2024-02-26 17:33:53 +08:00
|
|
|
|
use app\models\CollectionUploaded;
|
2024-03-10 13:20:32 +08:00
|
|
|
|
use app\utils\FileSizeHelper;
|
2024-02-26 17:33:53 +08:00
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2024-03-05 15:08:11 +08:00
|
|
|
|
use RuntimeException;
|
2024-02-25 10:31:07 +08:00
|
|
|
|
use Yii;
|
2024-03-05 15:08:11 +08:00
|
|
|
|
use yii\filters\AccessControl;
|
2024-02-23 14:37:36 +08:00
|
|
|
|
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;
|
2024-02-26 19:36:09 +08:00
|
|
|
|
use yii\web\ServerErrorHttpException;
|
|
|
|
|
use yii\web\UploadedFile;
|
2024-02-23 14:37:36 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2024-02-23 14:37:36 +08:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
2024-02-23 14:37:36 +08:00
|
|
|
|
'verbs' => [
|
2024-03-05 15:08:11 +08:00
|
|
|
|
'class' => VerbFilter::class,
|
2024-02-23 14:37:36 +08:00
|
|
|
|
'actions' => [
|
2024-03-05 15:08:11 +08:00
|
|
|
|
'index' => ['GET'],
|
|
|
|
|
'view' => ['GET'],
|
|
|
|
|
'create' => ['POST'],
|
2024-02-23 14:37:36 +08:00
|
|
|
|
'delete' => ['POST'],
|
2024-03-10 13:20:32 +08:00
|
|
|
|
'access' => ['GET'], //剩余空间检查√
|
|
|
|
|
'upload' => ['POST'], //剩余空间检查√
|
2024-02-23 14:37:36 +08:00
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-03-21 14:00:10 +08:00
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
|
|
|
|
parent::init();
|
2024-02-23 14:37:36 +08:00
|
|
|
|
|
2024-03-21 14:00:10 +08:00
|
|
|
|
if (Yii::$app->user->can('admin')) {
|
|
|
|
|
$this->layout = 'admin_main';
|
|
|
|
|
}elseif (Yii::$app->user->isGuest) {
|
|
|
|
|
$this->layout = 'guest_main';
|
|
|
|
|
} else {
|
|
|
|
|
$this->layout = 'main';
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-23 14:37:36 +08:00
|
|
|
|
/**
|
|
|
|
|
* Lists all CollectionTasks models.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-03-05 15:08:11 +08:00
|
|
|
|
public function actionIndex(): string
|
2024-02-23 14:37:36 +08:00
|
|
|
|
{
|
|
|
|
|
$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');
|
|
|
|
|
}
|
2024-02-23 14:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2024-02-23 14:37:36 +08:00
|
|
|
|
{
|
|
|
|
|
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-02-23 14:37:36 +08:00
|
|
|
|
*/
|
2024-03-05 15:08:11 +08:00
|
|
|
|
public function actionCreate(): Response|string
|
2024-02-23 14:37:36 +08:00
|
|
|
|
{
|
|
|
|
|
$model = new CollectionTasks();
|
2024-02-25 10:31:07 +08:00
|
|
|
|
$model->scenario = 'create'; // 设置场景
|
2024-02-23 14:37:36 +08:00
|
|
|
|
|
2024-03-05 15:08:11 +08:00
|
|
|
|
if ($this->request instanceof Request && $this->request->isPost) {
|
2024-02-25 10:31:07 +08:00
|
|
|
|
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-02-23 14:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-05 15:08:11 +08:00
|
|
|
|
//不允许get,所以无视下面这个return
|
2024-02-23 14:37:36 +08:00
|
|
|
|
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
|
2024-02-23 14:37:36 +08:00
|
|
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
|
|
|
*/
|
2024-03-05 15:08:11 +08:00
|
|
|
|
public function actionDelete(int $id): Response
|
2024-02-23 14:37:36 +08:00
|
|
|
|
{
|
2024-03-04 16:26:37 +08:00
|
|
|
|
// 获取模型
|
|
|
|
|
$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.');
|
|
|
|
|
}
|
2024-02-23 14:37:36 +08:00
|
|
|
|
|
|
|
|
|
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-02-23 14:37:36 +08:00
|
|
|
|
{
|
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)) {
|
2024-02-23 14:37:36 +08:00
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 15:08:11 +08:00
|
|
|
|
throw new NotFoundHttpException('没有权限访问这个页面或请求的页面不存在');
|
2024-02-23 14:37:36 +08:00
|
|
|
|
}
|
2024-02-26 17:33:53 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2024-02-26 19:36:09 +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);
|
2024-03-04 16:26:37 +08:00
|
|
|
|
if ($model === null | $model->status === 0) {
|
|
|
|
|
throw new NotFoundHttpException('请求的文件收集任务已失效或不存在');
|
2024-02-26 19:36:09 +08:00
|
|
|
|
} elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path)) {
|
|
|
|
|
throw new NotFoundHttpException('收集任务的目标路径不存在');
|
2024-03-10 13:20:32 +08:00
|
|
|
|
} elseif (!FileSizeHelper::hasEnoughSpace(0, $model->user_id)) {
|
|
|
|
|
throw new NotFoundHttpException('由于该用户存储空间已耗尽,请求的文件收集任务暂停');
|
2024-02-26 17:33:53 +08:00
|
|
|
|
} elseif ($secret === null) {
|
2024-02-26 19:36:09 +08:00
|
|
|
|
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', '访问凭证不正确');
|
2024-02-26 19:36:09 +08:00
|
|
|
|
return $this->render('_gateway', [
|
2024-02-26 17:33:53 +08:00
|
|
|
|
'model' => new CollectionTasks(),
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$model2 = new CollectionUploaded();
|
2024-02-26 19:36:09 +08:00
|
|
|
|
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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-26 19:36:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
|
*/
|
2024-03-05 15:08:11 +08:00
|
|
|
|
public function actionUpload(): Response
|
2024-02-26 19:36:09 +08:00
|
|
|
|
{
|
|
|
|
|
$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);
|
2024-02-26 19:36:09 +08:00
|
|
|
|
$userId = $task->user_id;
|
|
|
|
|
$folderPath = $task->folder_path;
|
2024-03-10 13:20:32 +08:00
|
|
|
|
if (!FileSizeHelper::hasEnoughSpace(0, $userId)) {
|
|
|
|
|
throw new NotFoundHttpException('由于该用户存储空间已耗尽,请求的文件收集任务暂停');
|
|
|
|
|
}
|
2024-02-26 19:36:09 +08:00
|
|
|
|
|
|
|
|
|
// 创建一个新的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('上传失败,验证错误');
|
|
|
|
|
}
|
2024-02-23 14:37:36 +08:00
|
|
|
|
}
|