diff --git a/core/bmp.go b/core/bmp.go index c3848b3..cf0b005 100644 --- a/core/bmp.go +++ b/core/bmp.go @@ -47,8 +47,7 @@ func ReadBmpImage(filePath string) (error, int32, int32) { // Check if it's a valid BMP file by verifying the file type. if string(fileHeader.FileType[:]) != "BM" { - fmt.Printf("=== Not a valid BMP file: %v\n", err) - return err, 0, 0 + return fmt.Errorf("not a valid BMP file: file type is %q, expected \"BM\"", string(fileHeader.FileType[:])), 0, 0 } // Read the info header. diff --git a/core/file_scanner.go b/core/file_scanner.go index 13f8169..95eb4cf 100644 --- a/core/file_scanner.go +++ b/core/file_scanner.go @@ -34,7 +34,12 @@ func doReadFileMimeInfo() { bar.SetNoticeColor(goPrint.FontColor.Red) for _, currentPath := range vars.GlobalFilePathList { // mime - vars.GlobalFilePath2MimeInfoMap[currentPath] = util.ReadFileMimeInfo(currentPath).String() + mt := util.ReadFileMimeInfo(currentPath) + if mt != nil { + vars.GlobalFilePath2MimeInfoMap[currentPath] = mt.String() + } else { + vars.GlobalFilePath2MimeInfoMap[currentPath] = "" + } count = count + 1 bar.PrintBar(util.CalcPercentage(count, total)) } diff --git a/core/handle_image.go b/core/handle_image.go index 1d48be9..26f7b7f 100644 --- a/core/handle_image.go +++ b/core/handle_image.go @@ -114,6 +114,7 @@ func doPickImageFile(uid string, rootDir string, imagePath2WidthHeightMap map[st moveHorizontalImage(rootDir, uid) moveVerticalImage(rootDir, uid) moveSquareImage(rootDir, uid) + moveGifImage(rootDir, uid) } // 统一处理gif @@ -255,7 +256,7 @@ func handleSquareImage(currentImagePath string, width int) { vars.ImageSquare8KList = append(vars.ImageSquare8KList, currentImagePath) } else if width < 10000 { vars.ImageSquare9KList = append(vars.ImageSquare9KList, currentImagePath) - } else if width > 10000 { + } else if width >= 10000 { vars.ImageSquareHKList = append(vars.ImageSquareHKList, currentImagePath) } } @@ -498,3 +499,13 @@ func moveSquareImage(rootDir string, uid string) { util.DoMoveFileToDir(vars.ImageSquareHKList, imageDirPath) } } + +// 移动gif图片 +func moveGifImage(rootDir string, uid string) { + if len(vars.ImageGifList) > 0 { + pathSeparator := string(os.PathSeparator) + imageDirPath := rootDir + pathSeparator + uid + "-图片_GIF" + util.CreateDir(imageDirPath) + util.DoMoveFileToDir(vars.ImageGifList, imageDirPath) + } +} diff --git a/core/handle_video.go b/core/handle_video.go index 931c635..839cc80 100644 --- a/core/handle_video.go +++ b/core/handle_video.go @@ -102,6 +102,7 @@ func doPickVideoFile(rootDir string, videoPath2WidthHeightMap map[string]string) moveHorizontalVideo(uid, rootDir) moveVerticalVideo(uid, rootDir) moveSquareVideo(uid, rootDir) + moveGifVideo(uid, rootDir) } // 移动垂直视频 @@ -324,6 +325,26 @@ func moveSquareVideo(uid string, rootDir string) { } } +// 移动gif视频 +func moveGifVideo(uid string, rootDir string) { + pathSeparator := string(os.PathSeparator) + if len(vars.VideoHorizontalGifList) > 0 { + videoDirPath := rootDir + pathSeparator + uid + "-视频_横屏_GIF" + util.CreateDir(videoDirPath) + util.DoMoveFileToDir(vars.VideoHorizontalGifList, videoDirPath) + } + if len(vars.VideoVerticalGifList) > 0 { + videoDirPath := rootDir + pathSeparator + uid + "-视频_竖屏_GIF" + util.CreateDir(videoDirPath) + util.DoMoveFileToDir(vars.VideoVerticalGifList, videoDirPath) + } + if len(vars.VideoSquareGifList) > 0 { + videoDirPath := rootDir + pathSeparator + uid + "-视频_等比_GIF" + util.CreateDir(videoDirPath) + util.DoMoveFileToDir(vars.VideoSquareGifList, videoDirPath) + } +} + // 移动普通视频 func moveNormalVideo(uid string, rootDir string) { pathSeparator := string(os.PathSeparator) @@ -370,7 +391,7 @@ func handleVerticalVideo(currentVideoPath string, height int, suffix string) { vars.VideoVertical8KList = append(vars.VideoVertical8KList, currentVideoPath) } else if height < 10000 { vars.VideoVertical9KList = append(vars.VideoVertical9KList, currentVideoPath) - } else if height > 10000 { + } else if height >= 10000 { vars.VideoVerticalHKList = append(vars.VideoVerticalHKList, currentVideoPath) } } @@ -488,7 +509,7 @@ func handleSquareVideo(currentVideoPath string, width int, height int, suffix st vars.VideoSquare8KList = append(vars.VideoSquare8KList, currentVideoPath) } else if width < 10000 { vars.VideoSquare9KList = append(vars.VideoSquare9KList, currentVideoPath) - } else if width > 10000 { + } else if width >= 10000 { vars.VideoSquareHKList = append(vars.VideoSquareHKList, currentVideoPath) } } diff --git a/util/file.go b/util/file.go index 422ee2a..8a7a259 100644 --- a/util/file.go +++ b/util/file.go @@ -1,16 +1,16 @@ package util import ( + "fmt" "github.com/gabriel-vasile/mimetype" "io/ioutil" - "log" "os" "path/filepath" ) -// CreateDir 创建目录 +// CreateDir 创建目录(含递归创建父目录) func CreateDir(dirPath string) bool { - err := os.Mkdir(dirPath, 0755) + err := os.MkdirAll(dirPath, 0755) return err == nil } @@ -20,11 +20,12 @@ func GetFileDirectory(filePath string) string { return directoryPath } -// ReadFileMimeInfo 获取文件mime信息 +// ReadFileMimeInfo 获取文件mime信息,失败时返回nil func ReadFileMimeInfo(filepath string) *mimetype.MIME { mt, err := mimetype.DetectFile(filepath) if err != nil { - log.Fatal(err) + fmt.Printf("=== 读取Mime信息失败: %s, 错误: %v\n", filepath, err) + return nil } return mt } diff --git a/util/video.go b/util/video.go index 5c48861..2e469c9 100644 --- a/util/video.go +++ b/util/video.go @@ -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() }