Repair rework for rar files

This commit is contained in:
Ben Sarmiento
2024-01-07 14:24:21 +01:00
parent e5f0f4d7bd
commit 72f7b0f1e5
5 changed files with 97 additions and 73 deletions

View File

@@ -5,6 +5,8 @@ import (
"math"
"strings"
"time"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
)
const EXPIRED_LINK_TOLERANCE_HOURS = 24
@@ -82,6 +84,57 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// sleep for 30 seconds to let the torrent accumulate more broken files if scanning
time.Sleep(30 * time.Second)
// handle rar'ed torrents
assignedCount := 0
rarCount := 0
unassignedDownloads := make([]*realdebrid.Download, 0)
torrent.UnassignedLinks.Each(func(link string) bool {
unrestrict := t.UnrestrictUntilOk(link)
if unrestrict != nil && unrestrict.Link != "" {
// assign to a selected file
assigned := false
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
// if strings.HasSuffix(file.Path, unrestrict.Filename) {
if file.Bytes == unrestrict.Filesize {
file.Link = unrestrict.Link
assigned = true
assignedCount++
}
})
if !assigned {
if strings.HasSuffix(unrestrict.Filename, ".rar") {
rarCount++
}
unassignedDownloads = append(unassignedDownloads, unrestrict)
}
}
return false
})
if assignedCount > 0 {
t.log.Infof("Assigned %d links to selected files for torrent %s", assignedCount, torrent.AccessKey)
} else if rarCount > 0 {
if t.Config.ShouldDeleteRarFiles() {
t.Delete(torrent.AccessKey, true)
} else {
for _, unassigned := range unassignedDownloads {
newFile := &File{
File: realdebrid.File{
ID: 0,
Path: unassigned.Filename,
Bytes: unassigned.Filesize,
Selected: 1,
},
Ended: torrent.LatestAdded,
Link: unassigned.Link,
}
torrent.SelectedFiles.Set(unassigned.Filename, newFile)
}
}
return
}
// second solution: add only the broken files
var brokenFiles []File
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
@@ -94,8 +147,6 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// todo: to verify removed logic when there's only 1 selected file selected and it's broken
// todo: handle rar'ed torrents
if len(brokenFiles) == 1 && torrent.SelectedFiles.Count() > 1 {
// if we download a single file, it will be named differently
// so we need to download 1 extra file to preserve the name
@@ -109,6 +160,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
}
}
}
if len(brokenFiles) > 0 {
t.log.Infof("Redownloading the %d broken files for torrent %s", len(brokenFiles), torrent.AccessKey)
brokenFileIDs := strings.Join(getFileIDs(brokenFiles), ",")