From 8921b33b6a850f1dbb425461ef23f5839d066a54 Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Thu, 20 Jun 2024 04:21:57 +0200 Subject: [PATCH] New rules for extraction --- internal/torrent/refresh.go | 7 +++++-- internal/torrent/repair.go | 40 ++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 6a872aa..b3aeafe 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -319,6 +319,9 @@ func (t *TorrentManager) mergeTorrents(existing, toMerge *Torrent) *Torrent { brokenCount := 0 okCount := 0 mergedTorrent.SelectedFiles.IterCb(func(key string, file *File) { + if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) { + return + } if file.State.Is("broken_file") { brokenCount++ } 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") } - 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 } @@ -392,8 +395,8 @@ func (t *TorrentManager) assignDirectory(tor *Torrent, triggerHook bool) { } func (t *TorrentManager) IsPlayable(filePath string) bool { - filePath = strings.ToLower(filePath) playableExts := t.Config.GetPlayableExtensions() + filePath = strings.ToLower(filePath) for _, ext := range playableExts { if strings.HasSuffix(filePath, fmt.Sprintf(".%s", ext)) { return true diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index a4832e6..039428c 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -130,6 +130,9 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) { // check 1: for broken files brokenFileCount := 0 torrent.SelectedFiles.IterCb(func(_ string, file *File) { + if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) { + return + } if file.State.Is("broken_file") { brokenFileCount++ } @@ -142,10 +145,14 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) { // check 2: for unassigned links (this means the torrent has started to deteriorate) unassignedCount := torrent.UnassignedLinks.Cardinality() 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) 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 allPlayable := true + videoFiles := []string{} 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 } - allPlayable = false if t.Config.GetRarAction() == "extract" && file.ID != 0 { 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 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 } @@ -296,7 +314,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) { func (t *TorrentManager) assignLinks(torrent *Torrent) bool { 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 assignedCount := 0 @@ -396,7 +414,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool { torrent.SelectedFiles.IterCb(func(_ string, file *File) { if utils.IsVideo(file.Path) { 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)) info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)}) if info != nil { @@ -405,7 +423,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool { } }) 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) if info != nil { t.setToBinOnceDone(info.ID) @@ -516,7 +534,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string) } else if info.Progress != 100 { t.repairLog.Infof("Downloading torrent %s (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress) } 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 @@ -585,13 +603,17 @@ func (t *TorrentManager) markAsUnfixable(torrent *Torrent, reason string) { } // 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 allBroken := true torrent.SelectedFiles.IterCb(func(_ string, file *File) { + if !utils.IsVideo(file.Path) && !t.IsPlayable(file.Path) { + return + } if file.State.Is("broken_file") { brokenFiles = append(brokenFiles, file) } else { + // file is ok allBroken = false } })