Repair fixes

This commit is contained in:
Ben Sarmiento
2023-12-07 15:09:04 +01:00
parent b4a366805a
commit b8ec36ff88
2 changed files with 32 additions and 5 deletions

View File

@@ -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
}

View File

@@ -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"`