From 2504b31d4c55d4f3ec9211b3e21833dd7a626c48 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Tue, 28 May 2024 02:37:28 +0200 Subject: [PATCH] Delete if links are still broken or no longer cached --- internal/torrent/repair.go | 45 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 3bc1f73..128577c 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -184,34 +184,33 @@ func (t *TorrentManager) repair(torrent *Torrent) { t.repairLog.Debugf("Torrent %s has %d broken files (out of %d), repairing by redownloading whole torrent", t.GetKey(torrent), len(brokenFiles), torrent.SelectedFiles.Count()) info, err := t.redownloadTorrent(torrent, []string{}) // reinsert the whole torrent, passing empty selection - if info != nil && info.Progress == 100 && !t.isStillBroken(info, brokenFiles) { - // successful repair - torrent.State.Event(context.Background(), "mark_as_repaired") - t.repairLog.Infof("Successfully repaired torrent %s by redownloading whole torrent", t.GetKey(torrent)) - // delete the torrents it replaced - torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool { - if torrentID != info.ID { - t.setXToBinOnceYDone(torrentID, info.ID) - } - return false - }) - return + if info != nil && info.Progress == 100 { + if !t.isStillBroken(info, brokenFiles) { + // successful repair + torrent.State.Event(context.Background(), "mark_as_repaired") + t.repairLog.Infof("Successfully repaired torrent %s by redownloading whole torrent", t.GetKey(torrent)) + // delete the torrents it replaced + torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool { + if torrentID != info.ID { + t.setXToBinOnceYDone(torrentID, info.ID) + } + return false + }) + return + } + + // if it's still broken, let's delete the newly downloaded torrent + t.setToBinImmediately(info.ID) + err = fmt.Errorf("links are still broken") + } else if info != nil && info.Progress != 100 { - t.repairLog.Infof("Torrent %s is still in progress after redownloading whole torrent but it should be repaired once done", t.GetKey(torrent)) - // once info.ID is done, we can delete the old torrent - torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool { - if torrentID != info.ID { - t.setXToBinOnceYDone(torrentID, info.ID) - } - return false - }) - return + // it's faster to download just the broken files, so let's delete the newly downloaded torrent + t.setToBinImmediately(info.ID) + err = fmt.Errorf("no longer cached") } if err != nil { t.repairLog.Warnf("Cannot repair torrent %s by redownloading whole torrent (error=%v)", t.GetKey(torrent), err) - } else { - t.repairLog.Warnf("Cannot repair torrent %s by redownloading whole torrent (links are still broken)", t.GetKey(torrent)) } if torrent.UnrepairableReason != "" {