import.go
⎇
Raw
1package service
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "log"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "sort"
14 "strconv"
15 "strings"
16 "syscall"
17
18 "github.com/gabriel-vasile/mimetype"
19
20 "vidarchive/internal/models"
21)
22
23// resolveBaseLibraryDir returns the absolute library directory a download writes
24// into, applying the optional per-download OutputDir while rejecting any path
25// that escapes the library root.
26func (s *DownloadService) resolveBaseLibraryDir(d *models.Download) (string, error) {
27 if !d.OutputDir.Valid || d.OutputDir.String == "" {
28 return s.cfg.LibraryDir, nil
29 }
30 // Reuse the library service's guard so both entry points enforce the boundary
31 // the same way — it resolves symlinks, which a plain prefix check does not.
32 dir, err := s.librarySvc.ResolveWithinLibrary(d.OutputDir.String)
33 if err != nil {
34 return "", fmt.Errorf("invalid output directory: %w", err)
35 }
36 return dir, nil
37}
38
39// importDownloadedItems moves each downloaded item from the temp dir into the
40// library and returns the number of items successfully imported. Per-item
41// failures are logged and skipped (a playlist with a few bad entries still
42// imports the rest); a non-nil error means the import couldn't even start.
43//
44// A cancel stops the import between items and returns ctx.Err() with the count
45// imported so far. Importing a long playlist takes real time (a move plus an
46// ffprobe per file), so a deleted download must not keep filling the library.
47func (s *DownloadService) importDownloadedItems(ctx context.Context, d *models.Download, tempDownloadDir, mode, ytdlpFlags string) (int, error) {
48 entries, err := os.ReadDir(tempDownloadDir)
49 if err != nil {
50 return 0, err
51 }
52
53 baseLibraryDir, err := s.resolveBaseLibraryDir(d)
54 if err != nil {
55 return 0, err
56 }
57 if err := os.MkdirAll(baseLibraryDir, 0755); err != nil {
58 return 0, err
59 }
60
61 var itemDirs []string
62 for _, entry := range entries {
63 if !entry.IsDir() {
64 continue
65 }
66 name := entry.Name()
67 if strings.HasPrefix(name, "item-") {
68 itemDirs = append(itemDirs, filepath.Join(tempDownloadDir, name))
69 }
70 }
71 sort.Strings(itemDirs)
72
73 imported := 0
74 for _, itemDir := range itemDirs {
75 if err := ctx.Err(); err != nil {
76 return imported, err
77 }
78 if err := s.importItemDir(ctx, d.URL, itemDir, baseLibraryDir, mode, ytdlpFlags); err != nil {
79 // A cancelled item isn't a bad item: stop instead of logging a warning
80 // for it and every one that follows.
81 if ctx.Err() != nil {
82 return imported, ctx.Err()
83 }
84 log.Printf("warning: failed to import item %s: %v", itemDir, err)
85 continue
86 }
87 imported++
88 }
89
90 // The temp dir (and any leftovers from failed imports) is removed by the
91 // caller's deferred cleanup, so partial state never leaks even on a crash.
92 return imported, nil
93}
94
95func (s *DownloadService) importItemDir(ctx context.Context, url, itemDir, baseLibraryDir, mode, ytdlpFlags string) error {
96 entries, err := os.ReadDir(itemDir)
97 if err != nil {
98 return err
99 }
100
101 var mediaFiles []os.DirEntry
102 var infoJSONPath string
103 var subtitleFiles []string
104
105 for _, entry := range entries {
106 if entry.IsDir() {
107 continue
108 }
109 name := entry.Name()
110 path := filepath.Join(itemDir, name)
111 ext := strings.ToLower(filepath.Ext(name))
112
113 if isInfoJSON(name) {
114 infoJSONPath = path
115 continue
116 }
117 if ext == ".vtt" || ext == ".srt" || ext == ".ass" || ext == ".ssa" {
118 subtitleFiles = append(subtitleFiles, path)
119 continue
120 }
121
122 mtype, err := mimetype.DetectFile(path)
123 if err == nil && mtype != nil && (strings.HasPrefix(mtype.String(), "audio/") || strings.HasPrefix(mtype.String(), "video/")) {
124 mediaFiles = append(mediaFiles, entry)
125 }
126 }
127
128 if len(mediaFiles) == 0 {
129 return fmt.Errorf("no media files found in %s", itemDir)
130 }
131
132 info := readInfoJSON(infoJSONPath)
133 name := s.deriveItemName(itemDir, info, mediaFiles)
134 videoID := info.ID
135
136 // Last point at which nothing has been written to the library yet: give up
137 // here on a cancel rather than part-way through, which would leave a folder
138 // with some of its files and no marker — or, in overwrite mode, delete the
139 // existing item and not replace it.
140 if err := ctx.Err(); err != nil {
141 return err
142 }
143
144 // Overwrite mode: replace the existing copy of this video in place rather than
145 // creating a duplicate folder. Removing the old dir lets uniqueDir reuse its
146 // name (or land on the new title if it changed upstream).
147 if mode == "overwrite" && videoID != "" {
148 if existing, ok := s.librarySvc.FindByVideoID(baseLibraryDir, videoID); ok {
149 if rel, err := filepath.Rel(s.cfg.LibraryDir, existing); err == nil {
150 s.librarySvc.evictCachedScan(filepath.ToSlash(rel))
151 }
152 os.RemoveAll(existing)
153 }
154 }
155
156 targetDir := s.uniqueDir(baseLibraryDir, name)
157 if err := os.MkdirAll(targetDir, 0755); err != nil {
158 return err
159 }
160
161 if infoJSONPath != "" {
162 if err := moveFile(infoJSONPath, filepath.Join(targetDir, "info.json")); err != nil {
163 return err
164 }
165 }
166
167 for _, entry := range mediaFiles {
168 if err := moveFile(filepath.Join(itemDir, entry.Name()), filepath.Join(targetDir, entry.Name())); err != nil {
169 return err
170 }
171 }
172
173 // Probe each media file's duration once, here in the worker (off the request
174 // path), and cache it in the marker so the library never has to probe while
175 // serving pages. Files we can't probe simply get no duration.
176 fileDurations := make(map[string]int)
177 for _, entry := range mediaFiles {
178 if d, ok := probeDuration(ctx, s.cfg.FFprobePath, filepath.Join(targetDir, entry.Name())); ok {
179 fileDurations[entry.Name()] = d
180 }
181 }
182
183 if len(subtitleFiles) > 0 {
184 subtitlesDir := filepath.Join(targetDir, subtitlesDirName)
185 if err := os.MkdirAll(subtitlesDir, 0755); err != nil {
186 return err
187 }
188 for _, sf := range subtitleFiles {
189 if err := moveFile(sf, filepath.Join(subtitlesDir, filepath.Base(sf))); err != nil {
190 return err
191 }
192 }
193 }
194
195 metadata := models.ItemMetadata{
196 Name: name,
197 SourceURL: url,
198 VideoID: videoID,
199 YtdlpFlags: ytdlpFlags,
200 FileDurations: fileDurations,
201 }
202
203 return s.librarySvc.writeMetadata(targetDir, metadata)
204}
205
206// infoJSON is the subset of yt-dlp's info.json VidArchive reads. ID is the
207// stable item identity; within a single subscription's own directory it is
208// enough to match items, so the extractor is not needed.
209type infoJSON struct {
210 ID string `json:"id"`
211 Title string `json:"title"`
212 Description string `json:"description"`
213 WebpageURL string `json:"webpage_url"`
214}
215
216// readInfoJSON parses an info.json. A missing, unreadable or malformed file
217// yields a zero-value struct: every caller treats absent fields as "unknown"
218// and falls back, so there is nothing to distinguish.
219func readInfoJSON(infoJSONPath string) infoJSON {
220 var info infoJSON
221 if infoJSONPath == "" {
222 return info
223 }
224 data, err := os.ReadFile(infoJSONPath)
225 if err != nil {
226 return info
227 }
228 if err := json.Unmarshal(data, &info); err != nil {
229 log.Printf("ignoring malformed %s: %v", infoJSONPath, err)
230 return infoJSON{}
231 }
232 return info
233}
234
235// isInfoJSON reports whether a file name is yt-dlp's metadata sidecar. yt-dlp
236// writes "<title>.info.json" next to the media, but a bare "info.json" is what
237// an already-imported item holds.
238func isInfoJSON(name string) bool {
239 return name == "info.json" || strings.HasSuffix(name, ".info.json")
240}
241
242// findInfoJSON returns the path to an info.json directly inside itemDir, or "".
243func findInfoJSON(itemDir string) string {
244 entries, err := os.ReadDir(itemDir)
245 if err != nil {
246 return ""
247 }
248 for _, entry := range entries {
249 if entry.IsDir() {
250 continue
251 }
252 name := entry.Name()
253 if isInfoJSON(name) {
254 return filepath.Join(itemDir, name)
255 }
256 }
257 return ""
258}
259
260// deriveItemName names the imported item after its title, falling back to the
261// largest media file's base name when there is no usable info.json.
262func (s *DownloadService) deriveItemName(itemDir string, info infoJSON, mediaFiles []os.DirEntry) string {
263 if info.Title != "" {
264 return sanitizeDirName(info.Title)
265 }
266
267 var largest os.DirEntry
268 var maxSize int64
269 for _, f := range mediaFiles {
270 st, err := os.Stat(filepath.Join(itemDir, f.Name()))
271 if err == nil && (largest == nil || st.Size() > maxSize) {
272 largest, maxSize = f, st.Size()
273 }
274 }
275 if largest == nil {
276 largest = mediaFiles[0]
277 }
278 base := strings.TrimSuffix(largest.Name(), filepath.Ext(largest.Name()))
279 return sanitizeDirName(base)
280}
281
282func (s *DownloadService) uniqueDir(base, name string) string {
283 dir := filepath.Join(base, name)
284 if _, err := os.Stat(dir); os.IsNotExist(err) {
285 return dir
286 }
287 for i := 1; ; i++ {
288 candidate := fmt.Sprintf("%s-%d", dir, i)
289 if _, err := os.Stat(candidate); os.IsNotExist(err) {
290 return candidate
291 }
292 }
293}
294
295// moveFile moves src to dst, falling back to copy-and-delete when the two are on
296// different filesystems. The temp and library directories are independently
297// configurable, so they can legitimately live on separate mounts — where a plain
298// rename fails with EXDEV.
299func moveFile(src, dst string) error {
300 if err := os.Rename(src, dst); err == nil {
301 return nil
302 } else if !errors.Is(err, syscall.EXDEV) {
303 return err
304 }
305
306 in, err := os.Open(src)
307 if err != nil {
308 return err
309 }
310 defer in.Close()
311
312 info, err := in.Stat()
313 if err != nil {
314 return err
315 }
316
317 out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
318 if err != nil {
319 return err
320 }
321
322 if _, err := io.Copy(out, in); err != nil {
323 out.Close()
324 os.Remove(dst)
325 return err
326 }
327 // Close explicitly: a deferred close would hide a flush error on the copy.
328 if err := out.Close(); err != nil {
329 os.Remove(dst)
330 return err
331 }
332
333 return os.Remove(src)
334}
335
336func sanitizeDirName(name string) string {
337 name = strings.TrimSpace(name)
338 replacer := strings.NewReplacer(
339 "/", "-",
340 "\\", "-",
341 ":", "-",
342 "*", "-",
343 "?", "-",
344 "\"", "-",
345 "<", "-",
346 ">", "-",
347 "|", "-",
348 )
349 name = replacer.Replace(name)
350 name = strings.TrimSpace(name)
351 // A name made only of dots resolves to the parent ("..") or to the target
352 // directory itself ("."), so joining it would place the item outside the
353 // library. Titles come from remote metadata, so refuse them here.
354 if strings.Trim(name, ".") == "" {
355 name = "untitled"
356 }
357 return name
358}
359
360// probeDuration returns the duration of a media file in whole seconds. The bool
361// is false when ffprobe is unavailable or the file has no usable duration.
362func probeDuration(ctx context.Context, ffprobePath, path string) (int, bool) {
363 out, err := exec.CommandContext(ctx, ffprobePath, "-v", "error",
364 "-show_entries", "format=duration",
365 "-of", "default=nw=1:nk=1", path).Output()
366 if err != nil {
367 return 0, false
368 }
369 f, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
370 if err != nil || f <= 0 {
371 return 0, false
372 }
373 return int(f + 0.5), true
374}
375