diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index ffcf017..4622cc6 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -182,10 +182,11 @@ func (t *TorrentManager) repair(torrent *Torrent) { t.log.Infof("repair_method#1: Torrent %s is still in progress but it should work once done (torrent is temporarily hidden until download has completed)", t.GetKey(torrent)) return } else if info != nil && info.IsDone() && !t.isStillBroken(info, brokenFiles) { + selectedFiles := getSelectedFiles(info) torrent.SelectedFiles.IterCb(func(_ string, oldFile *File) { - for ix, newFile := range info.Files { + for _, newFile := range selectedFiles { if oldFile.Bytes == newFile.Bytes { - oldFile.Link = info.Links[ix] + oldFile.Link = newFile.Link break } } @@ -527,10 +528,11 @@ func (t *TorrentManager) handleFixers(fixer realdebrid.Torrent) *Torrent { if info.IsDone() { if !t.isStillBroken(info, brokenFiles) { + selectedFiles := getSelectedFiles(info) torrent.SelectedFiles.IterCb(func(_ string, oldFile *File) { - for ix, newFile := range info.Files { + for _, newFile := range selectedFiles { if oldFile.Bytes == newFile.Bytes { - oldFile.Link = info.Links[ix] + oldFile.Link = newFile.Link break } } @@ -551,3 +553,25 @@ func (t *TorrentManager) handleFixers(fixer realdebrid.Torrent) *Torrent { t.deleteOnceDone.Add(fixer.ID) return nil } + +func getSelectedFiles(info *realdebrid.TorrentInfo) []*File { + var selectedFiles []*File + // if some Links are empty, we need to repair it + for _, file := range info.Files { + if file.Selected == 0 { + continue + } + selectedFiles = append(selectedFiles, &File{ + File: file, + Ended: info.Ended, + Link: "", // no link yet + }) + } + if len(selectedFiles) == len(info.Links) { + // all links are still intact! good! + for i, file := range selectedFiles { + file.Link = info.Links[i] + } + } + return selectedFiles +}