download_test.go
| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "syscall" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "vidarchive/internal/config" |
| 17 | "vidarchive/internal/models" |
| 18 | ) |
| 19 | |
| 20 | func TestSanitizeDirName(t *testing.T) { |
| 21 | tests := []struct{ in, want string }{ |
| 22 | {"normal title", "normal title"}, |
| 23 | {"a/b:c*d?e\"f<g>h|i", "a-b-c-d-e-f-g-h-i"}, |
| 24 | {" trimmed ", "trimmed"}, |
| 25 | {"", "untitled"}, |
| 26 | {"///", "---"}, |
| 27 | } |
| 28 | for _, tc := range tests { |
| 29 | if got := sanitizeDirName(tc.in); got != tc.want { |
| 30 | t.Errorf("sanitizeDirName(%q) = %q, want %q", tc.in, got, tc.want) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestUniqueDir(t *testing.T) { |
| 36 | base := t.TempDir() |
| 37 | svc := &DownloadService{} |
| 38 | |
| 39 | first := svc.uniqueDir(base, "item") |
| 40 | if filepath.Base(first) != "item" { |
| 41 | t.Errorf("first uniqueDir = %q, want .../item", first) |
| 42 | } |
| 43 | if err := os.MkdirAll(first, 0755); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | second := svc.uniqueDir(base, "item") |
| 47 | if filepath.Base(second) != "item-1" { |
| 48 | t.Errorf("second uniqueDir = %q, want .../item-1", second) |
| 49 | } |
| 50 | if err := os.MkdirAll(second, 0755); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | third := svc.uniqueDir(base, "item") |
| 54 | if filepath.Base(third) != "item-2" { |
| 55 | t.Errorf("third uniqueDir = %q, want .../item-2", third) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestDeriveItemName(t *testing.T) { |
| 60 | svc := &DownloadService{} |
| 61 | itemDir := t.TempDir() |
| 62 | |
| 63 | // info.json title wins and is sanitized. |
| 64 | infoPath := filepath.Join(itemDir, "info.json") |
| 65 | if err := os.WriteFile(infoPath, []byte(`{"title":"Cool: Video"}`), 0644); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | bigName := "big.mp4" |
| 69 | if err := os.WriteFile(filepath.Join(itemDir, bigName), []byte("xxxxxxxxxx"), 0644); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | media := []os.DirEntry{dirEntry(t, itemDir, bigName)} |
| 73 | if got := svc.deriveItemName(itemDir, readInfoJSON(infoPath), media); got != "Cool- Video" { |
| 74 | t.Errorf("deriveItemName with info.json = %q, want %q", got, "Cool- Video") |
| 75 | } |
| 76 | |
| 77 | // Without info.json, falls back to the largest media file's stem. |
| 78 | small := "small.mp4" |
| 79 | if err := os.WriteFile(filepath.Join(itemDir, small), []byte("x"), 0644); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | media = []os.DirEntry{dirEntry(t, itemDir, small), dirEntry(t, itemDir, bigName)} |
| 83 | if got := svc.deriveItemName(itemDir, infoJSON{}, media); got != "big" { |
| 84 | t.Errorf("deriveItemName fallback = %q, want %q (largest file stem)", got, "big") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func dirEntry(t *testing.T, dir, name string) os.DirEntry { |
| 89 | t.Helper() |
| 90 | entries, err := os.ReadDir(dir) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | for _, e := range entries { |
| 95 | if e.Name() == name { |
| 96 | return e |
| 97 | } |
| 98 | } |
| 99 | t.Fatalf("dir entry %q not found", name) |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | func TestParseFormatJSON(t *testing.T) { |
| 104 | data := []byte(`{ |
| 105 | "id": "vid", |
| 106 | "formats": [ |
| 107 | {"format_id": "18", "ext": "mp4", "resolution": "640x360", "fps": 30, "vcodec": "avc1", "acodec": "mp4a", "format_note": "360p"}, |
| 108 | {"format_id": "137", "ext": "mp4", "width": 1920, "height": 1080, "fps": 60, "vcodec": "avc1", "acodec": "none", "filesize": 1048576, "format_note": "1080p"}, |
| 109 | {"format_id": "233", "ext": "m4a", "resolution": "audio only", "vcodec": "none", "acodec": "mp4a", "audio_channels": 2, "format_note": "audio"} |
| 110 | ] |
| 111 | }`) |
| 112 | formats, err := parseFormatJSON(data) |
| 113 | if err != nil { |
| 114 | t.Fatalf("parseFormatJSON: %v", err) |
| 115 | } |
| 116 | if len(formats) != 3 { |
| 117 | t.Fatalf("expected 3 formats, got %d: %+v", len(formats), formats) |
| 118 | } |
| 119 | if formats[0].ID != "18" || formats[0].Ext != "mp4" || formats[0].Resolution != "640x360" { |
| 120 | t.Errorf("format[0] = %+v", formats[0]) |
| 121 | } |
| 122 | if formats[0].FPS != "30" { |
| 123 | t.Errorf("format[0] fps = %q, want 30", formats[0].FPS) |
| 124 | } |
| 125 | // Resolution is derived from width/height when no resolution string is present. |
| 126 | if formats[1].Resolution != "1920x1080" { |
| 127 | t.Errorf("format[1] resolution = %q, want 1920x1080", formats[1].Resolution) |
| 128 | } |
| 129 | if formats[1].FileSize != "1.0 MiB" { |
| 130 | t.Errorf("format[1] filesize = %q, want 1.0 MiB", formats[1].FileSize) |
| 131 | } |
| 132 | // Audio-only format: codec falls back to acodec and channels are populated. |
| 133 | if formats[2].Codec != "mp4a" { |
| 134 | t.Errorf("format[2] codec = %q, want mp4a", formats[2].Codec) |
| 135 | } |
| 136 | if formats[2].Channels != "2" { |
| 137 | t.Errorf("format[2] channels = %q, want 2", formats[2].Channels) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestParseFormatJSONPlaylistFallback(t *testing.T) { |
| 142 | // A playlist dump exposes formats under the first entry, not at the top level. |
| 143 | data := []byte(`{"_type":"playlist","entries":[{"id":"a","formats":[{"format_id":"18","ext":"mp4"}]}]}`) |
| 144 | formats, err := parseFormatJSON(data) |
| 145 | if err != nil { |
| 146 | t.Fatalf("parseFormatJSON: %v", err) |
| 147 | } |
| 148 | if len(formats) != 1 || formats[0].ID != "18" { |
| 149 | t.Fatalf("expected 1 format from entry fallback, got %+v", formats) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // TestImportItemDir exercises the full import: media + info.json + subtitles get |
| 154 | // sorted into a named item directory with a marker. Uses ffmpeg to produce real |
| 155 | // media so the mimetype-based classification in importItemDir matches. |
| 156 | func TestImportItemDir(t *testing.T) { |
| 157 | requireFFmpeg(t) |
| 158 | |
| 159 | libDir := t.TempDir() |
| 160 | svc := &DownloadService{cfg: &config.Config{LibraryDir: libDir, FFprobePath: "ffprobe"}} |
| 161 | |
| 162 | src := t.TempDir() |
| 163 | makeTestVideo(t, filepath.Join(src, "raw.mp4")) |
| 164 | if err := os.WriteFile(filepath.Join(src, "info.json"), []byte(`{"title":"My Clip"}`), 0644); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | 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 { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | |
| 171 | if err := svc.importItemDir(context.Background(), "https://example.com/v", src, libDir, "", "-f bestvideo+bestaudio"); err != nil { |
| 172 | t.Fatalf("importItemDir: %v", err) |
| 173 | } |
| 174 | |
| 175 | targetDir := filepath.Join(libDir, "My Clip") |
| 176 | if _, err := os.Stat(filepath.Join(targetDir, itemMarkerName)); err != nil { |
| 177 | t.Errorf("marker not created: %v", err) |
| 178 | } |
| 179 | if _, err := os.Stat(filepath.Join(targetDir, "info.json")); err != nil { |
| 180 | t.Errorf("info.json not moved: %v", err) |
| 181 | } |
| 182 | if _, err := os.Stat(filepath.Join(targetDir, "raw.mp4")); err != nil { |
| 183 | t.Errorf("media not moved: %v", err) |
| 184 | } |
| 185 | if _, err := os.Stat(filepath.Join(targetDir, subtitlesDirName, "raw.en.srt")); err != nil { |
| 186 | t.Errorf("subtitle not moved into subtitles dir: %v", err) |
| 187 | } |
| 188 | |
| 189 | // The marker should record the source URL and a probed per-file duration |
| 190 | // (the test video is 3s), so the library never has to probe at serve time. |
| 191 | markerData, _ := os.ReadFile(filepath.Join(targetDir, itemMarkerName)) |
| 192 | if want := "https://example.com/v"; !strings.Contains(string(markerData), want) { |
| 193 | t.Errorf("marker missing source url %q: %s", want, markerData) |
| 194 | } |
| 195 | if !strings.Contains(string(markerData), "file_durations") || !strings.Contains(string(markerData), "raw.mp4") { |
| 196 | t.Errorf("marker missing probed per-file duration: %s", markerData) |
| 197 | } |
| 198 | if want := "-f bestvideo+bestaudio"; !strings.Contains(string(markerData), want) { |
| 199 | t.Errorf("marker missing yt-dlp flags %q: %s", want, markerData) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestApplyMetadataPreservesEngagementAndPermissions(t *testing.T) { |
| 204 | e := newExecEnv(t) |
| 205 | existing := writeItem(t, e.cfg.LibraryDir, "existing", `name = "Old" |
| 206 | video_id = "old" |
| 207 | `, map[string]string{ |
| 208 | "video.mp4": "dummy", |
| 209 | "info.json": `{"id":"old","title":"Old","comments":[{"id":"c1","text":"archived"}],"heatmap":[{"start_time":0,"end_time":1,"value":1}],"filesize":123}`, |
| 210 | }) |
| 211 | source := filepath.Join(e.scratch, "refreshed.info.json") |
| 212 | if err := os.WriteFile(source, []byte(`{"id":"new","title":"New","description":"updated","comments":[]}`), 0644); err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | |
| 216 | if err := e.svc.applyMetadata(existing, infoJSON{ID: "new", Title: "New", Description: "updated"}, source); err != nil { |
| 217 | t.Fatalf("applyMetadata: %v", err) |
| 218 | } |
| 219 | |
| 220 | data, err := os.ReadFile(filepath.Join(existing, "info.json")) |
| 221 | if err != nil { |
| 222 | t.Fatal(err) |
| 223 | } |
| 224 | var got map[string]json.RawMessage |
| 225 | if err := json.Unmarshal(data, &got); err != nil { |
| 226 | t.Fatalf("parse installed info JSON: %v", err) |
| 227 | } |
| 228 | if _, ok := got["comments"]; !ok { |
| 229 | t.Fatal("refreshed sidecar lost archived comments") |
| 230 | } |
| 231 | if _, ok := got["heatmap"]; !ok { |
| 232 | t.Fatal("refreshed sidecar lost archived heatmap") |
| 233 | } |
| 234 | if string(got["title"]) != `"New"` { |
| 235 | t.Errorf("title = %s, want New", got["title"]) |
| 236 | } |
| 237 | if mode := fileMode(t, filepath.Join(existing, "info.json")); mode != 0644 { |
| 238 | t.Errorf("info.json mode = %o, want 0644", mode) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestApplyMetadataRollsBackMarkerWhenSidecarInstallFails(t *testing.T) { |
| 243 | e := newExecEnv(t) |
| 244 | existing := writeItem(t, e.cfg.LibraryDir, "rollback", `name = "Old" |
| 245 | video_id = "old" |
| 246 | `, map[string]string{"video.mp4": "dummy"}) |
| 247 | markerPath := filepath.Join(existing, itemMarkerName) |
| 248 | before, err := os.ReadFile(markerPath) |
| 249 | if err != nil { |
| 250 | t.Fatal(err) |
| 251 | } |
| 252 | // A directory at the destination makes the final rename fail after the |
| 253 | // marker has been written, exercising the rollback path. |
| 254 | infoPath := filepath.Join(existing, "info.json") |
| 255 | if err := os.Mkdir(infoPath, 0755); err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | source := filepath.Join(e.scratch, "rollback.info.json") |
| 259 | if err := os.WriteFile(source, []byte(`{"id":"new","title":"New"}`), 0644); err != nil { |
| 260 | t.Fatal(err) |
| 261 | } |
| 262 | |
| 263 | if err := e.svc.applyMetadata(existing, infoJSON{ID: "new", Title: "New"}, source); err == nil { |
| 264 | t.Fatal("expected sidecar installation to fail") |
| 265 | } |
| 266 | after, err := os.ReadFile(markerPath) |
| 267 | if err != nil { |
| 268 | t.Fatal(err) |
| 269 | } |
| 270 | if string(after) != string(before) { |
| 271 | t.Errorf("marker changed after failed sidecar install:\nbefore: %s\nafter: %s", before, after) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | func fileMode(t *testing.T, path string) os.FileMode { |
| 276 | t.Helper() |
| 277 | info, err := os.Stat(path) |
| 278 | if err != nil { |
| 279 | t.Fatal(err) |
| 280 | } |
| 281 | return info.Mode().Perm() |
| 282 | } |
| 283 | |
| 284 | func TestCheckReservedFlags(t *testing.T) { |
| 285 | cases := []struct { |
| 286 | name string |
| 287 | flags string |
| 288 | subscription bool |
| 289 | wantErr bool |
| 290 | }{ |
| 291 | {"empty", "", false, false}, |
| 292 | {"harmless", "--no-playlist --write-thumbnail", false, false}, |
| 293 | {"output short", "-o foo.mp4", false, true}, |
| 294 | {"output long", "--output foo.mp4", false, true}, |
| 295 | {"output equals form", "--output=foo.mp4", false, true}, |
| 296 | {"paths short", "-P /tmp", false, true}, |
| 297 | {"cookies", "--cookies x.txt", false, true}, |
| 298 | {"cookies inverse", "--no-cookies", false, true}, |
| 299 | // Subscription-only reserved flags pass for normal downloads... |
| 300 | {"skip-download non-sub", "--skip-download", false, false}, |
| 301 | {"write-info-json non-sub", "--write-info-json", false, false}, |
| 302 | // ...but are rejected for subscription runs (and their inverses). |
| 303 | {"skip-download sub", "--skip-download", true, true}, |
| 304 | {"no-skip-download sub", "--no-skip-download", true, true}, |
| 305 | {"write-info-json sub", "--write-info-json", true, true}, |
| 306 | {"no-write-info-json sub", "--no-write-info-json", true, true}, |
| 307 | {"download-archive sub", "--download-archive a.txt", true, true}, |
| 308 | // The "--flag=value" spelling names the same option as "--flag value", |
| 309 | // for the subscription-only table as well as the base one. |
| 310 | {"download-archive equals form sub", "--download-archive=a.txt", true, true}, |
| 311 | // Base reserved flags still apply to subscriptions. |
| 312 | {"output sub", "-o x", true, true}, |
| 313 | } |
| 314 | for _, tc := range cases { |
| 315 | t.Run(tc.name, func(t *testing.T) { |
| 316 | err := checkReservedFlags(tc.flags, tc.subscription) |
| 317 | if tc.wantErr != (err != nil) { |
| 318 | t.Errorf("checkReservedFlags(%q, %v) error = %v, wantErr %v", tc.flags, tc.subscription, err, tc.wantErr) |
| 319 | } |
| 320 | }) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | func TestImportDownloadedItemsRejectsOutputTraversal(t *testing.T) { |
| 325 | libDir := t.TempDir() |
| 326 | svc := &DownloadService{ |
| 327 | cfg: &config.Config{LibraryDir: libDir}, |
| 328 | librarySvc: NewLibraryService(libDir, "ffmpeg", "ffprobe"), |
| 329 | } |
| 330 | |
| 331 | // A temp download dir with one item subdir. |
| 332 | tempDir := t.TempDir() |
| 333 | itemDir := filepath.Join(tempDir, "item-00001") |
| 334 | if err := os.MkdirAll(itemDir, 0755); err != nil { |
| 335 | t.Fatal(err) |
| 336 | } |
| 337 | |
| 338 | d := &models.Download{ |
| 339 | URL: "u", |
| 340 | OutputDir: sql.NullString{String: "../escape", Valid: true}, |
| 341 | } |
| 342 | if _, err := svc.importDownloadedItems(context.Background(), d, tempDir, "", ""); err == nil { |
| 343 | t.Error("expected path-traversal output dir to be rejected") |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // A download deleted while its items are being imported must stop, not keep |
| 348 | // filling the library with items whose queue row no longer exists. |
| 349 | func TestImportDownloadedItemsStopsOnCancel(t *testing.T) { |
| 350 | requireFFmpeg(t) |
| 351 | |
| 352 | libDir := t.TempDir() |
| 353 | svc := &DownloadService{ |
| 354 | cfg: &config.Config{LibraryDir: libDir, FFprobePath: "ffprobe"}, |
| 355 | librarySvc: NewLibraryService(libDir, "ffmpeg", "ffprobe"), |
| 356 | } |
| 357 | |
| 358 | tempDir := t.TempDir() |
| 359 | for _, name := range []string{"item-00001", "item-00002"} { |
| 360 | itemDir := filepath.Join(tempDir, name) |
| 361 | if err := os.MkdirAll(itemDir, 0755); err != nil { |
| 362 | t.Fatal(err) |
| 363 | } |
| 364 | makeTestVideo(t, filepath.Join(itemDir, "raw.mp4")) |
| 365 | } |
| 366 | |
| 367 | ctx, cancel := context.WithCancel(context.Background()) |
| 368 | cancel() |
| 369 | |
| 370 | imported, err := svc.importDownloadedItems(ctx, &models.Download{URL: "u"}, tempDir, "", "") |
| 371 | if !errors.Is(err, context.Canceled) { |
| 372 | t.Errorf("error = %v, want context.Canceled", err) |
| 373 | } |
| 374 | if imported != 0 { |
| 375 | t.Errorf("imported = %d, want 0", imported) |
| 376 | } |
| 377 | entries, err := os.ReadDir(libDir) |
| 378 | if err != nil { |
| 379 | t.Fatal(err) |
| 380 | } |
| 381 | if len(entries) != 0 { |
| 382 | t.Errorf("library got %d entries, want 0", len(entries)) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // A cancel part-way through a playlist must stop at an item boundary: the items |
| 387 | // already imported stay, and no half-written directory is left behind. |
| 388 | func TestImportDownloadedItemsStopsBetweenItems(t *testing.T) { |
| 389 | requireFFmpeg(t) |
| 390 | |
| 391 | libDir := t.TempDir() |
| 392 | svc := &DownloadService{ |
| 393 | cfg: &config.Config{LibraryDir: libDir, FFprobePath: "ffprobe"}, |
| 394 | librarySvc: NewLibraryService(libDir, "ffmpeg", "ffprobe"), |
| 395 | } |
| 396 | |
| 397 | tempDir := t.TempDir() |
| 398 | for i, name := range []string{"item-00001", "item-00002", "item-00003"} { |
| 399 | itemDir := filepath.Join(tempDir, name) |
| 400 | if err := os.MkdirAll(itemDir, 0755); err != nil { |
| 401 | t.Fatal(err) |
| 402 | } |
| 403 | makeTestVideo(t, filepath.Join(itemDir, "raw.mp4")) |
| 404 | info := fmt.Sprintf(`{"id":"v%d","title":"Clip %d"}`, i+1, i+1) |
| 405 | if err := os.WriteFile(filepath.Join(itemDir, "clip.info.json"), []byte(info), 0644); err != nil { |
| 406 | t.Fatal(err) |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Cancel as soon as the first item lands, so the second boundary check trips. |
| 411 | ctx, cancel := context.WithCancel(context.Background()) |
| 412 | stop := make(chan struct{}) |
| 413 | go func() { |
| 414 | defer close(stop) |
| 415 | for { |
| 416 | if _, err := os.Stat(filepath.Join(libDir, "Clip 1", itemMarkerName)); err == nil { |
| 417 | cancel() |
| 418 | return |
| 419 | } |
| 420 | time.Sleep(2 * time.Millisecond) |
| 421 | } |
| 422 | }() |
| 423 | |
| 424 | imported, err := svc.importDownloadedItems(ctx, &models.Download{URL: "u"}, tempDir, "", "") |
| 425 | <-stop |
| 426 | cancel() |
| 427 | |
| 428 | if !errors.Is(err, context.Canceled) { |
| 429 | t.Fatalf("error = %v, want context.Canceled", err) |
| 430 | } |
| 431 | if imported < 1 || imported > 2 { |
| 432 | t.Errorf("imported = %d, want the cancel to land after 1 or 2 items", imported) |
| 433 | } |
| 434 | |
| 435 | // Whatever was imported must be complete: an item dir without a marker means |
| 436 | // the import stopped half-way through writing it. |
| 437 | entries, err := os.ReadDir(libDir) |
| 438 | if err != nil { |
| 439 | t.Fatal(err) |
| 440 | } |
| 441 | if len(entries) != imported { |
| 442 | t.Errorf("library has %d dirs but reported %d imported", len(entries), imported) |
| 443 | } |
| 444 | for _, entry := range entries { |
| 445 | marker := filepath.Join(libDir, entry.Name(), itemMarkerName) |
| 446 | if _, err := os.Stat(marker); err != nil { |
| 447 | t.Errorf("item %q left without a marker: %v", entry.Name(), err) |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // Overwrite mode replaces the existing copy of a video in place instead of |
| 453 | // adding a second directory for it. |
| 454 | func TestImportItemDirOverwriteReplacesExisting(t *testing.T) { |
| 455 | requireFFmpeg(t) |
| 456 | |
| 457 | libDir := t.TempDir() |
| 458 | svc := &DownloadService{ |
| 459 | cfg: &config.Config{LibraryDir: libDir, FFprobePath: "ffprobe"}, |
| 460 | librarySvc: NewLibraryService(libDir, "ffmpeg", "ffprobe"), |
| 461 | } |
| 462 | |
| 463 | // First import establishes the item. |
| 464 | first := t.TempDir() |
| 465 | makeTestVideo(t, filepath.Join(first, "raw.mp4")) |
| 466 | if err := os.WriteFile(filepath.Join(first, "clip.info.json"), []byte(`{"id":"vid1","title":"Old Title"}`), 0644); err != nil { |
| 467 | t.Fatal(err) |
| 468 | } |
| 469 | if err := svc.importItemDir(context.Background(), "https://example.com/v", first, libDir, "overwrite", ""); err != nil { |
| 470 | t.Fatalf("first import: %v", err) |
| 471 | } |
| 472 | if err := os.WriteFile(filepath.Join(libDir, "Old Title", "stale.txt"), []byte("gone"), 0644); err != nil { |
| 473 | t.Fatal(err) |
| 474 | } |
| 475 | |
| 476 | // Second import of the same video id, now retitled upstream. |
| 477 | second := t.TempDir() |
| 478 | makeTestVideo(t, filepath.Join(second, "raw.mp4")) |
| 479 | if err := os.WriteFile(filepath.Join(second, "clip.info.json"), []byte(`{"id":"vid1","title":"New Title"}`), 0644); err != nil { |
| 480 | t.Fatal(err) |
| 481 | } |
| 482 | if err := svc.importItemDir(context.Background(), "https://example.com/v", second, libDir, "overwrite", ""); err != nil { |
| 483 | t.Fatalf("second import: %v", err) |
| 484 | } |
| 485 | |
| 486 | entries, err := os.ReadDir(libDir) |
| 487 | if err != nil { |
| 488 | t.Fatal(err) |
| 489 | } |
| 490 | if len(entries) != 1 || entries[0].Name() != "New Title" { |
| 491 | var names []string |
| 492 | for _, e := range entries { |
| 493 | names = append(names, e.Name()) |
| 494 | } |
| 495 | t.Fatalf("library = %v, want exactly [New Title]", names) |
| 496 | } |
| 497 | if _, err := os.Stat(filepath.Join(libDir, "New Title", "stale.txt")); err == nil { |
| 498 | t.Error("the replaced item kept a file from the old copy") |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // Without overwrite mode the same video is imported again beside the first, so |
| 503 | // the guard above is really the thing preventing duplicates. |
| 504 | func TestImportItemDirWithoutOverwriteKeepsBoth(t *testing.T) { |
| 505 | requireFFmpeg(t) |
| 506 | |
| 507 | libDir := t.TempDir() |
| 508 | svc := &DownloadService{ |
| 509 | cfg: &config.Config{LibraryDir: libDir, FFprobePath: "ffprobe"}, |
| 510 | librarySvc: NewLibraryService(libDir, "ffmpeg", "ffprobe"), |
| 511 | } |
| 512 | |
| 513 | for i := 0; i < 2; i++ { |
| 514 | src := t.TempDir() |
| 515 | makeTestVideo(t, filepath.Join(src, "raw.mp4")) |
| 516 | if err := os.WriteFile(filepath.Join(src, "clip.info.json"), []byte(`{"id":"vid1","title":"Same"}`), 0644); err != nil { |
| 517 | t.Fatal(err) |
| 518 | } |
| 519 | if err := svc.importItemDir(context.Background(), "https://example.com/v", src, libDir, "", ""); err != nil { |
| 520 | t.Fatalf("import %d: %v", i, err) |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | entries, err := os.ReadDir(libDir) |
| 525 | if err != nil { |
| 526 | t.Fatal(err) |
| 527 | } |
| 528 | if len(entries) != 2 { |
| 529 | t.Errorf("library has %d dirs, want 2 (Same and Same-1)", len(entries)) |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // moveFile falls back to copy+delete when the source and destination are on |
| 534 | // different filesystems, which is the normal case for a temp dir on tmpfs and a |
| 535 | // library on disk. /dev/shm is a second tmpfs mount, so a rename across it |
| 536 | // really does return EXDEV. |
| 537 | func TestMoveFileCrossDeviceFallback(t *testing.T) { |
| 538 | dstDir, err := os.MkdirTemp("/dev/shm", "vidarchive-move-") |
| 539 | if err != nil { |
| 540 | t.Skipf("no second filesystem available for a cross-device move: %v", err) |
| 541 | } |
| 542 | defer os.RemoveAll(dstDir) |
| 543 | |
| 544 | srcDir := t.TempDir() |
| 545 | if sameDevice(t, srcDir, dstDir) { |
| 546 | t.Skip("temp dir and /dev/shm are on the same filesystem") |
| 547 | } |
| 548 | |
| 549 | src := filepath.Join(srcDir, "clip.mp4") |
| 550 | content := []byte("not really a video, but the bytes must survive") |
| 551 | if err := os.WriteFile(src, content, 0640); err != nil { |
| 552 | t.Fatal(err) |
| 553 | } |
| 554 | |
| 555 | // Confirm the fallback is actually what gets exercised. |
| 556 | dst := filepath.Join(dstDir, "clip.mp4") |
| 557 | if err := os.Rename(src, dst); !errors.Is(err, syscall.EXDEV) { |
| 558 | t.Skipf("rename across the two dirs did not report EXDEV: %v", err) |
| 559 | } |
| 560 | |
| 561 | if err := moveFile(src, dst); err != nil { |
| 562 | t.Fatalf("moveFile: %v", err) |
| 563 | } |
| 564 | |
| 565 | got, err := os.ReadFile(dst) |
| 566 | if err != nil { |
| 567 | t.Fatalf("read destination: %v", err) |
| 568 | } |
| 569 | if string(got) != string(content) { |
| 570 | t.Errorf("destination content = %q, want %q", got, content) |
| 571 | } |
| 572 | info, err := os.Stat(dst) |
| 573 | if err != nil { |
| 574 | t.Fatal(err) |
| 575 | } |
| 576 | if info.Mode().Perm() != 0640 { |
| 577 | t.Errorf("destination mode = %v, want 0640", info.Mode().Perm()) |
| 578 | } |
| 579 | if _, err := os.Stat(src); !os.IsNotExist(err) { |
| 580 | t.Error("source still exists after a successful move") |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // A move that cannot even read the source must not leave a destination behind. |
| 585 | func TestMoveFileMissingSourceLeavesNoDestination(t *testing.T) { |
| 586 | dir := t.TempDir() |
| 587 | dst := filepath.Join(dir, "dst.mp4") |
| 588 | |
| 589 | if err := moveFile(filepath.Join(dir, "missing.mp4"), dst); err == nil { |
| 590 | t.Fatal("expected an error moving a file that does not exist") |
| 591 | } |
| 592 | if _, err := os.Stat(dst); !os.IsNotExist(err) { |
| 593 | t.Error("destination was created for a failed move") |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | func sameDevice(t *testing.T, a, b string) bool { |
| 598 | t.Helper() |
| 599 | var sa, sb syscall.Stat_t |
| 600 | if err := syscall.Stat(a, &sa); err != nil { |
| 601 | t.Fatal(err) |
| 602 | } |
| 603 | if err := syscall.Stat(b, &sb); err != nil { |
| 604 | t.Fatal(err) |
| 605 | } |
| 606 | return sa.Dev == sb.Dev |
| 607 | } |
| 608 |