From d0f2abcd740657e23803567476da7a2eb7a5ba59 Mon Sep 17 00:00:00 2001 From: Deivid Soto Date: Sun, 29 Mar 2026 20:44:33 +0200 Subject: [PATCH] refactor: extract BuildSyncItems to library package, remove duplication --- internal/cmd/daemon.go | 42 ++------------------------------------ internal/cmd/scan.go | 37 +-------------------------------- internal/library/sync.go | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 76 deletions(-) create mode 100644 internal/library/sync.go diff --git a/internal/cmd/daemon.go b/internal/cmd/daemon.go index 2c38b11..8a7881b 100644 --- a/internal/cmd/daemon.go +++ b/internal/cmd/daemon.go @@ -573,7 +573,7 @@ func runAutoScan(ctx context.Context, cfg config.Config, interval time.Duration) } ac := agent.NewClient(cfg.Auth.APIURL, apiKey, "unarr/"+Version) - items := buildSyncItems(cache) + items := library.BuildSyncItems(cache) if len(items) == 0 { log.Printf("[auto-scan] no items to sync") return @@ -616,42 +616,4 @@ func runAutoScan(ctx context.Context, cfg config.Config, interval time.Duration) } } -// buildSyncItems converts cached library items to sync request items. -func buildSyncItems(cache *library.LibraryCache) []agent.LibrarySyncItem { - items := make([]agent.LibrarySyncItem, 0, len(cache.Items)) - for _, item := range cache.Items { - if item.ScanError != "" { - continue - } - si := agent.LibrarySyncItem{ - FilePath: item.FilePath, - FileName: item.FileName, - FileSize: item.FileSize, - Title: item.Title, - Year: item.Year, - ContentType: library.DeriveContentType(item), - Season: item.Season, - Episode: item.Episode, - } - - if item.MediaInfo != nil { - if item.MediaInfo.Video != nil { - si.Resolution = library.ResolveResolution(item.MediaInfo.Video.Height) - si.VideoCodec = item.MediaInfo.Video.Codec - si.HDR = item.MediaInfo.Video.HDR - si.BitDepth = item.MediaInfo.Video.BitDepth - } - codec, channels := library.PrimaryAudioTrack(item.MediaInfo.Audio) - si.AudioCodec = codec - si.AudioChannels = channels - si.AudioLanguages = library.AudioLanguages(item.MediaInfo.Audio) - si.SubtitleLanguages = library.SubtitleLanguages(item.MediaInfo.Subtitles) - si.AudioTracks = item.MediaInfo.Audio - si.SubtitleTracks = item.MediaInfo.Subtitles - si.VideoInfo = item.MediaInfo.Video - } - - items = append(items, si) - } - return items -} +// buildSyncItems moved to internal/library/sync.go as library.BuildSyncItems diff --git a/internal/cmd/scan.go b/internal/cmd/scan.go index 8872215..791e636 100644 --- a/internal/cmd/scan.go +++ b/internal/cmd/scan.go @@ -153,42 +153,7 @@ func syncToServer(ctx context.Context, cfg config.Config, cache *library.Library ac := agent.NewClient(cfg.Auth.APIURL, apiKey, "unarr/"+Version) - // Build sync items from cache - items := make([]agent.LibrarySyncItem, 0, len(cache.Items)) - for _, item := range cache.Items { - if item.ScanError != "" { - continue // skip items with scan errors - } - si := agent.LibrarySyncItem{ - FilePath: item.FilePath, - FileName: item.FileName, - FileSize: item.FileSize, - Title: item.Title, - Year: item.Year, - ContentType: library.DeriveContentType(item), - Season: item.Season, - Episode: item.Episode, - } - - if item.MediaInfo != nil { - if item.MediaInfo.Video != nil { - si.Resolution = library.ResolveResolution(item.MediaInfo.Video.Height) - si.VideoCodec = item.MediaInfo.Video.Codec - si.HDR = item.MediaInfo.Video.HDR - si.BitDepth = item.MediaInfo.Video.BitDepth - } - codec, channels := library.PrimaryAudioTrack(item.MediaInfo.Audio) - si.AudioCodec = codec - si.AudioChannels = channels - si.AudioLanguages = library.AudioLanguages(item.MediaInfo.Audio) - si.SubtitleLanguages = library.SubtitleLanguages(item.MediaInfo.Subtitles) - si.AudioTracks = item.MediaInfo.Audio - si.SubtitleTracks = item.MediaInfo.Subtitles - si.VideoInfo = item.MediaInfo.Video - } - - items = append(items, si) - } + items := library.BuildSyncItems(cache) if len(items) == 0 { color.Yellow("\n No valid items to sync.") diff --git a/internal/library/sync.go b/internal/library/sync.go new file mode 100644 index 0000000..bfa75ec --- /dev/null +++ b/internal/library/sync.go @@ -0,0 +1,44 @@ +package library + +import "github.com/torrentclaw/torrentclaw-cli/internal/agent" + +// BuildSyncItems converts cached library items to sync request items. +// Shared between unarr scan (cmd/scan.go) and auto-scan (cmd/daemon.go). +func BuildSyncItems(cache *LibraryCache) []agent.LibrarySyncItem { + items := make([]agent.LibrarySyncItem, 0, len(cache.Items)) + for _, item := range cache.Items { + if item.ScanError != "" { + continue + } + si := agent.LibrarySyncItem{ + FilePath: item.FilePath, + FileName: item.FileName, + FileSize: item.FileSize, + Title: item.Title, + Year: item.Year, + ContentType: DeriveContentType(item), + Season: item.Season, + Episode: item.Episode, + } + + if item.MediaInfo != nil { + if item.MediaInfo.Video != nil { + si.Resolution = ResolveResolution(item.MediaInfo.Video.Height) + si.VideoCodec = item.MediaInfo.Video.Codec + si.HDR = item.MediaInfo.Video.HDR + si.BitDepth = item.MediaInfo.Video.BitDepth + } + codec, channels := PrimaryAudioTrack(item.MediaInfo.Audio) + si.AudioCodec = codec + si.AudioChannels = channels + si.AudioLanguages = AudioLanguages(item.MediaInfo.Audio) + si.SubtitleLanguages = SubtitleLanguages(item.MediaInfo.Subtitles) + si.AudioTracks = item.MediaInfo.Audio + si.SubtitleTracks = item.MediaInfo.Subtitles + si.VideoInfo = item.MediaInfo.Video + } + + items = append(items, si) + } + return items +}