feat: 添加复制相关逻辑,修复文件夹剪切无法正常执行的错误
This commit is contained in:
1 parent
089dc68419
commit
cb8198d1bb
12 files changed
+418
-5
No files matched your search
@@ -29,6 +29,15 @@ func CopyHandler(fsOperateEventCenter *fsOperateEventCenter.FsOperateEventCenter
|
||||
|
||||
newPath := filepath.Join(staticPath, req.NewPath, filepath.Base(decodedPath))
|
||||
eventId := fsOperateEventCenter.Push(func() error {
|
||||
// 检查源路径是文件还是目录
|
||||
fileInfo, err := os.Stat(decodedPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() {
|
||||
return copyDir(decodedPath, newPath)
|
||||
}
|
||||
return copyFile(decodedPath, newPath)
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"eventId": eventId})
|
||||
@@ -55,3 +64,43 @@ func copyFile(src, dst string) error {
|
||||
|
||||
return destFile.Sync()
|
||||
}
|
||||
|
||||
// 复制目录及其内容
|
||||
func copyDir(src, dst string) error {
|
||||
// 获取源目录信息
|
||||
srcInfo, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建目标目录
|
||||
if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 读取源目录内容
|
||||
entries, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 遍历并复制每个条目
|
||||
for _, entry := range entries {
|
||||
srcPath := filepath.Join(src, entry.Name())
|
||||
dstPath := filepath.Join(dst, entry.Name())
|
||||
|
||||
if entry.IsDir() {
|
||||
// 递归复制子目录
|
||||
if err = copyDir(srcPath, dstPath); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// 复制文件
|
||||
if err = copyFile(srcPath, dstPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user