Files
kenaito-media-picker/core/file_scanner.go
T
odboy f5e3f6b4ef fix: 修复媒体整理中的文件覆盖、BMP解析与重复整理问题
- 移动文件改用 uuid+原文件名,避免同名覆盖丢失数据
- BMP 负高度取绝对值,自顶向下格式解析正确
- 新增 {目录名}-picker 白名单输出目录,整理后跳过重复扫描
- 分类目录名去掉 uuid 前缀,重复运行并入已有目录
2025-03-15 21:17:24 +08:00

102 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package core
import (
"fmt"
"github.com/redmask-hb/GoSimplePrint/goPrint"
"kenaito-media-picker/util"
"kenaito-media-picker/vars"
"log"
"os"
"path"
"path/filepath"
"strings"
)
type FileScanner struct {
}
func (FileScanner) DoScan(rootDir string) {
fmt.Printf("=== 开始扫描文件 \n")
if err := filepath.Walk(rootDir, visit); err != nil {
log.Fatal(err)
}
doReadFileMimeInfo()
}
func doReadFileMimeInfo() {
total := len(vars.GlobalFilePathList)
fmt.Printf("=== 文件总数: %d \n", total)
// 扫描文件mime信息
var count = 0
bar := goPrint.NewBar(100)
bar.SetNotice("=== 扫描文件Mime:")
bar.SetGraph(">")
bar.SetNoticeColor(goPrint.FontColor.Red)
for _, currentPath := range vars.GlobalFilePathList {
// mime
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))
}
bar.PrintEnd("=== Finish")
}
func (FileScanner) DoFilter() {
total := len(vars.GlobalFilePathList)
var count = 0
bar := goPrint.NewBar(100)
bar.SetNotice("=== 过滤已支持的媒体:")
bar.SetGraph(">")
for _, globalFilePath := range vars.GlobalFilePathList {
fileMime := vars.GlobalFilePath2MimeInfoMap[globalFilePath]
count = count + 1
if strings.Contains(fileMime, "video/") { // 视频
vars.GlobalVideoPathList = append(vars.GlobalVideoPathList, globalFilePath)
bar.PrintBar(util.CalcPercentage(count, total))
continue
}
// mime格式为application/octet-stream的视频
ext := path.Ext(globalFilePath)
if util.IsSupportVideo(ext) {
vars.GlobalVideoPathList = append(vars.GlobalVideoPathList, globalFilePath)
bar.PrintBar(util.CalcPercentage(count, total))
continue
}
if strings.Contains(fileMime, "image/") { // 图片
vars.GlobalImagePathList = append(vars.GlobalImagePathList, globalFilePath)
bar.PrintBar(util.CalcPercentage(count, total))
continue
}
if util.IsSupportImage(ext) {
vars.GlobalImagePathList = append(vars.GlobalImagePathList, globalFilePath)
bar.PrintBar(util.CalcPercentage(count, total))
continue
}
// 其他的文件不处理
}
bar.PrintEnd("=== Finish")
}
// 定义walkFn回调函数visit
func visit(currentPath string, info os.FileInfo, err error) error {
if err != nil {
return err // 如果有错误,直接返回
}
if info.IsDir() && vars.GlobalPickerDir != "" && currentPath == vars.GlobalPickerDir {
return filepath.SkipDir // 跳过白名单目录(已整理文件的输出目录)
}
if !info.IsDir() {
vars.GlobalFilePathList = append(vars.GlobalFilePathList, currentPath)
// filename, include ext
vars.GlobalFilePath2FileNameMap[currentPath] = filepath.Base(currentPath)
// file ext
vars.GlobalFilePath2FileExtMap[currentPath] = path.Ext(currentPath)
}
return nil
}