This commit is contained in:
Ben Sarmiento
2024-05-23 19:29:16 +02:00
parent 2a5f12e37f
commit d03b59bb2a
10 changed files with 275 additions and 226 deletions

View File

@@ -150,10 +150,7 @@ func (t *TorrentManager) Repair(torrent *Torrent, wg *sync.WaitGroup) {
}
func (t *TorrentManager) repair(torrent *Torrent) {
torrentIDs := []string{}
for id := range torrent.Components {
torrentIDs = append(torrentIDs, id)
}
torrentIDs := torrent.Components.Keys()
t.log.Infof("Started repair process for torrent %s (ids=%v)", t.GetKey(torrent), torrentIDs)
// handle torrents with incomplete links for selected files
@@ -177,12 +174,11 @@ func (t *TorrentManager) repair(torrent *Torrent) {
return
}
// delete old torrents
for id := range torrent.Components {
if id == info.ID {
continue
torrent.Components.IterCb(func(id string, _ *realdebrid.TorrentInfo) {
if id != info.ID {
t.api.DeleteTorrent(id)
}
t.api.DeleteTorrent(id)
}
})
t.log.Infof("Successfully repaired torrent %s by redownloading all files", t.GetKey(torrent))
return
} else if info != nil && info.Progress != 100 {
@@ -210,18 +206,18 @@ func (t *TorrentManager) repair(torrent *Torrent) {
return
}
t.log.Infof("Repairing by downloading %d batches of the %d broken files of torrent %s", int(math.Ceil(float64(len(brokenFiles))/130)), len(brokenFiles), t.GetKey(torrent))
t.log.Infof("Repairing by downloading %d batches of the %d broken files of torrent %s", int(math.Ceil(float64(len(brokenFiles))/100)), len(brokenFiles), t.GetKey(torrent))
newlyDownloadedIds := make([]string, 0)
group := make([]*File, 0)
batchNum := 1
for _, file := range brokenFiles {
group = append(group, file)
if len(group) >= 130 {
brokenFileIDs := getFileIDs(brokenFiles)
var group []string
for _, fileIDStr := range brokenFileIDs {
group = append(group, fileIDStr)
if len(group) >= 100 {
t.log.Debugf("Downloading batch %d of broken files of torrent %s", batchNum, t.GetKey(torrent))
batchNum++
brokenFileIDs := getFileIDs(group)
redownloadedInfo, err := t.redownloadTorrent(torrent, brokenFileIDs)
redownloadedInfo, err := t.redownloadTorrent(torrent, group)
if err != nil {
t.log.Warnf("Cannot repair torrent %s by downloading broken files (error=%s) giving up", t.GetKey(torrent), err.Error())
for _, newId := range newlyDownloadedIds {
@@ -230,15 +226,14 @@ func (t *TorrentManager) repair(torrent *Torrent) {
return
}
newlyDownloadedIds = append(newlyDownloadedIds, redownloadedInfo.ID)
group = make([]*File, 0)
group = make([]string, 0)
}
}
t.log.Debugf("Downloading last batch of broken files of torrent %s", t.GetKey(torrent))
if len(group) > 0 {
brokenFileIDs := getFileIDs(group)
_, err := t.redownloadTorrent(torrent, brokenFileIDs)
_, err := t.redownloadTorrent(torrent, group)
if err != nil {
t.log.Warnf("Cannot repair torrent %s by downloading broken files (error=%s) giving up", t.GetKey(torrent), err.Error())
for _, newId := range newlyDownloadedIds {
@@ -276,6 +271,9 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
// base it on size because why not?
if !assigned && file.State.Is("broken_file") && file.Bytes == unrestrict.Filesize && strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename)) {
file.Link = link
if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") {
file.Link = file.Link[0:39]
}
if err := file.State.Event(context.Background(), "repair_file"); err != nil {
t.log.Errorf("Failed to mark file %s as repaired: %v", file.Path, err)
return
@@ -351,17 +349,14 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
// broken files means broken links
// if brokenFiles is not provided, we will redownload all files
finalSelection := strings.Join(selection, ",")
selectionCount := len(selection)
if selectionCount == 0 {
tmpSelection := ""
if len(selection) == 0 {
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
tmpSelection += fmt.Sprintf("%d,", file.ID) // select all files
selectionCount++
selection = append(selection, fmt.Sprintf("%d", file.ID))
})
if tmpSelection == "" {
return nil, nil // nothing to repair
if len(selection) == 0 {
return nil, nil
} else {
finalSelection = tmpSelection[:len(tmpSelection)-1]
finalSelection = strings.Join(selection, ",")
}
}
@@ -434,9 +429,9 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
}
// check if incorrect number of links
if info.Progress == 100 && len(info.Links) != selectionCount {
if info.Progress == 100 && len(info.Links) != len(selection) {
t.trash(newTorrentID)
return nil, fmt.Errorf("torrent %s only got %d links but we need %d", t.GetKey(torrent), len(info.Links), selectionCount)
return nil, fmt.Errorf("torrent %s only got %d links but we need %d", t.GetKey(torrent), len(info.Links), len(selection))
}
t.log.Infof("Redownloading torrent %s successful (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress)
@@ -544,6 +539,9 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles
// all links are still intact! good!
for i, file := range selectedFiles {
file.Link = info.Links[i]
if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") {
file.Link = file.Link[0:39]
}
if err := file.State.Event(context.Background(), "repair_file"); err != nil {
t.log.Errorf("Failed to mark file %s as repaired: %v", file.Path, err)
return true