实现复制粘贴功能
This commit is contained in:
parent
1551514abf
commit
b09d2e6c36
@ -43,6 +43,7 @@ class HomeController extends Controller
|
||||
'multi-ff-zip-dl' => ['POST'],
|
||||
'zip' => ['POST'],
|
||||
'unzip' => ['POST'],
|
||||
'paste' => ['POST'],
|
||||
],
|
||||
],
|
||||
]
|
||||
@ -483,6 +484,10 @@ class HomeController extends Controller
|
||||
return Yii::$app->response->sendFile($zipPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionZip()
|
||||
{
|
||||
$model = new ZipForm();
|
||||
@ -561,4 +566,177 @@ class HomeController extends Controller
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function actionPaste()
|
||||
{
|
||||
// 获取请求中的操作类型、相对路径和目标目录
|
||||
$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)) {
|
||||
$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') {
|
||||
// 如果是复制操作
|
||||
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 操作是否成功
|
||||
*/
|
||||
protected function copyDirectory($source, $destination)
|
||||
{
|
||||
// 创建目标目录
|
||||
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 操作是否成功
|
||||
*/
|
||||
protected function moveDirectory($source, $destination)
|
||||
{
|
||||
// 创建目标目录
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -123,20 +123,48 @@ $(document).on('click', '.single-rename-btn', function () {
|
||||
var relativePath = $('.select-item:checked').first().data('relativePath');
|
||||
$('.rename-btn[value="' + relativePath + '"]').trigger('click');
|
||||
});
|
||||
|
||||
// 当用户点击复制按钮时
|
||||
$(document).on('click', '.batch-copy-btn', function () {
|
||||
console.log('复制按钮被点击');
|
||||
// 在这里添加你的代码
|
||||
var relativePaths = $('.select-item:checked').map(function () {
|
||||
return $(this).data('relativePath');
|
||||
}).get();
|
||||
sessionStorage.setItem('operation', 'copy');
|
||||
sessionStorage.setItem('relativePaths', JSON.stringify(relativePaths));
|
||||
updateButtons(); // 更新按钮的状态
|
||||
});
|
||||
|
||||
// 当用户点击剪切按钮时
|
||||
$(document).on('click', '.batch-cut-btn', function () {
|
||||
console.log('剪切按钮被点击');
|
||||
// 在这里添加你的代码
|
||||
var relativePaths = $('.select-item:checked').map(function () {
|
||||
return $(this).data('relativePath');
|
||||
}).get();
|
||||
sessionStorage.setItem('operation', 'cut');
|
||||
sessionStorage.setItem('relativePaths', JSON.stringify(relativePaths));
|
||||
updateButtons(); // 更新按钮的状态
|
||||
});
|
||||
|
||||
// 当用户点击粘贴按钮时
|
||||
$(document).on('click', '.batch-paste-btn', function () {
|
||||
console.log('粘贴按钮被点击');
|
||||
// 在这里添加你的代码
|
||||
var operation = sessionStorage.getItem('operation');
|
||||
var relativePaths = JSON.parse(sessionStorage.getItem('relativePaths'));
|
||||
var targetDirectory = $('#target-dir').val();
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "index.php?r=home%2Fpaste",
|
||||
data: { operation: operation, relativePaths: relativePaths, targetDirectory: targetDirectory },
|
||||
success: function(response) {
|
||||
// 处理响应
|
||||
location.reload();
|
||||
},
|
||||
error: function() {
|
||||
// 处理错误
|
||||
console.error('AJAX request failed.');
|
||||
}
|
||||
});
|
||||
sessionStorage.removeItem('operation'); // 清除 sessionStorage 中的 operation
|
||||
sessionStorage.removeItem('relativePaths'); // 清除 sessionStorage 中的 relativePaths
|
||||
hasCopiedOrCut = false; // 设置 hasCopiedOrCut 为 false
|
||||
updateButtons(); // 更新按钮的状态
|
||||
});
|
||||
|
||||
$(document).on('click', '.calc-sum-btn', function () {
|
||||
@ -302,7 +330,7 @@ function updateButtons() {
|
||||
var count = checkboxes.length;
|
||||
var isSingleFile = count === 1 && !checkboxes.first().data('isDirectory');
|
||||
var isSingleZip = isSingleFile && checkboxes.first().closest('tr').find('.file_icon').hasClass('fa-file-zipper');
|
||||
var hasCopiedOrCut = false/* 判断是否有复制或剪切的项,你需要自己实现这部分逻辑 */;
|
||||
var hasOperation = sessionStorage.getItem('operation') !== null; // 检查 sessionStorage 中是否存在 operation
|
||||
|
||||
$('.single-download-btn').toggle(isSingleFile);
|
||||
$('.batch-zip-download-btn').toggle(count > 0 && !isSingleFile);
|
||||
@ -311,7 +339,7 @@ function updateButtons() {
|
||||
$('.single-rename-btn').toggle(count === 1);
|
||||
$('.batch-copy-btn').toggle(count >= 1);
|
||||
$('.batch-cut-btn').toggle(count >= 1);
|
||||
$('.batch-paste-btn').toggle(hasCopiedOrCut);
|
||||
$('.batch-paste-btn').toggle(hasOperation); // 根据 hasOperation 的值来决定是否显示粘贴按钮
|
||||
$('.calc-sum-btn').toggle(isSingleFile);
|
||||
$('.single-share-btn').toggle(count === 1);
|
||||
$('.batch-delete-btn').toggle(count >= 1);
|
||||
|
Loading…
Reference in New Issue
Block a user