From cc37a92d75752e23990704346148c5eab22e708d Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Thu, 11 Jan 2024 07:43:27 +0100 Subject: [PATCH] Identify broken links properly --- internal/torrent/delete.go | 6 +----- internal/torrent/manager.go | 2 +- internal/torrent/refresh.go | 1 + internal/torrent/repair.go | 22 ++++++---------------- internal/torrent/types.go | 17 +++++++++++++++++ internal/universal/downloader.go | 3 +++ 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/internal/torrent/delete.go b/internal/torrent/delete.go index e532c6d..d36def4 100644 --- a/internal/torrent/delete.go +++ b/internal/torrent/delete.go @@ -16,16 +16,12 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { torrent.DownloadedIDs.Each(func(id string) bool { info, _ := infoCache.Get(id) info.SelectedFiles.IterCb(func(_ string, file *File) { - found := false for _, unselectedID := range unselectedIDs { if file.ID == unselectedID { - found = true + file.Link = "unselect" break } } - if found { - file.Link = "unselect" - } }) t.writeTorrentToFile(id, info, false) return false diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 2ca244d..ff1a0db 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -43,7 +43,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p Api: api, allAccessKeys: mapset.NewSet[string](), latestState: &LibraryState{}, - requiredVersion: "10.01.2024", + requiredVersion: "11.01.2024", workerPool: p, log: log, } diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 51b2a0c..3ce93c6 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -180,6 +180,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { } else { torrent.InProgressIDs.Add(info.ID) } + torrent.BrokenLinks = mapset.NewSet[string]() t.writeTorrentToFile(rdTorrent.ID, &torrent, true) infoCache.Set(rdTorrent.ID, &torrent) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index bffac4a..0ee2677 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -102,26 +102,16 @@ func (t *TorrentManager) repairAll() { func (t *TorrentManager) Repair(torrent *Torrent) { // save the broken files to the file cache - var brokenFileIDs []int - torrent.SelectedFiles.IterCb(func(_ string, file *File) { - if file.Link == "repair" { - brokenFileIDs = append(brokenFileIDs, file.ID) - } - }) infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) torrent.DownloadedIDs.Each(func(id string) bool { info, _ := infoCache.Get(id) info.SelectedFiles.IterCb(func(_ string, file *File) { - found := false - for _, brokenFileID := range brokenFileIDs { - if file.ID == brokenFileID { - found = true - break + torrent.BrokenLinks.Each(func(link string) bool { + if file.Link == link { + file.Link = "repair" } - } - if found { - file.Link = "unselect" - } + return file.Link == link + }) }) t.writeTorrentToFile(id, info, false) return false @@ -221,7 +211,7 @@ func (t *TorrentManager) repair(torrent *Torrent) { // second solution: add only the broken files var brokenFiles []File torrent.SelectedFiles.IterCb(func(_ string, file *File) { - if file.Link == "repair" || file.Link == "" { + if !strings.HasPrefix(file.Link, "http") && file.Link != "unselect" { brokenFiles = append(brokenFiles, *file) } }) diff --git a/internal/torrent/types.go b/internal/torrent/types.go index 82ee021..a2dad34 100644 --- a/internal/torrent/types.go +++ b/internal/torrent/types.go @@ -24,6 +24,7 @@ type Torrent struct { DownloadedIDs mapset.Set[string] `json:"DownloadedIDs"` InProgressIDs mapset.Set[string] `json:"InProgressIDs"` UnassignedLinks mapset.Set[string] `json:"UnassignedLinks"` + BrokenLinks mapset.Set[string] `json:"BrokenLinks"` Version string `json:"Version"` // only used for files } @@ -35,6 +36,7 @@ func (t *Torrent) MarshalJSON() ([]byte, error) { DownloadedIDsJson stdjson.RawMessage `json:"DownloadedIDs"` InProgressIDsJson stdjson.RawMessage `json:"InProgressIDs"` UnassignedLinksJson stdjson.RawMessage `json:"UnassignedLinks"` + BrokenLinksJson stdjson.RawMessage `json:"BrokenLinks"` *Alias }{ Alias: (*Alias)(t), @@ -67,6 +69,13 @@ func (t *Torrent) MarshalJSON() ([]byte, error) { temp.UnassignedLinksJson = []byte(unassignedLinksStr) } + if t.BrokenLinks.IsEmpty() { + temp.BrokenLinksJson = []byte(`""`) + } else { + brokenLinksStr := `"` + strings.Join(t.BrokenLinks.ToSlice(), ",") + `"` + temp.BrokenLinksJson = []byte(brokenLinksStr) + } + return json.Marshal(temp) } @@ -77,6 +86,7 @@ func (t *Torrent) UnmarshalJSON(data []byte) error { DownloadedIDsJson stdjson.RawMessage `json:"DownloadedIDs"` InProgressIDsJson stdjson.RawMessage `json:"InProgressIDs"` UnassignedLinksJson stdjson.RawMessage `json:"UnassignedLinks"` + BrokenLinksJson stdjson.RawMessage `json:"BrokenLinks"` *Alias }{ Alias: (*Alias)(t), @@ -113,6 +123,13 @@ func (t *Torrent) UnmarshalJSON(data []byte) error { t.UnassignedLinks = mapset.NewSet[string]() } + if len(temp.BrokenLinksJson) > 2 { + brokenLinks := strings.Split(strings.ReplaceAll(string(temp.BrokenLinksJson), `"`, ""), ",") + t.BrokenLinks = mapset.NewSet[string](brokenLinks...) + } else { + t.BrokenLinks = mapset.NewSet[string]() + } + return nil } diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index a0ab862..e301349 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -56,6 +56,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp if unrestrict == nil { log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link) if cfg.EnableRepair() { + torrent.BrokenLinks.Add(file.Link) file.Link = "repair" torMgr.Repair(torrent) } else { @@ -159,6 +160,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor if file != nil && unrestrict.Streamable == 1 { log.Warnf("Cannot download file %s: %v", file.Path, err) if cfg.EnableRepair() && torrent != nil { + torrent.BrokenLinks.Add(file.Link) file.Link = "repair" torMgr.Repair(torrent) } else { @@ -174,6 +176,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor if file != nil && unrestrict.Streamable == 1 { log.Warnf("Received a %s status code for file %s", download.Status, file.Path) if cfg.EnableRepair() && torrent != nil { + torrent.BrokenLinks.Add(file.Link) file.Link = "repair" torMgr.Repair(torrent) } else {