2024-02-10 17:41:07 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\controllers;
|
|
|
|
|
|
2024-02-14 21:35:54 +08:00
|
|
|
|
use app\models\NewFolderForm;
|
2024-02-11 16:04:21 +08:00
|
|
|
|
use app\models\RenameForm;
|
2024-02-13 20:44:01 +08:00
|
|
|
|
use app\models\UploadForm;
|
2024-02-15 18:37:06 +08:00
|
|
|
|
use app\models\ZipForm;
|
2024-03-09 17:25:23 +08:00
|
|
|
|
use app\utils\FileSizeHelper;
|
2024-02-11 12:39:05 +08:00
|
|
|
|
use app\utils\FileTypeDetector;
|
2024-02-16 13:59:54 +08:00
|
|
|
|
use Exception;
|
|
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
|
use RecursiveIteratorIterator;
|
2024-02-16 11:51:53 +08:00
|
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
|
2024-02-15 18:37:06 +08:00
|
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\FileAlreadyExistsException;
|
|
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
|
|
|
|
|
use wapmorgan\UnifiedArchive\UnifiedArchive;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
use Yii;
|
2024-02-14 21:35:54 +08:00
|
|
|
|
use yii\bootstrap5\ActiveForm;
|
2024-03-05 15:08:11 +08:00
|
|
|
|
use yii\filters\AccessControl;
|
2024-02-11 16:04:21 +08:00
|
|
|
|
use yii\filters\VerbFilter;
|
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
use yii\web\Controller;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
|
use yii\web\Response;
|
2024-02-13 20:44:01 +08:00
|
|
|
|
use yii\web\UploadedFile;
|
2024-02-15 12:10:41 +08:00
|
|
|
|
use ZipArchive;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
|
2024-02-11 16:04:21 +08:00
|
|
|
|
class HomeController extends Controller
|
2024-02-10 17:41:07 +08:00
|
|
|
|
{
|
2024-02-14 19:12:09 +08:00
|
|
|
|
protected string $pattern = '/^[^\p{C}:*?"<>|\\\\]+$/u';
|
2024-02-13 20:44:01 +08:00
|
|
|
|
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function behaviors(): array
|
2024-02-11 16:04:21 +08:00
|
|
|
|
{
|
|
|
|
|
return array_merge(
|
|
|
|
|
parent::behaviors(),
|
|
|
|
|
[
|
2024-03-05 15:08:11 +08:00
|
|
|
|
'access' => [
|
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
|
'rules' => [
|
|
|
|
|
[
|
|
|
|
|
'allow' => true,
|
2024-03-20 16:26:03 +08:00
|
|
|
|
'actions' => ['index', 'download', 'preview', 'rename', 'delete', 'upload', 'new-folder', 'download-folder', 'multi-ff-zip-dl', 'zip', 'unzip', 'paste', 'search'],
|
2024-03-05 15:08:11 +08:00
|
|
|
|
'roles' => ['user'],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
2024-02-11 16:04:21 +08:00
|
|
|
|
'verbs' => [
|
|
|
|
|
'class' => VerbFilter::class,
|
|
|
|
|
'actions' => [
|
|
|
|
|
'index' => ['GET'],
|
|
|
|
|
'download' => ['GET'],
|
2024-03-10 13:20:32 +08:00
|
|
|
|
'preview' => ['GET'],
|
2024-02-11 16:04:21 +08:00
|
|
|
|
'rename' => ['POST'],
|
|
|
|
|
'delete' => ['POST'],
|
2024-03-10 13:20:32 +08:00
|
|
|
|
'upload' => ['POST'], //剩余空间检查√
|
2024-02-15 12:10:41 +08:00
|
|
|
|
'new-folder' => ['POST'],
|
|
|
|
|
'download-folder' => ['GET'],
|
2024-02-15 15:08:17 +08:00
|
|
|
|
'multi-ff-zip-dl' => ['POST'],
|
2024-03-10 13:20:32 +08:00
|
|
|
|
'zip' => ['POST'], //剩余空间检查√
|
|
|
|
|
'unzip' => ['POST'], //剩余空间检查√
|
|
|
|
|
'paste' => ['POST'], //剩余空间检查√
|
2024-03-20 16:26:03 +08:00
|
|
|
|
'search' => ['POST']
|
2024-02-11 16:04:21 +08:00
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 17:41:07 +08:00
|
|
|
|
/**
|
2024-02-16 13:59:54 +08:00
|
|
|
|
* display the page of the file manager (accepts a directory parameter like cd command)
|
2024-02-11 12:39:05 +08:00
|
|
|
|
* visit it via https://devs.chenx221.cyou:8081/index.php?r=home
|
|
|
|
|
*
|
2024-02-16 13:59:54 +08:00
|
|
|
|
* @param null $directory
|
2024-02-10 17:41:07 +08:00
|
|
|
|
* @return string|Response
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionIndex($directory = null): Response|string
|
2024-02-10 17:41:07 +08:00
|
|
|
|
{
|
2024-03-09 17:25:23 +08:00
|
|
|
|
$model = Yii::$app->user->identity;
|
2024-03-05 15:08:11 +08:00
|
|
|
|
// if (Yii::$app->user->isGuest) {
|
|
|
|
|
// Yii::$app->session->setFlash('error','请先登录');
|
|
|
|
|
// return $this->redirect(Yii::$app->user->loginUrl);
|
|
|
|
|
// } else if (!Yii::$app->user->can('accessHome')){
|
|
|
|
|
// throw new NotFoundHttpException('当前用户组不允许访问此页面');
|
|
|
|
|
// }
|
2024-02-16 11:17:55 +08:00
|
|
|
|
$rootDataDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
|
2024-02-12 14:04:13 +08:00
|
|
|
|
if ($directory === '.' || $directory == null) {
|
2024-02-12 12:49:18 +08:00
|
|
|
|
$directory = null;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
$parentDirectory = null;
|
2024-02-15 18:37:06 +08:00
|
|
|
|
} elseif (str_contains($directory, '..')) {
|
2024-02-12 12:49:18 +08:00
|
|
|
|
throw new NotFoundHttpException('Invalid directory.');
|
2024-02-10 17:41:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
$parentDirectory = dirname($directory);
|
|
|
|
|
}
|
2024-02-16 11:17:55 +08:00
|
|
|
|
$directoryContents = $this->getDirectoryContents(join(DIRECTORY_SEPARATOR, [$rootDataDirectory, $directory ?: '.']));
|
2024-02-11 12:39:05 +08:00
|
|
|
|
foreach ($directoryContents as $key => $item) {
|
|
|
|
|
$relativePath = $directory ? $directory . '/' . $item : $item;
|
|
|
|
|
$absolutePath = Yii::getAlias('@app') . '/data/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
$type = FileTypeDetector::detect($absolutePath);
|
2024-02-12 14:04:13 +08:00
|
|
|
|
$lastModified = filemtime($absolutePath);
|
|
|
|
|
$size = is_file($absolutePath) ? filesize($absolutePath) : null;
|
2024-02-19 13:00:07 +08:00
|
|
|
|
$rawType = is_file($absolutePath) ? mime_content_type($absolutePath) : null;
|
|
|
|
|
$directoryContents[$key] = ['name' => $item, 'type' => $type, 'lastModified' => $lastModified, 'size' => $size, 'rawType' => $rawType];
|
2024-02-11 12:39:05 +08:00
|
|
|
|
}
|
2024-03-10 16:38:39 +08:00
|
|
|
|
$usedSpace = FileSizeHelper::getUserHomeDirSize();
|
|
|
|
|
$vaultUsedSpace = FileSizeHelper::getUserVaultDirSize();
|
2024-03-09 17:25:23 +08:00
|
|
|
|
$storageLimit = $model->storage_limit;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
return $this->render('index', [
|
|
|
|
|
'directoryContents' => $directoryContents,
|
|
|
|
|
'parentDirectory' => $parentDirectory,
|
|
|
|
|
'directory' => $directory, // 将$directory传递给视图
|
2024-03-09 17:25:23 +08:00
|
|
|
|
'usedSpace' => $usedSpace, // B
|
|
|
|
|
'vaultUsedSpace' => $vaultUsedSpace, // B
|
|
|
|
|
'storageLimit' => $storageLimit, // MB
|
2024-02-10 17:41:07 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定路径下的文件和文件夹内容
|
|
|
|
|
* @param string $path 路径
|
|
|
|
|
* @return array 文件和文件夹内容数组
|
|
|
|
|
* @throws NotFoundHttpException 如果路径不存在
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
protected function getDirectoryContents(string $path): array
|
2024-02-10 17:41:07 +08:00
|
|
|
|
{
|
|
|
|
|
// 确定路径是否存在
|
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
|
throw new NotFoundHttpException('Directory not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取路径下的所有文件和文件夹
|
|
|
|
|
$directoryContents = scandir($path);
|
|
|
|
|
|
2024-02-12 12:49:18 +08:00
|
|
|
|
// 移除 '.' 和 '..'
|
|
|
|
|
$directoryContents = array_diff($directoryContents, ['.', '..']);
|
|
|
|
|
|
|
|
|
|
// 使用 usort 对目录内容进行排序,使文件夹始终在文件之前
|
|
|
|
|
usort($directoryContents, function ($a, $b) use ($path) {
|
|
|
|
|
$aIsDir = is_dir($path . '/' . $a);
|
|
|
|
|
$bIsDir = is_dir($path . '/' . $b);
|
|
|
|
|
if ($aIsDir === $bIsDir) {
|
|
|
|
|
return strnatcasecmp($a, $b); // 如果两者都是文件夹或都是文件,按名称排序
|
|
|
|
|
}
|
|
|
|
|
return $aIsDir ? -1 : 1; // 文件夹始终在文件之前
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $directoryContents;
|
2024-02-10 17:41:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下载指定路径下的文件
|
2024-02-11 12:39:05 +08:00
|
|
|
|
* download link:https://devs.chenx221.cyou:8081/index.php?r=home%2Fdownload&relativePath={the relative path of the file}
|
|
|
|
|
*
|
2024-02-10 17:41:07 +08:00
|
|
|
|
* @param string $relativePath 文件的相对路径
|
|
|
|
|
* @throws NotFoundHttpException 如果文件不存在
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionDownload(string $relativePath): void
|
2024-02-10 17:41:07 +08:00
|
|
|
|
{
|
|
|
|
|
// 对相对路径进行解码
|
|
|
|
|
$relativePath = rawurldecode($relativePath);
|
|
|
|
|
|
|
|
|
|
// 检查相对路径是否只包含允许的字符
|
2024-02-15 18:37:06 +08:00
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
2024-02-10 17:41:07 +08:00
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确定文件的绝对路径
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
// 使用realpath函数解析路径,并检查解析后的路径是否在预期的目录中
|
|
|
|
|
$realPath = realpath($absolutePath);
|
2024-04-11 15:40:08 +08:00
|
|
|
|
$dataDirectory = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, Yii::getAlias(Yii::$app->params['dataDirectory']));
|
2024-02-10 17:41:07 +08:00
|
|
|
|
if (!$realPath || !str_starts_with($realPath, $dataDirectory)) {
|
|
|
|
|
throw new NotFoundHttpException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
|
|
if (!file_exists($realPath)) {
|
|
|
|
|
throw new NotFoundHttpException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将文件发送给用户进行下载
|
|
|
|
|
Yii::$app->response->sendFile($realPath)->send();
|
|
|
|
|
}
|
2024-02-11 16:04:21 +08:00
|
|
|
|
|
2024-02-19 11:30:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param string $relativePath
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
|
|
|
|
public function actionPreview(string $relativePath): void
|
|
|
|
|
{
|
|
|
|
|
// 对相对路径进行解码
|
|
|
|
|
$relativePath = rawurldecode($relativePath);
|
|
|
|
|
|
|
|
|
|
// 检查相对路径是否只包含允许的字符
|
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确定文件的绝对路径
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
|
|
if (!file_exists($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取图像的 MIME 类型
|
|
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
|
|
|
$mimeType = finfo_file($finfo, $absolutePath);
|
|
|
|
|
finfo_close($finfo);
|
|
|
|
|
|
|
|
|
|
// 设置响应头
|
|
|
|
|
header('Content-Type: ' . $mimeType);
|
|
|
|
|
header('Content-Disposition: inline; filename="' . basename($absolutePath) . '"');
|
|
|
|
|
|
|
|
|
|
// 读取并输出图像数据
|
|
|
|
|
readfile($absolutePath);
|
|
|
|
|
|
|
|
|
|
// 结束脚本执行
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2024-03-10 13:20:32 +08:00
|
|
|
|
|
2024-02-11 16:04:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* 重命名文件或文件夹
|
2024-02-12 14:11:49 +08:00
|
|
|
|
* @return string|Response|null
|
2024-02-11 16:04:21 +08:00
|
|
|
|
* @throws NotFoundHttpException 如果文件或文件夹不存在
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionRename(): Response|string|null
|
2024-02-11 16:04:21 +08:00
|
|
|
|
{
|
|
|
|
|
$relativePath = Yii::$app->request->post('relativePath');
|
|
|
|
|
|
|
|
|
|
// 对相对路径进行解码
|
|
|
|
|
$relativePath = rawurldecode($relativePath);
|
|
|
|
|
|
|
|
|
|
// 检查相对路径是否只包含允许的字符
|
2024-02-15 18:37:06 +08:00
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
2024-02-11 16:04:21 +08:00
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确定文件的绝对路径
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
// 检查文件或文件夹是否存在
|
|
|
|
|
if (!file_exists($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('File or directory not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$model = new RenameForm();
|
|
|
|
|
|
2024-02-16 13:59:54 +08:00
|
|
|
|
try {
|
|
|
|
|
$model->newName = ArrayHelper::getValue(Yii::$app->request->post('RenameForm'), 'newName');
|
|
|
|
|
} catch (Exception) {
|
|
|
|
|
throw new NotFoundHttpException('Invalid request.');
|
|
|
|
|
}
|
2024-02-11 16:04:21 +08:00
|
|
|
|
|
|
|
|
|
if (!$model->validate()) {
|
|
|
|
|
Yii::$app->response->statusCode = 400;
|
|
|
|
|
return $model->getFirstError('newName');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查新名称是否和现有的文件或文件夹重名
|
|
|
|
|
if (file_exists(dirname($absolutePath) . '/' . $model->newName)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to rename file or directory.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => dirname($relativePath)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重命名文件或文件夹
|
|
|
|
|
if (!rename($absolutePath, dirname($absolutePath) . '/' . $model->newName)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to rename file or directory.');
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('success', 'File or directory renamed successfully.');
|
|
|
|
|
}
|
|
|
|
|
return $this->redirect(['index', 'directory' => dirname($relativePath)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除文件或文件夹
|
|
|
|
|
* @throws NotFoundHttpException 如果文件或文件夹不存在
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionDelete(): Response
|
2024-02-11 16:04:21 +08:00
|
|
|
|
{
|
2024-02-16 12:10:27 +08:00
|
|
|
|
$relativePaths = Yii::$app->request->post('relativePath');
|
|
|
|
|
if (!is_array($relativePaths)) {
|
|
|
|
|
$relativePaths = [$relativePaths];
|
2024-02-11 16:04:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 12:10:27 +08:00
|
|
|
|
foreach ($relativePaths as $relativePath) {
|
|
|
|
|
$relativePath = rawurldecode($relativePath);
|
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
if (!file_exists($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('File or directory not found.');
|
2024-02-12 12:49:18 +08:00
|
|
|
|
} else {
|
2024-02-16 12:10:27 +08:00
|
|
|
|
$realPath = realpath($absolutePath);
|
|
|
|
|
$expectedPathPrefix = realpath(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id);
|
|
|
|
|
if (!str_starts_with($realPath, $expectedPathPrefix)) {
|
|
|
|
|
throw new NotFoundHttpException('File or directory not found.');
|
|
|
|
|
}
|
2024-02-12 12:49:18 +08:00
|
|
|
|
}
|
2024-02-16 12:10:27 +08:00
|
|
|
|
|
|
|
|
|
if (is_dir($absolutePath)) {
|
|
|
|
|
if (!$this->deleteDirectory($absolutePath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to delete directory.');
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('success', 'Directory deleted successfully.');
|
|
|
|
|
}
|
2024-02-12 12:49:18 +08:00
|
|
|
|
} else {
|
2024-02-16 12:10:27 +08:00
|
|
|
|
if (!unlink($absolutePath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to delete file.');
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('success', 'File deleted successfully.');
|
|
|
|
|
}
|
2024-02-12 12:49:18 +08:00
|
|
|
|
}
|
2024-02-11 16:04:21 +08:00
|
|
|
|
}
|
2024-02-16 12:10:27 +08:00
|
|
|
|
return $this->redirect(['index', 'directory' => dirname($relativePaths[0])]);
|
2024-02-11 16:04:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 递归删除目录及其内容
|
|
|
|
|
* @param string $dir 目录路径
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
protected function deleteDirectory(string $dir): bool
|
2024-02-11 16:04:21 +08:00
|
|
|
|
{
|
|
|
|
|
if (!is_dir($dir)) {
|
2024-02-12 12:49:18 +08:00
|
|
|
|
return false;
|
2024-02-11 16:04:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$files = array_diff(scandir($dir), ['.', '..']);
|
|
|
|
|
foreach ($files as $file) {
|
2024-02-12 12:49:18 +08:00
|
|
|
|
if (is_dir("$dir/$file")) {
|
|
|
|
|
if (!$this->deleteDirectory("$dir/$file")) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!unlink("$dir/$file")) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!rmdir($dir)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-02-11 16:04:21 +08:00
|
|
|
|
}
|
2024-02-13 20:44:01 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2024-02-14 21:35:54 +08:00
|
|
|
|
* 文件、文件夹上传
|
2024-02-14 19:12:09 +08:00
|
|
|
|
* 注意,已存在的同名文件会被覆盖
|
2024-02-13 20:44:01 +08:00
|
|
|
|
* https://devs.chenx221.cyou:8081/index.php?r=home%2Fupload
|
|
|
|
|
*
|
2024-02-20 16:24:42 +08:00
|
|
|
|
* @return int|Response
|
2024-02-13 20:44:01 +08:00
|
|
|
|
*/
|
2024-03-05 15:08:11 +08:00
|
|
|
|
public function actionUpload(): Response|int
|
2024-02-13 20:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
$model = new UploadForm();
|
|
|
|
|
$model->targetDir = Yii::$app->request->post('targetDir', '.');
|
|
|
|
|
$uploadedFiles = UploadedFile::getInstancesByName('files');
|
|
|
|
|
$successCount = 0;
|
|
|
|
|
$totalCount = count($uploadedFiles);
|
2024-03-17 15:37:13 +08:00
|
|
|
|
$sp = Yii::$app->request->post('sp');
|
2024-02-13 20:44:01 +08:00
|
|
|
|
|
|
|
|
|
foreach ($uploadedFiles as $uploadedFile) {
|
|
|
|
|
$model->uploadFile = $uploadedFile;
|
2024-02-15 18:37:06 +08:00
|
|
|
|
if (!preg_match($this->pattern, $model->uploadFile->fullPath) || str_contains($model->uploadFile->fullPath, '..')) {
|
2024-02-13 20:44:01 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-03-10 13:20:32 +08:00
|
|
|
|
if (!FileSizeHelper::hasEnoughSpace($model->uploadFile->size)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-02-13 20:44:01 +08:00
|
|
|
|
if ($model->upload()) {
|
|
|
|
|
$successCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-20 16:24:42 +08:00
|
|
|
|
if ($sp === 'editSaving') {
|
|
|
|
|
if ($successCount === $totalCount) {
|
|
|
|
|
Yii::$app->response->statusCode = 200;
|
2024-03-10 13:20:32 +08:00
|
|
|
|
return $this->asJson(['status' => 200, 'message' => '文件上传成功']);
|
|
|
|
|
} else {
|
2024-02-20 16:24:42 +08:00
|
|
|
|
Yii::$app->response->statusCode = 500;
|
2024-03-10 13:20:32 +08:00
|
|
|
|
return $this->asJson(['status' => 500, 'message' => '文件上传失败']);
|
2024-02-20 16:24:42 +08:00
|
|
|
|
}
|
2024-03-10 13:20:32 +08:00
|
|
|
|
} else {
|
2024-02-20 16:24:42 +08:00
|
|
|
|
if ($successCount === $totalCount) {
|
2024-03-10 13:20:32 +08:00
|
|
|
|
Yii::$app->session->setFlash('success', '文件上传成功');
|
2024-02-20 16:24:42 +08:00
|
|
|
|
} elseif ($successCount > 0) {
|
2024-03-10 13:20:32 +08:00
|
|
|
|
Yii::$app->session->setFlash('warning', '部分文件上传失败,这可能是用户剩余空间不足导致');
|
2024-02-20 16:24:42 +08:00
|
|
|
|
} else {
|
2024-03-10 13:20:32 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '文件上传失败,这可能是用户剩余空间不足导致');
|
2024-02-20 16:24:42 +08:00
|
|
|
|
}
|
|
|
|
|
//返回状态码200
|
2024-03-10 13:20:32 +08:00
|
|
|
|
return Yii::$app->response->statusCode = 200;
|
2024-02-13 20:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-14 21:35:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2024-02-15 10:14:23 +08:00
|
|
|
|
* 新建文件夹
|
|
|
|
|
*
|
2024-02-16 13:59:54 +08:00
|
|
|
|
* @return array|Response
|
2024-02-14 21:35:54 +08:00
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionNewFolder(): Response|array
|
2024-02-14 21:35:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$relativePath = Yii::$app->request->post('relativePath');
|
|
|
|
|
$relativePath = rawurldecode($relativePath);
|
|
|
|
|
$model = new NewFolderForm();
|
|
|
|
|
|
2024-02-15 12:10:41 +08:00
|
|
|
|
if ($model->load(Yii::$app->request->post())) {
|
2024-02-14 21:35:54 +08:00
|
|
|
|
$model->relativePath = $relativePath;
|
2024-02-15 12:10:41 +08:00
|
|
|
|
if (Yii::$app->request->isAjax) {
|
2024-02-14 21:35:54 +08:00
|
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
|
|
|
|
return ActiveForm::validate($model);
|
|
|
|
|
}
|
2024-02-15 12:10:41 +08:00
|
|
|
|
if ($model->validate()) {
|
2024-02-14 21:35:54 +08:00
|
|
|
|
if ($model->createFolder()) {
|
|
|
|
|
Yii::$app->session->setFlash('success', 'Folder created successfully.');
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to create folder.');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$errors = $model->errors;
|
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
|
Yii::$app->session->setFlash('error', $error[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->redirect(['index', 'directory' => $relativePath]);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 12:10:41 +08:00
|
|
|
|
/**
|
|
|
|
|
* 计算指定目录的大小
|
|
|
|
|
*
|
|
|
|
|
* @param string $directory 目录路径
|
|
|
|
|
* @return int 目录的大小(字节)
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
protected function getDirectorySize(string $directory): int
|
2024-02-15 12:10:41 +08:00
|
|
|
|
{
|
|
|
|
|
$size = 0;
|
2024-02-16 13:59:54 +08:00
|
|
|
|
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
|
2024-02-15 12:10:41 +08:00
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if ($file->isFile()) {
|
|
|
|
|
$size += $file->getSize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionDownloadFolder($relativePath): Response|\yii\console\Response
|
2024-02-15 12:10:41 +08:00
|
|
|
|
{
|
|
|
|
|
$relativePath = rawurldecode($relativePath);
|
2024-02-15 18:37:06 +08:00
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
2024-02-15 12:10:41 +08:00
|
|
|
|
throw new NotFoundHttpException('Invalid path.');
|
|
|
|
|
}
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
if (!is_dir($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('Directory not found.');
|
|
|
|
|
}
|
|
|
|
|
$size = $this->getDirectorySize($absolutePath);
|
|
|
|
|
|
|
|
|
|
// 检查需要下载的文件夹大小是否超过200MB
|
|
|
|
|
if ($size > 200 * 1024 * 1024) {
|
|
|
|
|
throw new NotFoundHttpException('Directory size exceeds the limit of 200MB.');
|
2024-02-15 15:08:17 +08:00
|
|
|
|
} else {
|
2024-02-15 12:10:41 +08:00
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
|
$tempDir = sys_get_temp_dir() . '/';
|
|
|
|
|
$zipPath = $tempDir . basename($absolutePath) . '.zip';
|
|
|
|
|
|
|
|
|
|
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
|
2024-02-16 13:59:54 +08:00
|
|
|
|
$files = new RecursiveIteratorIterator(
|
|
|
|
|
new RecursiveDirectoryIterator($absolutePath),
|
|
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY
|
2024-02-15 12:10:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
2024-02-16 13:59:54 +08:00
|
|
|
|
foreach ($files as $file) {
|
2024-02-15 12:10:41 +08:00
|
|
|
|
if (!$file->isDir()) {
|
|
|
|
|
$filePath = $file->getRealPath();
|
|
|
|
|
$relativePath = substr($filePath, strlen($absolutePath) + 1);
|
|
|
|
|
|
|
|
|
|
$zip->addFile($filePath, $relativePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-14 21:35:54 +08:00
|
|
|
|
|
2024-02-15 12:10:41 +08:00
|
|
|
|
$zip->close();
|
|
|
|
|
}
|
|
|
|
|
return Yii::$app->response->sendFile($zipPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-15 15:08:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return \yii\console\Response|Response
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionMultiFfZipDl(): Response|\yii\console\Response
|
2024-02-15 15:08:17 +08:00
|
|
|
|
{
|
|
|
|
|
// 获取请求中的文件和文件夹的相对路径
|
|
|
|
|
$relativePaths = Yii::$app->request->post('relativePaths');
|
|
|
|
|
|
|
|
|
|
// 对相对路径进行解码
|
|
|
|
|
$relativePaths = array_map('rawurldecode', $relativePaths);
|
|
|
|
|
|
|
|
|
|
// 初始化总大小
|
|
|
|
|
$totalSize = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($relativePaths as $relativePath) {
|
|
|
|
|
// 检查相对路径是否只包含允许的字符
|
2024-02-15 18:37:06 +08:00
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
2024-02-15 15:08:17 +08:00
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确定文件或文件夹的绝对路径
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
// 检查文件或文件夹是否存在
|
|
|
|
|
if (!file_exists($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('File or directory not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算文件或文件夹的大小,并添加到总大小
|
|
|
|
|
if (is_dir($absolutePath)) {
|
|
|
|
|
$totalSize += $this->getDirectorySize($absolutePath);
|
|
|
|
|
} else {
|
|
|
|
|
$totalSize += filesize($absolutePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查总大小是否超过200MB
|
|
|
|
|
if ($totalSize > 200 * 1024 * 1024) {
|
|
|
|
|
throw new NotFoundHttpException('Total size exceeds the limit of 200MB.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建一个新的ZipArchive实例
|
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
|
$tempDir = sys_get_temp_dir() . '/';
|
|
|
|
|
$zipPath = $tempDir . time() . '.zip';
|
|
|
|
|
|
|
|
|
|
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
|
|
|
|
|
foreach ($relativePaths as $relativePath) {
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
if (is_file($absolutePath)) {
|
|
|
|
|
$zip->addFile($absolutePath, $relativePath);
|
|
|
|
|
} else if (is_dir($absolutePath)) {
|
2024-02-16 13:59:54 +08:00
|
|
|
|
$files = new RecursiveIteratorIterator(
|
|
|
|
|
new RecursiveDirectoryIterator($absolutePath),
|
|
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY
|
2024-02-15 15:08:17 +08:00
|
|
|
|
);
|
2024-02-16 13:59:54 +08:00
|
|
|
|
foreach ($files as $file) {
|
2024-02-15 15:08:17 +08:00
|
|
|
|
if (!$file->isDir()) {
|
|
|
|
|
$filePath = $file->getRealPath();
|
|
|
|
|
$relativePathInZip = $relativePath . '/' . substr($filePath, strlen($absolutePath) + 1);
|
|
|
|
|
|
|
|
|
|
$zip->addFile($filePath, $relativePathInZip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$zip->close();
|
|
|
|
|
}
|
|
|
|
|
return Yii::$app->response->sendFile($zipPath);
|
|
|
|
|
}
|
2024-02-15 18:37:06 +08:00
|
|
|
|
|
2024-02-16 13:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @return Response
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionZip(): Response
|
2024-02-15 18:37:06 +08:00
|
|
|
|
{
|
|
|
|
|
$model = new ZipForm();
|
|
|
|
|
$relativePaths = json_decode(Yii::$app->request->post('relativePath'), true);
|
2024-03-10 13:20:32 +08:00
|
|
|
|
$targetDirectory = Yii::$app->request->post('targetDirectory') ?? '.';
|
2024-02-15 18:37:06 +08:00
|
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
|
|
|
|
|
if (str_contains($targetDirectory, '..')) {
|
|
|
|
|
throw new NotFoundHttpException('Invalid target directory.');
|
|
|
|
|
} elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $targetDirectory)) {
|
|
|
|
|
throw new NotFoundHttpException('Directory not found.');
|
|
|
|
|
}
|
|
|
|
|
$absolutePaths = [];
|
|
|
|
|
foreach ($relativePaths as $relativePath) {
|
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
if (!file_exists($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('The requested file does not exist.');
|
|
|
|
|
}
|
|
|
|
|
$absolutePaths[] = $absolutePath;
|
|
|
|
|
}
|
|
|
|
|
$zipPath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $targetDirectory . '/' . $model->zipFilename . '.' . $model->zipFormat;
|
|
|
|
|
try {
|
|
|
|
|
UnifiedArchive::create($absolutePaths, $zipPath);
|
2024-03-10 13:20:32 +08:00
|
|
|
|
// 检查新的压缩文件的大小是否超过用户的存储限制
|
|
|
|
|
if (!FileSizeHelper::hasEnoughSpace()) {
|
|
|
|
|
// 如果超过,删除这个新的压缩文件,并显示一个错误消息
|
|
|
|
|
unlink($zipPath);
|
|
|
|
|
Yii::$app->session->setFlash('error', '打包失败,空间不足');
|
|
|
|
|
} else {
|
|
|
|
|
Yii::$app->session->setFlash('success', '打包成功');
|
|
|
|
|
}
|
2024-02-16 13:59:54 +08:00
|
|
|
|
} catch (FileAlreadyExistsException) {
|
2024-02-15 18:37:06 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '目标文件夹已存在同名压缩档案');
|
2024-02-16 13:59:54 +08:00
|
|
|
|
} catch (UnsupportedOperationException) {
|
2024-02-15 18:37:06 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '不支持的操作');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
2024-02-16 11:51:53 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionUnzip(): array
|
2024-02-16 11:51:53 +08:00
|
|
|
|
{
|
|
|
|
|
$relativePath = Yii::$app->request->post('relativePath');
|
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
|
|
|
|
|
if (!file_exists($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('File not found.');
|
|
|
|
|
}
|
|
|
|
|
$archive = UnifiedArchive::open($absolutePath);
|
|
|
|
|
if ($archive === null) {
|
|
|
|
|
throw new NotFoundHttpException('Failed to open the archive.');
|
|
|
|
|
}
|
2024-02-27 11:15:09 +08:00
|
|
|
|
$now_time = time();
|
|
|
|
|
$targetDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time;
|
2024-02-16 11:51:53 +08:00
|
|
|
|
if (!is_dir($targetDirectory)) {
|
|
|
|
|
mkdir($targetDirectory, 0777, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$archive->extract($targetDirectory);
|
2024-02-16 13:59:54 +08:00
|
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
2024-03-10 13:20:32 +08:00
|
|
|
|
if (!FileSizeHelper::hasEnoughSpace()) {
|
|
|
|
|
$this->deleteDirectory($targetDirectory);
|
|
|
|
|
Yii::$app->session->setFlash('error', '解压失败,空间不足');
|
|
|
|
|
return [
|
|
|
|
|
'status' => 500,
|
|
|
|
|
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
|
|
|
|
|
'parentDirectory' => dirname($relativePath),
|
|
|
|
|
];
|
2024-03-20 16:26:03 +08:00
|
|
|
|
} else {
|
2024-03-10 13:20:32 +08:00
|
|
|
|
Yii::$app->session->setFlash('success', '解压成功');
|
|
|
|
|
return [
|
|
|
|
|
'status' => 200,
|
|
|
|
|
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-02-16 13:59:54 +08:00
|
|
|
|
} catch (ArchiveExtractionException) {
|
2024-02-16 11:51:53 +08:00
|
|
|
|
$this->deleteDirectory($targetDirectory);
|
2024-03-10 13:20:32 +08:00
|
|
|
|
Yii::$app->session->setFlash('error', '解压过程中出现错误');
|
2024-02-16 13:59:54 +08:00
|
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
2024-02-16 11:51:53 +08:00
|
|
|
|
return [
|
|
|
|
|
'status' => 500,
|
2024-02-27 11:15:09 +08:00
|
|
|
|
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
|
2024-02-16 11:51:53 +08:00
|
|
|
|
'parentDirectory' => dirname($relativePath),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-16 13:37:15 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
public function actionPaste(): Response
|
2024-02-16 13:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
// 获取请求中的操作类型、相对路径和目标目录
|
|
|
|
|
$operation = Yii::$app->request->post('operation');
|
|
|
|
|
$relativePaths = Yii::$app->request->post('relativePaths');
|
|
|
|
|
$targetDirectory = Yii::$app->request->post('targetDirectory');
|
|
|
|
|
|
|
|
|
|
// 对相对路径进行解码
|
|
|
|
|
$relativePaths = array_map('rawurldecode', $relativePaths);
|
|
|
|
|
|
|
|
|
|
// 对每个相对路径进行处理
|
|
|
|
|
foreach ($relativePaths as $relativePath) {
|
|
|
|
|
// 确定源文件/目录和目标文件/目录的绝对路径
|
|
|
|
|
$sourcePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
$targetPath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $targetDirectory . '/' . basename($relativePath);
|
|
|
|
|
|
|
|
|
|
// 检查目标路径是否是源路径的子路径 // 你是白痴吗
|
|
|
|
|
if ($operation === 'cut' && is_dir($sourcePath) && str_starts_with($targetPath, $sourcePath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Cannot move a directory into itself.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查目标路径是否已经存在同名文件或目录
|
|
|
|
|
if ($operation === 'copy' && file_exists($targetPath)) {
|
2024-03-10 13:20:32 +08:00
|
|
|
|
$sourceFileSize = filesize($sourcePath);
|
|
|
|
|
|
|
|
|
|
// 检查是否有足够的空间进行复制操作
|
|
|
|
|
if (!FileSizeHelper::hasEnoughSpace($sourceFileSize)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Not enough space to copy the file.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
2024-02-16 13:37:15 +08:00
|
|
|
|
$pathInfo = pathinfo($targetPath);
|
|
|
|
|
$i = 1;
|
|
|
|
|
do {
|
|
|
|
|
$newFilename = $pathInfo['filename'] . '_副本' . ($i > 1 ? $i : '');
|
|
|
|
|
$newTargetPath = $pathInfo['dirname'] . '/' . $newFilename . '.' . $pathInfo['extension'];
|
|
|
|
|
$i++;
|
|
|
|
|
} while (file_exists($newTargetPath));
|
|
|
|
|
$targetPath = $newTargetPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据操作类型执行相应的操作
|
|
|
|
|
if ($operation === 'copy') {
|
2024-03-10 13:20:32 +08:00
|
|
|
|
$sourceSize = is_dir($sourcePath) ? FileSizeHelper::getDirectorySize($sourcePath) : filesize($sourcePath);
|
|
|
|
|
if (!FileSizeHelper::hasEnoughSpace($sourceSize)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Not enough space to copy.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
2024-02-16 13:37:15 +08:00
|
|
|
|
if (is_dir($sourcePath)) {
|
|
|
|
|
// 如果源路径是目录,递归复制目录
|
|
|
|
|
if (!$this->copyDirectory($sourcePath, $targetPath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to copy directory.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果源路径是文件,使用PHP的copy函数复制文件
|
|
|
|
|
if (!copy($sourcePath, $targetPath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to copy file.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($operation === 'cut') {
|
|
|
|
|
// 如果是剪切操作
|
|
|
|
|
if (is_dir($sourcePath)) {
|
|
|
|
|
// 如果源路径是目录,递归移动目录
|
|
|
|
|
if (!$this->moveDirectory($sourcePath, $targetPath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to move directory.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果源路径是文件,使用PHP的rename函数移动文件
|
|
|
|
|
if (!rename($sourcePath, $targetPath)) {
|
|
|
|
|
Yii::$app->session->setFlash('error', 'Failed to move file.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Yii::$app->session->setFlash('success', 'Operation completed successfully.');
|
|
|
|
|
return $this->redirect(['index', 'directory' => $targetDirectory]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 递归复制目录及其内容
|
|
|
|
|
* @param string $source 源目录路径
|
|
|
|
|
* @param string $destination 目标目录路径
|
|
|
|
|
* @return bool 操作是否成功
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
protected function copyDirectory(string $source, string $destination): bool
|
2024-02-16 13:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
// 创建目标目录
|
|
|
|
|
if (!mkdir($destination)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打开源目录
|
|
|
|
|
$dir = opendir($source);
|
|
|
|
|
if ($dir === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 递归复制目录的内容
|
|
|
|
|
while (($entry = readdir($dir)) !== false) {
|
|
|
|
|
if ($entry === '.' || $entry === '..') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sourcePath = $source . '/' . $entry;
|
|
|
|
|
$destinationPath = $destination . '/' . $entry;
|
|
|
|
|
|
|
|
|
|
if (is_dir($sourcePath)) {
|
|
|
|
|
// 如果源路径是目录,递归复制目录
|
|
|
|
|
if (!$this->copyDirectory($sourcePath, $destinationPath)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果源路径是文件,复制文件
|
|
|
|
|
if (!copy($sourcePath, $destinationPath)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir($dir);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 递归移动目录及其内容
|
|
|
|
|
* @param string $source 源目录路径
|
|
|
|
|
* @param string $destination 目标目录路径
|
|
|
|
|
* @return bool 操作是否成功
|
|
|
|
|
*/
|
2024-02-16 13:59:54 +08:00
|
|
|
|
protected function moveDirectory(string $source, string $destination): bool
|
2024-02-16 13:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
// 创建目标目录
|
|
|
|
|
if (!mkdir($destination)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打开源目录
|
|
|
|
|
$dir = opendir($source);
|
|
|
|
|
if ($dir === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 递归移动目录的内容
|
|
|
|
|
while (($entry = readdir($dir)) !== false) {
|
|
|
|
|
if ($entry === '.' || $entry === '..') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sourcePath = $source . '/' . $entry;
|
|
|
|
|
$destinationPath = $destination . '/' . $entry;
|
|
|
|
|
|
|
|
|
|
if (is_dir($sourcePath)) {
|
|
|
|
|
// 如果源路径是目录,递归移动目录
|
|
|
|
|
if (!$this->moveDirectory($sourcePath, $destinationPath)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果源路径是文件,移动文件
|
|
|
|
|
if (!rename($sourcePath, $destinationPath)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir($dir);
|
|
|
|
|
|
|
|
|
|
// 删除源目录
|
|
|
|
|
if (!rmdir($source)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-16 13:59:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
|
|
|
|
public function actionChecksum(): array
|
|
|
|
|
{
|
|
|
|
|
$relativePath = Yii::$app->request->post('relativePath');
|
|
|
|
|
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
|
|
|
|
|
throw new NotFoundHttpException('Invalid file path.');
|
|
|
|
|
}
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
|
|
|
|
|
if (!is_file($absolutePath)) {
|
|
|
|
|
throw new NotFoundHttpException('The specified path does not point to a file.');
|
|
|
|
|
}
|
|
|
|
|
// 计算文件的校验值
|
|
|
|
|
$crc32b = hash_file('crc32b', $absolutePath);
|
|
|
|
|
$sha256 = hash_file('sha256', $absolutePath);
|
|
|
|
|
|
|
|
|
|
// 将校验值返回给客户端
|
|
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
|
|
|
|
return [
|
|
|
|
|
'crc32b' => strtoupper($crc32b),
|
|
|
|
|
'sha256' => strtoupper($sha256),
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-03-20 16:26:03 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
|
*/
|
|
|
|
|
public function actionSearch(): Response
|
|
|
|
|
{
|
|
|
|
|
if (Yii::$app->request->isAjax) {
|
|
|
|
|
$directory = empty(Yii::$app->request->post('directory'))?'.':Yii::$app->request->post('directory');
|
|
|
|
|
$keyword = Yii::$app->request->post('keyword');
|
|
|
|
|
if ($keyword == null) {
|
|
|
|
|
return $this->asJson(['status' => 'error', 'message' => '关键词不能为空']);
|
|
|
|
|
}
|
|
|
|
|
if (strlen($keyword) < 3) {
|
|
|
|
|
return $this->asJson(['status' => 'error', 'message' => '关键词长度不得小于3']);
|
|
|
|
|
}
|
|
|
|
|
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $directory;
|
|
|
|
|
// check if the directory is valid & exists
|
|
|
|
|
if (!preg_match($this->pattern, $directory) || str_contains($directory, '..') || !is_dir($absolutePath)) {
|
|
|
|
|
return $this->asJson(['status' => 'error', 'message' => '无效路径']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$directoryIterator = new RecursiveDirectoryIterator($absolutePath);
|
|
|
|
|
$iterator = new RecursiveIteratorIterator($directoryIterator);
|
|
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
|
foreach ($iterator as $file) {
|
|
|
|
|
if (str_contains($file->getFilename(), $keyword)) {
|
|
|
|
|
$results[] = [
|
|
|
|
|
'name' => $file->getFilename(),
|
|
|
|
|
'type' => FileTypeDetector::detect($file->getPathname()),
|
|
|
|
|
'relativePath' => $directory.str_replace($absolutePath, '', $file->getPath())
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->asJson(['status' => 'success', 'data' => $results]);
|
|
|
|
|
}
|
|
|
|
|
return $this->asJson(['status' => 'error', 'message' => '非法请求']);
|
|
|
|
|
}
|
2024-02-10 17:41:07 +08:00
|
|
|
|
}
|