From 6c7c57ebfa16d12025e69b7d84d84d020539f790 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Fri, 19 Jan 2024 03:31:30 +0100 Subject: [PATCH] Fixer refactor --- internal/torrent/refresh.go | 1 + internal/torrent/repair.go | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 96aef03..7855555 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -31,6 +31,7 @@ func (t *TorrentManager) RefreshTorrents() []string { if instances[idx].IsDone() && t.fixers.Has(instances[idx].ID) { fixer := instances[idx] torrent, _ := t.fixers.Pop(fixer.ID) + t.log.Debugf("Fixer %s is done, let's check if it fixed torrent %s", instances[idx].ID, t.GetKey(torrent)) brokenFiles := getBrokenFiles(torrent) info, err := t.redownloadTorrent(torrent, "") if err == nil { diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 73add72..3d53c6f 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -85,6 +85,14 @@ func (t *TorrentManager) repairAll() { torrent.SelectedFiles.IterCb(func(_ string, file *File) { if !strings.HasPrefix(file.Link, "http") && file.Link != "unselect" { hasBrokenFiles = true + return + } + if !isCached && strings.HasPrefix(file.Link, "http") { + unrestrict := t.UnrestrictUntilOk(file.Link) + if unrestrict == nil || file.Bytes != unrestrict.Filesize { + hasBrokenFiles = true + return + } } }) @@ -217,14 +225,20 @@ func (t *TorrentManager) repair(torrent *Torrent) { // get all broken files brokenFiles := getBrokenFiles(torrent) - t.log.Debugf("During repair, zurg found %d broken files for torrent %s", len(brokenFiles), t.GetKey(torrent)) + fileIDs := getFileIDs(brokenFiles) + brokenFileIDs := strings.Join(fileIDs, ",") // first solution: reinsert and see if the broken file is now working - t.log.Debugf("repair_method#1: Trying to redownload torrent %s to repair it", t.GetKey(torrent)) + t.log.Debugf("repair_method#1: Trying to redownload torrent %s to repair files (%s)", t.GetKey(torrent), brokenFileIDs) info, err := t.redownloadTorrent(torrent, "") if err != nil { - t.log.Warnf("Cannot repair torrent %s", t.GetKey(torrent)) - } else if info.Progress != 100 || (info.IsDone() && !t.isStillBroken(info, brokenFiles)) { + t.log.Warnf("Cannot repair torrent %s using repair_method#1", t.GetKey(torrent)) + } + if info != nil && info.Progress != 100 { + t.log.Infof("Redownloading torrent %s after repair_method#1, it should work once done", t.GetKey(torrent)) + return + } + if info != nil && info.IsDone() && !t.isStillBroken(info, brokenFiles) { t.log.Infof("Successfully repaired torrent %s using repair_method#1", t.GetKey(torrent)) return } @@ -232,10 +246,9 @@ func (t *TorrentManager) repair(torrent *Torrent) { // second solution: add only the broken files if len(brokenFiles) > 0 { t.log.Infof("repair_method#2: Redownloading %dof%d broken files for torrent %s", len(brokenFiles), torrent.SelectedFiles.Count(), t.GetKey(torrent)) - brokenFileIDs := strings.Join(getFileIDs(brokenFiles), ",") _, err := t.redownloadTorrent(torrent, brokenFileIDs) if err != nil { - t.log.Warnf("Cannot repair torrent %s", t.GetKey(torrent)) + t.log.Warnf("Cannot repair torrent %s using repair_method#2", t.GetKey(torrent)) } else { t.log.Infof("Successfully repaired torrent %s using repair_method#2", t.GetKey(torrent)) } @@ -323,7 +336,6 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) return nil, fmt.Errorf("it did not fix the issue for %s (id=%s), only got %d files but we need %d, undoing", t.GetKey(torrent), info.ID, len(info.Links), brokenCount) } - t.log.Infof("Redownload successful %s (id=%s)", t.GetKey(torrent), newTorrentID) if len(oldTorrentIDs) > 0 { // only triggered when brokenFiles == "" for _, id := range oldTorrentIDs {