New rules for extraction

This commit is contained in:
Ben Adrian Sarmiento
2024-06-20 04:21:57 +02:00
parent 3e9afe3278
commit 8921b33b6a
2 changed files with 36 additions and 11 deletions

View File

@@ -319,6 +319,9 @@ func (t *TorrentManager) mergeTorrents(existing, toMerge *Torrent) *Torrent {
brokenCount := 0 brokenCount := 0
okCount := 0 okCount := 0
mergedTorrent.SelectedFiles.IterCb(func(key string, file *File) { mergedTorrent.SelectedFiles.IterCb(func(key string, file *File) {
if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) {
return
}
if file.State.Is("broken_file") { if file.State.Is("broken_file") {
brokenCount++ brokenCount++
} else if file.State.Is("ok_file") { } else if file.State.Is("ok_file") {
@@ -330,7 +333,7 @@ func (t *TorrentManager) mergeTorrents(existing, toMerge *Torrent) *Torrent {
mergedTorrent.State.Event(context.Background(), "mark_as_repaired") mergedTorrent.State.Event(context.Background(), "mark_as_repaired")
} }
t.log.Debugf("Merged torrent %s has %d broken files", t.GetKey(mergedTorrent), brokenCount) t.log.Debugf("After merging, torrent %s has %d broken file(s)", t.GetKey(mergedTorrent), brokenCount)
return mergedTorrent return mergedTorrent
} }
@@ -392,8 +395,8 @@ func (t *TorrentManager) assignDirectory(tor *Torrent, triggerHook bool) {
} }
func (t *TorrentManager) IsPlayable(filePath string) bool { func (t *TorrentManager) IsPlayable(filePath string) bool {
filePath = strings.ToLower(filePath)
playableExts := t.Config.GetPlayableExtensions() playableExts := t.Config.GetPlayableExtensions()
filePath = strings.ToLower(filePath)
for _, ext := range playableExts { for _, ext := range playableExts {
if strings.HasSuffix(filePath, fmt.Sprintf(".%s", ext)) { if strings.HasSuffix(filePath, fmt.Sprintf(".%s", ext)) {
return true return true

View File

@@ -130,6 +130,9 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) {
// check 1: for broken files // check 1: for broken files
brokenFileCount := 0 brokenFileCount := 0
torrent.SelectedFiles.IterCb(func(_ string, file *File) { torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) {
return
}
if file.State.Is("broken_file") { if file.State.Is("broken_file") {
brokenFileCount++ brokenFileCount++
} }
@@ -142,10 +145,14 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) {
// check 2: for unassigned links (this means the torrent has started to deteriorate) // check 2: for unassigned links (this means the torrent has started to deteriorate)
unassignedCount := torrent.UnassignedLinks.Cardinality() unassignedCount := torrent.UnassignedLinks.Cardinality()
if unassignedCount > 0 { if unassignedCount > 0 {
t.repairLog.Debugf("Torrent %s has %d unassigned links, adding to repair list", t.GetKey(torrent), unassignedCount) t.repairLog.Debugf("Torrent %s has %d unassigned link(s), adding to repair list", t.GetKey(torrent), unassignedCount)
toRepair.Add(torrent) toRepair.Add(torrent)
return return
} }
// if marked as broken, but no broken files or unassigned links, mark as repaired
if torrent.State.Is("broken_torrent") {
torrent.State.Event(context.Background(), "mark_as_repaired")
}
}) })
}) })
@@ -192,15 +199,19 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
} }
}) })
brokenFiles, allBroken := getBrokenFiles(torrent) brokenFiles, allBroken := t.getBrokenFiles(torrent)
// check if broken files are playable // check if broken files are playable
allPlayable := true allPlayable := true
videoFiles := []string{}
for _, file := range brokenFiles { for _, file := range brokenFiles {
if utils.IsVideo(file.Path) { if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) {
continue
}
if utils.IsVideo(file.Path) {
videoFiles = append(videoFiles, fmt.Sprintf("%d", file.ID))
continue continue
} }
allPlayable = false allPlayable = false
if t.Config.GetRarAction() == "extract" && file.ID != 0 { if t.Config.GetRarAction() == "extract" && file.ID != 0 {
t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent)) t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent))
@@ -211,6 +222,13 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
} }
} }
if !allPlayable { if !allPlayable {
if len(videoFiles) > 0 {
t.repairLog.Debugf("Extracting %d video file(s) from rar'ed torrent %s", len(videoFiles), t.GetKey(torrent))
info, _ := t.redownloadTorrent(torrent, videoFiles)
if info != nil {
t.setToBinOnceDone(info.ID)
}
}
return return
} }
@@ -296,7 +314,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
func (t *TorrentManager) assignLinks(torrent *Torrent) bool { func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
unassignedTotal := torrent.UnassignedLinks.Cardinality() unassignedTotal := torrent.UnassignedLinks.Cardinality()
t.repairLog.Infof("Trying to assign %d links to the %d selected files of incomplete torrent %s", unassignedTotal, torrent.SelectedFiles.Count(), t.GetKey(torrent)) t.repairLog.Infof("Trying to assign %d link(s) to the %d selected files of incomplete torrent %s", unassignedTotal, torrent.SelectedFiles.Count(), t.GetKey(torrent))
// handle torrents with incomplete links for selected files // handle torrents with incomplete links for selected files
assignedCount := 0 assignedCount := 0
@@ -396,7 +414,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
torrent.SelectedFiles.IterCb(func(_ string, file *File) { torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if utils.IsVideo(file.Path) { if utils.IsVideo(file.Path) {
videoFiles = append(videoFiles, fmt.Sprintf("%d", file.ID)) videoFiles = append(videoFiles, fmt.Sprintf("%d", file.ID))
} else if file.ID != 0 { } else if file.ID != 0 && t.IsPlayable(file.Path) {
t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent)) t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent))
info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)}) info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)})
if info != nil { if info != nil {
@@ -405,7 +423,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
} }
}) })
if len(videoFiles) > 0 { if len(videoFiles) > 0 {
t.repairLog.Debugf("Extracting %d video files from rar'ed torrent %s", len(videoFiles), t.GetKey(torrent)) t.repairLog.Debugf("Extracting %d video file(s) from rar'ed torrent %s", len(videoFiles), t.GetKey(torrent))
info, _ := t.redownloadTorrent(torrent, videoFiles) info, _ := t.redownloadTorrent(torrent, videoFiles)
if info != nil { if info != nil {
t.setToBinOnceDone(info.ID) t.setToBinOnceDone(info.ID)
@@ -516,7 +534,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
} else if info.Progress != 100 { } else if info.Progress != 100 {
t.repairLog.Infof("Downloading torrent %s (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress) t.repairLog.Infof("Downloading torrent %s (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress)
} else { } else {
t.repairLog.Infof("Downloaded %d file(s) of torrent %s (id=%s)", len(selection), t.GetKey(torrent), info.ID, info.Progress) t.repairLog.Infof("Downloaded %d file(s) of torrent %s (id=%s)", len(selection), t.GetKey(torrent), info.ID)
} }
return info, nil return info, nil
@@ -585,13 +603,17 @@ func (t *TorrentManager) markAsUnfixable(torrent *Torrent, reason string) {
} }
// getBrokenFiles returns the files that are not http links and not deleted // getBrokenFiles returns the files that are not http links and not deleted
func getBrokenFiles(torrent *Torrent) ([]*File, bool) { func (t *TorrentManager) getBrokenFiles(torrent *Torrent) ([]*File, bool) {
var brokenFiles []*File var brokenFiles []*File
allBroken := true allBroken := true
torrent.SelectedFiles.IterCb(func(_ string, file *File) { torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) {
return
}
if file.State.Is("broken_file") { if file.State.Is("broken_file") {
brokenFiles = append(brokenFiles, file) brokenFiles = append(brokenFiles, file)
} else { } else {
// file is ok
allBroken = false allBroken = false
} }
}) })