From 58b0b8b91793d49012cc049bd325113c3aba937a Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Fri, 26 Jan 2024 12:59:00 +0100 Subject: [PATCH] Remove link on repair --- internal/torrent/repair.go | 59 ++++++++++++++++---------------- internal/universal/downloader.go | 6 ++-- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 7d92c4b..825c4c6 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -35,16 +35,40 @@ func (t *TorrentManager) repairAll() { return } + // save the broken files to the file cache + // broken files are also added when trying to open a file + if torrent.BrokenLinks.Cardinality() > 0 { + infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) + torrent.DownloadedIDs.Each(func(id string) bool { + info, _ := infoCache.Get(id) + hasBrokenFiles := false + info.SelectedFiles.IterCb(func(_ string, file *File) { + torrent.BrokenLinks.Each(func(brokenLink string) bool { + if file.Link == brokenLink { + hasBrokenFiles = true + file.Link = "" + return true + } + return false + }) + }) + if hasBrokenFiles { + info.BrokenLinks = torrent.BrokenLinks + t.writeTorrentToFile(id, info) + } + return false + }) + } + // check 2: for broken files - hasBrokenFiles := false + brokenFileIDs := mapset.NewSet[int]() torrent.SelectedFiles.IterCb(func(_ string, file *File) { if !strings.HasPrefix(file.Link, "http") && file.Link != "unselect" { - hasBrokenFiles = true - return + brokenFileIDs.Add(file.ID) } }) - if hasBrokenFiles { - t.log.Debugf("Torrent %s has broken files, adding to repair list", t.GetKey(torrent)) + if brokenFileIDs.Cardinality() > 0 { + t.log.Debugf("Torrent %s has broken files (ids=%v), adding to repair list", t.GetKey(torrent), brokenFileIDs.ToSlice()) toRepair.Add(torrent) return } @@ -80,31 +104,6 @@ func (t *TorrentManager) Repair(torrent *Torrent) { t.repairs.Add(t.GetKey(torrent)) - // save the broken files to the file cache - // broken files are also added when trying to open a file - if torrent.BrokenLinks.Cardinality() > 0 { - infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) - torrent.DownloadedIDs.Each(func(id string) bool { - info, _ := infoCache.Get(id) - hasBrokenFiles := false - info.SelectedFiles.IterCb(func(_ string, file *File) { - torrent.BrokenLinks.Each(func(brokenLink string) bool { - if file.Link == brokenLink { - hasBrokenFiles = true - file.Link = "" - return true - } - return false - }) - }) - if hasBrokenFiles { - info.BrokenLinks = torrent.BrokenLinks - t.writeTorrentToFile(id, info) - } - return false - }) - } - t.log.Infof("Attempting repair for torrent %s", t.GetKey(torrent)) // blocks for approx 45 minutes if active torrents are full diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index 572166a..9740cc6 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -55,7 +55,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp if unrestrict == nil { log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link) torrent.BrokenLinks.Add(file.Link) - file.Link = "repair" + // file.Link = "repair" if cfg.EnableRepair() { torMgr.SetNewLatestState(intTor.LibraryState{}) } else { @@ -167,7 +167,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor log.Warnf("Cannot download file %s: %v", unrestrict.Download, err) if file != nil && unrestrict.Streamable == 1 { torrent.BrokenLinks.Add(file.Link) - file.Link = "repair" + // file.Link = "repair" if cfg.EnableRepair() && torrent != nil { torMgr.SetNewLatestState(intTor.LibraryState{}) } else { @@ -184,7 +184,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) torrent.BrokenLinks.Add(file.Link) - file.Link = "repair" + // file.Link = "repair" if cfg.EnableRepair() && torrent != nil { torMgr.SetNewLatestState(intTor.LibraryState{}) } else {