diff --git a/config/ytdlp.go b/config/ytdlp.go index 0f9d6b66..ade3c38d 100644 --- a/config/ytdlp.go +++ b/config/ytdlp.go @@ -1,7 +1,9 @@ package config // DefaultYtdlpFilenameTemplate is used when ytdlp.filename_template is not set. -const DefaultYtdlpFilenameTemplate = "%(title)s.%(ext)s" +// The title is capped at 200 bytes so names stay under the 255-byte NAME_MAX +// limit, with room for suffixes such as ".f140.m4a.part". +const DefaultYtdlpFilenameTemplate = "%(title).200B.%(ext)s" type YtdlpConfig struct { // MaxHeight limits the video resolution by height in pixels (e.g. 1080, 720). diff --git a/core/tasks/ytdlp/output_test.go b/core/tasks/ytdlp/output_test.go new file mode 100644 index 00000000..d8652ed7 --- /dev/null +++ b/core/tasks/ytdlp/output_test.go @@ -0,0 +1,71 @@ +package ytdlp + +import ( + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/krau/SaveAny-Bot/config" +) + +// TestDefaultFilenameTemplateLongTitle reproduces "[Errno 36] Filename too long" for +// posts whose caption becomes the title. It needs a local yt-dlp and only +// renders the template from an info JSON, so it never touches the network. +func TestDefaultFilenameTemplateLongTitle(t *testing.T) { + bin, err := exec.LookPath("yt-dlp") + if err != nil { + t.Skip("yt-dlp not installed") + } + + cases := []struct { + name string + title string + }{ + {name: "ascii", title: strings.Repeat("Cita-cita_terbesar_kita_", 20)}, + {name: "multibyte", title: strings.Repeat("آية الكرسي ", 40)}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + info := filepath.Join(dir, "info.json") + writeInfoJSON(t, info, tc.title) + + out, err := exec.Command(bin, "--ignore-config", "--load-info-json", info, + "-o", filepath.Join(dir, config.DefaultYtdlpFilenameTemplate), "--print", "filename").Output() + if err != nil { + t.Fatalf("yt-dlp: %v", err) + } + name := filepath.Base(strings.TrimSpace(string(out))) + // ".part" and format ids (".f140") are appended later; keep room for them. + if got := len(name + ".f140.part"); got > 255 { + t.Fatalf("filename is %d bytes, want <= 255: %q", got, name) + } + if !strings.HasSuffix(name, ".m4a") { + t.Fatalf("extension lost: %q", name) + } + }) + } +} + +func writeInfoJSON(t *testing.T, path, title string) { + t.Helper() + raw, err := json.Marshal(map[string]any{ + "id": "abc123", + "title": title, + "ext": "m4a", + "extractor": "generic", + "extractor_key": "Generic", + "webpage_url": "https://example.invalid/post", + "url": "https://example.invalid/media.m4a", + "formats": []map[string]any{{"format_id": "0", "url": "https://example.invalid/media.m4a", "ext": "m4a"}}, + }) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, raw, 0o600); err != nil { + t.Fatal(err) + } +} diff --git a/docs/content/en/usage/ytdlp.md b/docs/content/en/usage/ytdlp.md index faee2a08..4b5ece49 100644 --- a/docs/content/en/usage/ytdlp.md +++ b/docs/content/en/usage/ytdlp.md @@ -42,7 +42,7 @@ For safety, flags that execute programs, load external configuration, or read an ## File names -Downloaded files are named after the video title (`%(title)s.%(ext)s`) and saved into the storage directory you selected. Pass a yt-dlp output template to customize the name: +Downloaded files are named after the video title, cut to 200 bytes so long titles stay within file name limits (`%(title).200B.%(ext)s`), and saved into the storage directory you selected. Pass a yt-dlp output template to customize the name: ```bash /download https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s" diff --git a/docs/content/zh/usage/ytdlp.md b/docs/content/zh/usage/ytdlp.md index 4ac0afb9..36c29340 100644 --- a/docs/content/zh/usage/ytdlp.md +++ b/docs/content/zh/usage/ytdlp.md @@ -42,7 +42,7 @@ weight: 7 ## 文件命名 -下载文件默认按视频标题命名(`%(title)s.%(ext)s`),保存到你选择的存储目录。可以传入 yt-dlp 输出模板自定义文件名: +下载文件默认按视频标题命名,标题截断至 200 字节以免超出文件名长度限制(`%(title).200B.%(ext)s`),保存到你选择的存储目录。可以传入 yt-dlp 输出模板自定义文件名: ```bash /download https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s" diff --git a/web/ui/src/settingsFields.ts b/web/ui/src/settingsFields.ts index ed648369..33193463 100644 --- a/web/ui/src/settingsFields.ts +++ b/web/ui/src/settingsFields.ts @@ -192,7 +192,7 @@ export const settingsGroups: SettingsGroupDef[] = [ path: 'filename_template', label: 'settings.ytdlp.filenameTemplate', kind: 'text', - placeholder: '%(title)s.%(ext)s', + placeholder: '%(title).200B.%(ext)s', }, { path: 'restrict_filenames',