From b8ec36ff8834335427e3e5e27d507f6fd2b7dede Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Thu, 7 Dec 2023 15:09:04 +0100 Subject: [PATCH] Repair fixes --- internal/torrent/repair.go | 26 +++++++++++++++++++++----- internal/torrent/types.go | 11 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index b123120..74f3e47 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -48,7 +48,7 @@ func (t *TorrentManager) Repair(torrent *Torrent) { } func (t *TorrentManager) repair(torrent *Torrent) { - if torrent.AllInProgress() { + if torrent.AnyInProgress() { t.log.Infof("Torrent %s is in progress, skipping repair until download is done", torrent.AccessKey) return } @@ -59,10 +59,17 @@ func (t *TorrentManager) repair(torrent *Torrent) { return } - // first solution: reinsert with same selection - if t.reinsertTorrent(torrent, "") { - t.log.Infof("Successfully downloaded torrent %s to repair it", torrent.AccessKey) - return + expiredLinkTolerance := 24 * time.Hour + if torrent.OlderThanDuration(expiredLinkTolerance) { + // first solution: reinsert with same selection + if t.reinsertTorrent(torrent, "") { + t.log.Infof("Successfully downloaded torrent %s to repair it", torrent.AccessKey) + return + } else { + t.log.Warn("Failed to repair by reinserting torrent") + } + } else { + t.log.Infof("Torrent %s is not older than %d hours to be repaired by reinsertion, skipping", torrent.AccessKey, expiredLinkTolerance.Hours()) } // second solution: add only the missing files @@ -103,9 +110,12 @@ func (t *TorrentManager) repair(torrent *Torrent) { } func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string) bool { + oldTorrentIDs := make([]string, 0) // missing files means missing links // if missingFiles is not provided if missingFiles == "" { + // only replace the torrent if we are reinserting all files + oldTorrentIDs = torrent.DownloadedIDs.List() tmpSelection := "" torrent.SelectedFiles.IterCb(func(_ string, file *File) { tmpSelection += fmt.Sprintf("%d,", file.ID) // select all files @@ -151,6 +161,9 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string) if info.Progress != 100 { t.log.Infof("Torrent id=%s is not cached anymore so we have to wait until completion (this should fix the issue already)", info.ID) + for _, id := range oldTorrentIDs { + t.Api.DeleteTorrent(id) + } return true } @@ -162,6 +175,9 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string) } t.log.Infof("Repair successful id=%s", newTorrentID) + for _, id := range oldTorrentIDs { + t.Api.DeleteTorrent(id) + } return true } diff --git a/internal/torrent/types.go b/internal/torrent/types.go index 7b3e5f6..dffbf70 100644 --- a/internal/torrent/types.go +++ b/internal/torrent/types.go @@ -2,7 +2,9 @@ package torrent import ( stdjson "encoding/json" + "fmt" "strings" + "time" "github.com/debridmediamanager/zurg/pkg/realdebrid" jsoniter "github.com/json-iterator/go" @@ -113,6 +115,15 @@ func (t *Torrent) ComputeTotalSize() int64 { return totalSize } +func (t *Torrent) OlderThanDuration(duration time.Duration) bool { + latestAdded, err := time.Parse(time.RFC3339, t.LatestAdded) + if err != nil { + fmt.Println("Error parsing time:", err) + return false + } + return time.Since(latestAdded) > duration +} + type File struct { realdebrid.File Added string `json:"Added"`