(filePath string, editor mp3.TagEditor, lyricsFetcher fetcher.LyricsFetcher, artworkFetcher fetcher.ArtworkFetcher)
| 277 | } |
| 278 | |
| 279 | func processFile(filePath string, editor mp3.TagEditor, lyricsFetcher fetcher.LyricsFetcher, artworkFetcher fetcher.ArtworkFetcher) FileResult { |
| 280 | result := FileResult{filepath: filePath} |
| 281 | |
| 282 | tags, err := editor.ReadTags(filePath) |
| 283 | if err != nil { |
| 284 | result.error = err |
| 285 | return result |
| 286 | } |
| 287 | |
| 288 | updates := mp3.TagUpdates{} |
| 289 | |
| 290 | sourceTitle := tags.Title |
| 291 | if strings.TrimSpace(sourceTitle) == "" { |
| 292 | sourceTitle = utils.DeriveTitleFromFilename(filePath) |
| 293 | logrus.Debugf("Derived title from filename (%s): %s", filepath.Base(filePath), sourceTitle) |
| 294 | } |
| 295 | sourceArtist := tags.Artist |
| 296 | |
| 297 | var lyricsWg sync.WaitGroup |
| 298 | var lyricsResult string |
| 299 | var lyricsError error |
| 300 | var artworkResult []byte |
| 301 | var artworkError error |
| 302 | |
| 303 | if batchLyrics { |
| 304 | if tags.Lyrics != "" && !batchForce { |
| 305 | logrus.Debugf("Lyrics already present for %s (skip; --force to overwrite)", filepath.Base(filePath)) |
| 306 | } else { |
| 307 | lyricsWg.Add(1) |
| 308 | go func() { |
| 309 | defer lyricsWg.Done() |
| 310 | lyricsResult, lyricsError = lyricsFetcher.Fetch(sourceTitle, sourceArtist) |
| 311 | }() |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if batchArtwork { |
| 316 | if len(tags.Artwork) > 0 && !batchForce { |
| 317 | logrus.Debugf("Artwork already present for %s (skip; --force to overwrite)", filepath.Base(filePath)) |
| 318 | } else { |
| 319 | lyricsWg.Add(1) |
| 320 | go func() { |
| 321 | defer lyricsWg.Done() |
| 322 | artworkResult, artworkError = artworkFetcher.Fetch(sourceTitle, sourceArtist, tags.Album) |
| 323 | }() |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | lyricsWg.Wait() |
| 328 | |
| 329 | if batchLyrics && lyricsError == nil && lyricsResult != "" { |
| 330 | updates.Lyrics = lyricsResult |
| 331 | } else if batchLyrics && lyricsError != nil { |
| 332 | logrus.Debugf("Failed to fetch lyrics for %s: %v", filepath.Base(filePath), lyricsError) |
| 333 | } |
| 334 | |
| 335 | if batchArtwork && artworkError == nil && artworkResult != nil { |
| 336 | updates.Artwork = artworkResult |
no test coverage detected