@@ -2,6 +2,7 @@ package unpackerr
22
33import (
44 "errors"
5+ "strings"
56 "time"
67
78 "golift.io/starr"
@@ -136,3 +137,72 @@ func (u *Unpackerr) haveLidarrQitem(name string) bool {
136137
137138 return false
138139}
140+
141+ // lidarrServerByURL returns the Lidarr server config that matches the given URL, or nil.
142+ func (u * Unpackerr ) lidarrServerByURL (url string ) * LidarrConfig {
143+ for _ , server := range u .Lidarr {
144+ if server .URL == url {
145+ return server
146+ }
147+ }
148+
149+ return nil
150+ }
151+
152+ // extractionHasFlacFiles returns true if any path in files has a .flac extension.
153+ // Used to only trigger manual import after a FLAC+CUE split, not for e.g. zip-of-mp3s.
154+ func extractionHasFlacFiles (files []string ) bool {
155+ for _ , p := range files {
156+ if strings .HasSuffix (strings .ToLower (p ), ".flac" ) {
157+ return true
158+ }
159+ }
160+
161+ return false
162+ }
163+
164+ // importSplitFlacTracks runs in a goroutine after a Lidarr FLAC+CUE split extraction completes.
165+ // It asks Lidarr for the manual import list for the extract folder and sends the ManualImport command
166+ // so Lidarr imports the split track files.
167+ func (u * Unpackerr ) importSplitFlacTracks (item * Extract , server * LidarrConfig ) {
168+ if server == nil {
169+ u .Printf ("[Lidarr] No Lidarr server found for manual import, this might be a bug: %s" , item .Path )
170+ return
171+ }
172+
173+ downloadID , _ := item .IDs ["downloadId" ].(string )
174+ artistID , _ := item .IDs ["artistId" ].(int64 )
175+
176+ params := & lidarr.ManualImportParams {
177+ Folder : item .Path ,
178+ DownloadID : downloadID ,
179+ ArtistID : artistID ,
180+ FilterExistingFiles : false ,
181+ ReplaceExistingFiles : true ,
182+ }
183+
184+ outputs , err := server .ManualImport (params )
185+ if err != nil {
186+ u .Errorf ("[Lidarr] Manual import list failed for %s: %v" , item .Path , err )
187+ return
188+ }
189+
190+ if len (outputs ) == 0 {
191+ u .Printf ("[Lidarr] No files returned for manual import (folder: %s); import manually in Lidarr" , item .Path )
192+ return
193+ }
194+
195+ cmd := lidarr .ManualImportCommandFromOutputs (outputs , true )
196+ if cmd == nil {
197+ u .Printf ("[Lidarr] No importable files for manual import: %s" , item .Path )
198+ return
199+ }
200+
201+ _ , err = server .SendManualImportCommand (cmd )
202+ if err != nil {
203+ u .Errorf ("[Lidarr] Manual import command failed for %s: %v" , item .Path , err )
204+ return
205+ }
206+
207+ u .Printf ("[Lidarr] Manual import triggered for %d files: %s" , len (cmd .Files ), item .Path )
208+ }
0 commit comments