fix: 修复扫描/探测/移动三个健壮性问题

- scanner: filepath.Ext 替代 path.Ext,修复 Windows 点号目录名扩展名解析错误
- scanner: Walk 遇不可访问路径仅告警跳过,不再整体中止扫描
- probe: ffprobe 探测增加 30s 超时,避免损坏文件卡死程序
- mover: copyFile 失败时清理半截目标文件,消除双重 Close
- 新增代码审查报告 bug20260814.md
This commit is contained in:
2025-04-07 21:00:37 +08:00
parent b812abd345
commit c266da2858
4 changed files with 126 additions and 8 deletions
+8 -4
View File
@@ -59,7 +59,7 @@ func moveOne(src, targetDir string) bool {
return true
}
// copyFile 逐块复制文件
// copyFile 逐块复制文件;失败时清理已写入的目标文件,避免残留半截文件
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
@@ -71,12 +71,16 @@ func copyFile(src, dst string) error {
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
out.Close()
_ = os.Remove(dst)
return err
}
return out.Close()
if err := out.Close(); err != nil {
_ = os.Remove(dst)
return err
}
return nil
}
// percent 计算百分比(0~100)
+8 -1
View File
@@ -2,10 +2,12 @@
package probe
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strconv"
"time"
)
// ProbeResult 媒体探测结果(图片/视频/音频通用)
@@ -39,9 +41,14 @@ type probeOutput struct {
Format formatInfo `json:"format"`
}
// ffprobeTimeout ffprobe 探测超时时间,超时视为探测失败(避免损坏文件卡死程序)
const ffprobeTimeout = 30 * time.Second
// ProbeMediaFile 用 ffprobe 探测媒体文件,一次调用同时获取视频/音频流与时长
func ProbeMediaFile(ffprobePath, filePath string) (*ProbeResult, error) {
cmd := exec.Command(ffprobePath, "-v", "error",
ctx, cancel := context.WithTimeout(context.Background(), ffprobeTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, ffprobePath, "-v", "error",
"-show_entries", "stream=index,codec_type,codec_name,width,height:format=duration,format_name",
"-of", "json", filePath)
output, err := cmd.Output()
+4 -3
View File
@@ -4,7 +4,6 @@ package scanner
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
@@ -41,7 +40,9 @@ func Scan(opts Options) (*Result, error) {
res := &Result{}
err := filepath.Walk(opts.RootDir, func(currentPath string, info os.FileInfo, err error) error {
if err != nil {
return err
// 单个路径访问失败(权限不足/损坏)不中止整体扫描,仅告警后跳过该子树
fmt.Printf("=== 扫描跳过(无法访问): %s, 错误: %v\n", currentPath, err)
return nil
}
if info.IsDir() {
if opts.SkipDir != "" && currentPath == opts.SkipDir {
@@ -57,7 +58,7 @@ func Scan(opts Options) (*Result, error) {
res.Files = append(res.Files, File{
Path: currentPath,
Name: info.Name(),
Ext: path.Ext(currentPath),
Ext: filepath.Ext(currentPath),
})
return nil
})