download_test.go
| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "vidarchive/internal/config" |
| 11 | "vidarchive/internal/models" |
| 12 | ) |
| 13 | |
| 14 | func TestSanitizeDirName(t *testing.T) { |
| 15 | tests := []struct{ in, want string }{ |
| 16 | {"normal title", "normal title"}, |
| 17 | {"a/b:c*d?e\"f<g>h|i", "a-b-c-d-e-f-g-h-i"}, |
| 18 | {" trimmed ", "trimmed"}, |
| 19 | {"", "untitled"}, |
| 20 | {"///", "---"}, |
| 21 | } |
| 22 | for _, tc := range tests { |
| 23 | if got := sanitizeDirName(tc.in); got != tc.want { |
| 24 | t.Errorf("sanitizeDirName(%q) = %q, want %q", tc.in, got, tc.want) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestUniqueDir(t *testing.T) { |
| 30 | base := t.TempDir() |
| 31 | svc := &DownloadService{} |
| 32 | |
| 33 | first := svc.uniqueDir(base, "item") |
| 34 | if filepath.Base(first) != "item" { |
| 35 | t.Errorf("first uniqueDir = %q, want .../item", first) |
| 36 | } |
| 37 | if err := os.MkdirAll(first, 0755); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | second := svc.uniqueDir(base, "item") |
| 41 | if filepath.Base(second) != "item-1" { |
| 42 | t.Errorf("second uniqueDir = %q, want .../item-1", second) |
| 43 | } |
| 44 | if err := os.MkdirAll(second, 0755); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | third := svc.uniqueDir(base, "item") |
| 48 | if filepath.Base(third) != "item-2" { |
| 49 | t.Errorf("third uniqueDir = %q, want .../item-2", third) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestDeriveItemName(t *testing.T) { |
| 54 | svc := &DownloadService{} |
| 55 | itemDir := t.TempDir() |
| 56 | |
| 57 | // info.json title wins and is sanitized. |
| 58 | infoPath := filepath.Join(itemDir, "info.json") |
| 59 | if err := os.WriteFile(infoPath, []byte(`{"title":"Cool: Video"}`), 0644); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | bigName := "big.mp4" |
| 63 | if err := os.WriteFile(filepath.Join(itemDir, bigName), []byte("xxxxxxxxxx"), 0644); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | media := []os.DirEntry{dirEntry(t, itemDir, bigName)} |
| 67 | if got := svc.deriveItemName(itemDir, infoPath, media); got != "Cool- Video" { |
| 68 | t.Errorf("deriveItemName with info.json = %q, want %q", got, "Cool- Video") |
| 69 | } |
| 70 | |
| 71 | // Without info.json, falls back to the largest media file's stem. |
| 72 | small := "small.mp4" |
| 73 | if err := os.WriteFile(filepath.Join(itemDir, small), []byte("x"), 0644); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | media = []os.DirEntry{dirEntry(t, itemDir, small), dirEntry(t, itemDir, bigName)} |
| 77 | if got := svc.deriveItemName(itemDir, "", media); got != "big" { |
| 78 | t.Errorf("deriveItemName fallback = %q, want %q (largest file stem)", got, "big") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func dirEntry(t *testing.T, dir, name string) os.DirEntry { |
| 83 | t.Helper() |
| 84 | entries, err := os.ReadDir(dir) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | for _, e := range entries { |
| 89 | if e.Name() == name { |
| 90 | return e |
| 91 | } |
| 92 | } |
| 93 | t.Fatalf("dir entry %q not found", name) |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func TestParseFormatList(t *testing.T) { |
| 98 | output := `[info] Available formats: |
| 99 | ID EXT RESOLUTION FPS |
| 100 | 18 mp4 640x360 30 |
| 101 | 137 mp4 1920x1080 60 |
| 102 | 140 m4a audio only |
| 103 | ` |
| 104 | formats := parseFormatList(output) |
| 105 | if len(formats) != 3 { |
| 106 | t.Fatalf("expected 3 formats, got %d: %+v", len(formats), formats) |
| 107 | } |
| 108 | if formats[0].ID != "18" || formats[0].Ext != "mp4" { |
| 109 | t.Errorf("format[0] = %+v", formats[0]) |
| 110 | } |
| 111 | if formats[1].Resolution != "1920x1080" { |
| 112 | t.Errorf("format[1] resolution = %q, want 1920x1080", formats[1].Resolution) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // TestImportItemDir exercises the full import: media + info.json + subtitles get |
| 117 | // sorted into a named item directory with a marker. Uses ffmpeg to produce real |
| 118 | // media so the mimetype-based classification in importItemDir matches. |
| 119 | func TestImportItemDir(t *testing.T) { |
| 120 | requireFFmpeg(t) |
| 121 | |
| 122 | libDir := t.TempDir() |
| 123 | svc := &DownloadService{cfg: &config.Config{LibraryDir: libDir}} |
| 124 | |
| 125 | src := t.TempDir() |
| 126 | makeTestVideo(t, filepath.Join(src, "raw.mp4")) |
| 127 | if err := os.WriteFile(filepath.Join(src, "info.json"), []byte(`{"title":"My Clip"}`), 0644); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | if err := os.WriteFile(filepath.Join(src, "raw.en.srt"), []byte("1\n00:00:00,000 --> 00:00:01,000\nhi\n"), 0644); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | |
| 134 | if err := svc.importItemDir("https://example.com/v", src, libDir); err != nil { |
| 135 | t.Fatalf("importItemDir: %v", err) |
| 136 | } |
| 137 | |
| 138 | targetDir := filepath.Join(libDir, "My Clip") |
| 139 | if _, err := os.Stat(filepath.Join(targetDir, itemMarkerName)); err != nil { |
| 140 | t.Errorf("marker not created: %v", err) |
| 141 | } |
| 142 | if _, err := os.Stat(filepath.Join(targetDir, "info.json")); err != nil { |
| 143 | t.Errorf("info.json not moved: %v", err) |
| 144 | } |
| 145 | if _, err := os.Stat(filepath.Join(targetDir, "raw.mp4")); err != nil { |
| 146 | t.Errorf("media not moved: %v", err) |
| 147 | } |
| 148 | if _, err := os.Stat(filepath.Join(targetDir, subtitlesDirName, "raw.en.srt")); err != nil { |
| 149 | t.Errorf("subtitle not moved into subtitles dir: %v", err) |
| 150 | } |
| 151 | |
| 152 | // The marker should record the source URL. |
| 153 | markerData, _ := os.ReadFile(filepath.Join(targetDir, itemMarkerName)) |
| 154 | if want := "https://example.com/v"; !strings.Contains(string(markerData), want) { |
| 155 | t.Errorf("marker missing source url %q: %s", want, markerData) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestImportDownloadedItemsRejectsOutputTraversal(t *testing.T) { |
| 160 | libDir := t.TempDir() |
| 161 | svc := &DownloadService{cfg: &config.Config{LibraryDir: libDir}} |
| 162 | |
| 163 | // A temp download dir with one item subdir. |
| 164 | tempDir := t.TempDir() |
| 165 | itemDir := filepath.Join(tempDir, "item-00001") |
| 166 | if err := os.MkdirAll(itemDir, 0755); err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | |
| 170 | d := &models.Download{ |
| 171 | URL: "u", |
| 172 | OutputDir: sql.NullString{String: "../escape", Valid: true}, |
| 173 | } |
| 174 | if err := svc.importDownloadedItems(d, tempDir); err == nil { |
| 175 | t.Error("expected path-traversal output dir to be rejected") |
| 176 | } |
| 177 | } |
| 178 |