Add support for rar extraction
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
"github.com/debridmediamanager/zurg/pkg/utils"
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
)
|
||||
@@ -179,6 +180,25 @@ func (t *TorrentManager) repair(torrent *Torrent) {
|
||||
|
||||
brokenFiles, allBroken := getBrokenFiles(torrent)
|
||||
|
||||
// check if broken files are playable
|
||||
allPlayable := true
|
||||
for _, file := range brokenFiles {
|
||||
if utils.IsPlayable(file.Path) {
|
||||
continue
|
||||
}
|
||||
|
||||
allPlayable = false
|
||||
if t.Config.GetRarAction() == "extract" {
|
||||
info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)})
|
||||
if info != nil {
|
||||
t.setToBinOnceDone(info.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !allPlayable {
|
||||
return
|
||||
}
|
||||
|
||||
// first step: redownload the whole 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())
|
||||
@@ -331,36 +351,62 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
|
||||
t.writeTorrentToFile(torrent)
|
||||
}
|
||||
|
||||
// this is a rar'ed torrent, nothing we can do
|
||||
if assignedCount == 0 && rarCount == 1 {
|
||||
if t.Config.ShouldDeleteRarFiles() {
|
||||
action := t.Config.GetRarAction()
|
||||
|
||||
if action == "delete" {
|
||||
t.repairLog.Warnf("Torrent %s is rar'ed and we cannot repair it, deleting it as configured", t.GetKey(torrent))
|
||||
t.Delete(t.GetKey(torrent), true)
|
||||
return false
|
||||
}
|
||||
|
||||
newUnassignedLinks.IterCb(func(_ string, unassigned *realdebrid.Download) {
|
||||
newFile := &File{
|
||||
File: realdebrid.File{
|
||||
ID: 0,
|
||||
Path: unassigned.Filename,
|
||||
Bytes: unassigned.Filesize,
|
||||
Selected: 0,
|
||||
},
|
||||
Ended: torrent.Added,
|
||||
Link: unassigned.Link,
|
||||
State: NewFileState("ok_file"),
|
||||
}
|
||||
torrent.SelectedFiles.Set(unassigned.Filename, newFile)
|
||||
})
|
||||
|
||||
if action == "extract" {
|
||||
videoFiles := []string{}
|
||||
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
|
||||
if utils.IsPlayable(file.Path) {
|
||||
videoFiles = append(videoFiles, fmt.Sprintf("%d", file.ID))
|
||||
} else {
|
||||
info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)})
|
||||
if info != nil {
|
||||
t.setToBinOnceDone(info.ID)
|
||||
}
|
||||
}
|
||||
})
|
||||
if len(videoFiles) > 0 {
|
||||
info, _ := t.redownloadTorrent(torrent, videoFiles)
|
||||
if info != nil {
|
||||
t.setToBinOnceDone(info.ID)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.repairLog.Warnf("Torrent %s is rar'ed and we cannot repair it", t.GetKey(torrent))
|
||||
newUnassignedLinks.IterCb(func(_ string, unassigned *realdebrid.Download) {
|
||||
newFile := &File{
|
||||
File: realdebrid.File{
|
||||
ID: 0,
|
||||
Path: unassigned.Filename,
|
||||
Bytes: unassigned.Filesize,
|
||||
Selected: 0,
|
||||
},
|
||||
Ended: torrent.Added,
|
||||
Link: unassigned.Link,
|
||||
State: NewFileState("ok_file"),
|
||||
}
|
||||
torrent.SelectedFiles.Set(unassigned.Filename, newFile)
|
||||
})
|
||||
torrent.UnassignedLinks = mapset.NewSet[string]()
|
||||
t.markAsUnfixable(torrent, "rar'ed by RD")
|
||||
t.markAsUnplayable(torrent, "rar'ed by RD")
|
||||
torrent.State.Event(context.Background(), "mark_as_repaired")
|
||||
}
|
||||
|
||||
torrent.UnassignedLinks = mapset.NewSet[string]()
|
||||
torrent.State.Event(context.Background(), "mark_as_repaired")
|
||||
t.writeTorrentToFile(torrent)
|
||||
|
||||
return false // end repair
|
||||
}
|
||||
|
||||
return true
|
||||
return true // continue repair
|
||||
}
|
||||
|
||||
func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string) (*realdebrid.TorrentInfo, error) {
|
||||
@@ -454,7 +500,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
|
||||
return nil, fmt.Errorf("only got %d links but we need %d", len(info.Links), len(selection))
|
||||
}
|
||||
|
||||
t.repairLog.Infof("Redownloading torrent %s successful (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress)
|
||||
t.repairLog.Infof("Redownloading %d file(s) of torrent %s successful (id=%s, progress=%d)", len(selection), t.GetKey(torrent), info.ID, info.Progress)
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user