fix: 重复文件归类、图片探测与明暗分析健壮性

- organize/store: 按 MD5 去重,重复图片/视频/音频统一移入「重复文件」目录,不再覆盖已有记录
- probe/image: webp/bmp/psd/psb 专用解码失败后走 ffprobe 兜底,与通用路径一致
- probe/brightness: 大图内存缩放为缩略图再分析明暗,超4000万像素跳过分析防OOM
- README: 补充重复文件目录说明,修正 Go 版本为 1.25.0
This commit is contained in:
2025-04-12 21:02:16 +08:00
parent 39fe02b424
commit a667f7fa35
5 changed files with 162 additions and 38 deletions
+29 -10
View File
@@ -32,35 +32,54 @@ func IsSupportImage(ext string) bool {
}
// ProbeImage 统一探测图片尺寸:
// 1. 特殊格式(webp/bmp/psd/psb)走专用解码器
// 2. 其余支持格式走 Go 标准库 image.DecodeConfig
// 1. 特殊格式(webp/bmp/psd/psb)走专用解码器,失败后用 ffprobe 兜底
// 2. 其余白名单格式走 Go 标准库 image.DecodeConfig
// 3. 标准库失败时用 ffprobe 兜底(ffmpeg 支持的图片格式更广,如 tiff/svg 等)
//
// ffprobePath 为空表示不启用 ffprobe 兜底;扩展名不属于支持格式时返回错误。
// ffprobePath 为空表示不启用 ffprobe 兜底;非白名单扩展名(如 tiff)在 ffprobe 可用时兜底探测。
func ProbeImage(filePath, ffprobePath string) (*ProbeResult, error) {
ext := strings.ToLower(filepath.Ext(filePath))
switch ext {
case ".webp":
return probeWebp(filePath)
if res, err := probeWebp(filePath); err == nil {
return res, nil
}
case ".bmp":
return probeBmp(filePath)
if res, err := probeBmp(filePath); err == nil {
return res, nil
}
case ".psd", ".psb":
return probePsd(filePath)
if res, err := probePsd(filePath); err == nil {
return res, nil
}
}
if !IsSupportImage(ext) {
// 非白名单扩展名(tiff/svg 等):ffprobe 可用时兜底探测,否则视为不支持
if res, err := probeByFFprobe(ffprobePath, filePath); err == nil {
return res, nil
}
return nil, fmt.Errorf("不支持的图片格式: %s", ext)
}
if w, h, err := decodeConfig(filePath); err == nil {
return &ProbeResult{Width: w, Height: h}, nil
}
if ffprobePath != "" {
if res, err := ProbeMediaFile(ffprobePath, filePath); err == nil && res.HasVideo {
return res, nil
}
if res, err := probeByFFprobe(ffprobePath, filePath); err == nil {
return res, nil
}
return nil, fmt.Errorf("无法读取图片尺寸: %s", filePath)
}
// probeByFFprobe 用 ffprobe 兜底探测图片尺寸;ffprobePath 为空或探测失败时返回错误
func probeByFFprobe(ffprobePath, filePath string) (*ProbeResult, error) {
if ffprobePath == "" {
return nil, fmt.Errorf("未启用 ffprobe 兜底")
}
if res, err := ProbeMediaFile(ffprobePath, filePath); err == nil && res.HasVideo {
return res, nil
}
return nil, fmt.Errorf("ffprobe 兜底失败: %s", filePath)
}
// decodeConfig 用 Go 标准库读取图片宽高
func decodeConfig(filePath string) (int, int, error) {
f, err := os.Open(filePath)