refactor: GIF 单独归类、跨文件系统复制回退与异常处理优化
This commit is contained in:
+35
-3
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/redmask-hb/GoSimplePrint/goPrint"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -147,12 +148,43 @@ func DoMoveFileToDir(filePathList []string, videoDirPath string) {
|
||||
bar.PrintEnd("=== Finish")
|
||||
}
|
||||
|
||||
// 移动文件到目录
|
||||
// 移动文件到目录,跨文件系统时自动 fallback 为 copy+delete
|
||||
func moveFileToDir(sourceFilePath string, targetDirectory string) bool {
|
||||
splits := strings.Split(sourceFilePath, string(os.PathSeparator))
|
||||
fileName := splits[len(splits)-1]
|
||||
targetFilePath := targetDirectory + fileName
|
||||
err := os.Rename(sourceFilePath, targetFilePath)
|
||||
//fmt.Printf("=== 移动文件, 源: %s, 目标: %s \n", sourceFilePath, targetFilePath)
|
||||
return err == nil
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
// 跨文件系统等场景:fallback copy + delete
|
||||
if err := copyFile(sourceFilePath, targetFilePath); err != nil {
|
||||
fmt.Printf("=== 复制文件失败, 源: %s, 目标: %s, 错误: %v\n", sourceFilePath, targetFilePath, err)
|
||||
return false
|
||||
}
|
||||
if err := os.Remove(sourceFilePath); err != nil {
|
||||
fmt.Printf("=== 删除源文件失败: %s, 错误: %v\n", sourceFilePath, err)
|
||||
// 复制成功但删除失败,文件已存在于两处,返回 true 表示目标已就位
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// copyFile 逐块复制文件
|
||||
func copyFile(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if _, err := io.Copy(out, in); err != nil {
|
||||
return err
|
||||
}
|
||||
return out.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user