新增批量删除功能,修复故障的单文件删除功能

This commit is contained in:
Chenx221 2024-02-16 12:10:27 +08:00
parent 7459ef24b8
commit 1551514abf
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 47 additions and 26 deletions

View File

@ -210,36 +210,42 @@ class HomeController extends Controller
*/ */
public function actionDelete() public function actionDelete()
{ {
$relativePath = Yii::$app->request->post('relativePath'); $relativePaths = Yii::$app->request->post('relativePath');
$relativePath = rawurldecode($relativePath); if (!is_array($relativePaths)) {
if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { $relativePaths = [$relativePaths];
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.');
} else {
$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.');
}
} }
if (is_dir($absolutePath)) { foreach ($relativePaths as $relativePath) {
if (!$this->deleteDirectory($absolutePath)) { $relativePath = rawurldecode($relativePath);
Yii::$app->session->setFlash('error', 'Failed to delete directory.'); if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) {
} else { throw new NotFoundHttpException('Invalid file path.');
Yii::$app->session->setFlash('success', 'Directory deleted successfully.');
} }
} else { $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
if (!unlink($absolutePath)) { if (!file_exists($absolutePath)) {
Yii::$app->session->setFlash('error', 'Failed to delete file.'); throw new NotFoundHttpException('File or directory not found.');
} else { } else {
Yii::$app->session->setFlash('success', 'File deleted successfully.'); $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.');
}
}
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.');
}
} else {
if (!unlink($absolutePath)) {
Yii::$app->session->setFlash('error', 'Failed to delete file.');
} else {
Yii::$app->session->setFlash('success', 'File deleted successfully.');
}
} }
} }
return $this->redirect(['index', 'directory' => dirname($relativePath)]); return $this->redirect(['index', 'directory' => dirname($relativePaths[0])]);
} }
/** /**

View File

@ -150,8 +150,23 @@ $(document).on('click', '.single-share-btn', function () {
}); });
$(document).on('click', '.batch-delete-btn', function () { $(document).on('click', '.batch-delete-btn', function () {
console.log('删除按钮被点击'); var relativePaths = $('.select-item:checked').map(function () {
// 在这里添加你的代码 return $(this).data('relativePath');
}).get();
$.ajax({
type: "POST",
url: "index.php?r=home%2Fdelete",
data: { relativePath: relativePaths },
success: function(response) {
// 处理响应
location.reload();
},
error: function() {
// 处理错误
console.error('AJAX request failed.');
location.reload();
}
});
}); });
//下面的代码实现了各种按钮/样式功能,建议别看了( //下面的代码实现了各种按钮/样式功能,建议别看了(