Skip to content

fix(files): detectSubtitles 用 path.Dir 取父目录而非 TrimRight 字符集#233

Open
pengpeng wants to merge 1 commit intomainfrom
cursor/fix-files-subtitles-parent-dir
Open

fix(files): detectSubtitles 用 path.Dir 取父目录而非 TrimRight 字符集#233
pengpeng wants to merge 1 commit intomainfrom
cursor/fix-files-subtitles-parent-dir

Conversation

@pengpeng
Copy link
Copy Markdown
Member

@pengpeng pengpeng commented May 5, 2026

概要

`pkg/files/file.go` 的 `detectSubtitles` 用:

```go
parentDir := strings.TrimRight(i.Path, i.Name)
```

`strings.TrimRight` 的第二个参数是 字符集(cutset of runes),不是字符串后缀。对一个 video 文件 `/foo/bar/baz.mp4`、`Name = "baz.mp4"`:

  • 字符集 = `{'b','a','z','.','m','p','4'}`
  • 从右往左剥所有命中字符集的 rune
  • 结果可能是 `"/foo/"`,或者对其它文件名是更糟糕的串

随后的 `afero.ReadDir(i.Fs, parentDir)` 要么读到错误的目录、要么直接报错——几乎所有真实视频文件都拿不到字幕检测结果

改动

用 `path.Dir(i.Path)` 取真正的父目录,并加注释说明历史 bug。

验证方式

  • 在某个目录下放 `movie.mp4` + `movie.en.vtt`,访问 `movie.mp4` 的 file info,确认 `Subtitles` 字段包含 `movie.en.vtt` 的路径。
  • 视频文件名包含父目录名中的字符(如 `/abc/abcdef.mp4`)时,字幕检测仍然落在 `/abc`,而非被乱剥成奇怪的路径。

Made with Cursor

detectSubtitles built parentDir as
    strings.TrimRight(i.Path, i.Name)

but TrimRight treats its second argument as a *cutset of runes*, not
a suffix. For a video at "/foo/bar/baz.mp4" with Name "baz.mp4", it
strips every trailing rune that appears in the set
{'b','a','z','.','m','p','4'}, producing something like "/foo/" -
or, for many filenames, a totally wrong path. The subsequent
ReadDir then either failed or scanned the wrong directory, so
adjacent .vtt subtitle files were almost never detected.

Use path.Dir(i.Path), which actually returns the parent directory.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant