Skip to content

Commit cead146

Browse files
committed
only import split files
1 parent 8da8e32 commit cead146

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

pkg/unpackerr/lidarr.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package unpackerr
22

33
import (
44
"errors"
5+
"path/filepath"
56
"strings"
67
"time"
78

@@ -161,6 +162,31 @@ func extractionHasFlacFiles(files []string) bool {
161162
return false
162163
}
163164

165+
// filterManualImportToSplitTracks returns only outputs whose path is in the extract's NewFiles
166+
// (the split track files). This excludes the original FLAC file we split from, which Lidarr
167+
// would otherwise try and fail to import.
168+
func filterManualImportToSplitTracks(outputs []*lidarr.ManualImportOutput, item *Extract) []*lidarr.ManualImportOutput {
169+
if item == nil || item.Resp == nil || len(item.Resp.NewFiles) == 0 {
170+
return outputs
171+
}
172+
173+
splitPaths := make(map[string]struct{}, len(item.Resp.NewFiles))
174+
for _, p := range item.Resp.NewFiles {
175+
splitPaths[filepath.Clean(p)] = struct{}{}
176+
}
177+
178+
filtered := outputs[:0]
179+
for _, out := range outputs {
180+
if out != nil && out.Path != "" {
181+
if _, ok := splitPaths[filepath.Clean(out.Path)]; ok {
182+
filtered = append(filtered, out)
183+
}
184+
}
185+
}
186+
187+
return filtered
188+
}
189+
164190
// importSplitFlacTracks runs in a goroutine after a Lidarr FLAC+CUE split extraction completes.
165191
// It asks Lidarr for the manual import list for the extract folder and sends the ManualImport command
166192
// so Lidarr imports the split track files.
@@ -192,12 +218,24 @@ func (u *Unpackerr) importSplitFlacTracks(item *Extract, server *LidarrConfig) {
192218
return
193219
}
194220

221+
// Exclude the original FLAC we split from; only import the split track files (item.Resp.NewFiles).
222+
// Lidarr returns every file in the folder including the source file, which will never import.
223+
outputs = filterManualImportToSplitTracks(outputs, item)
224+
225+
if len(outputs) == 0 {
226+
u.Printf("[Lidarr] No split track files to import (folder: %s); original FLAC excluded", item.Path)
227+
return
228+
}
229+
195230
cmd := lidarr.ManualImportCommandFromOutputs(outputs, true)
196231
if cmd == nil {
197232
u.Printf("[Lidarr] No importable files for manual import: %s", item.Path)
198233
return
199234
}
200235

236+
u.Debugf("[Lidarr] Sending manual import command: replaceExisting=%v, importMode=%q, files=%d: %v",
237+
cmd.ReplaceExistingFiles, cmd.ImportMode, len(cmd.Files), cmd.Files)
238+
201239
_, err = server.SendManualImportCommand(cmd)
202240
if err != nil {
203241
u.Errorf("[Lidarr] Manual import command failed for %s: %v", item.Path, err)

0 commit comments

Comments
 (0)