diff --git a/internal/app.go b/internal/app.go index 3282e63..6e40e3c 100644 --- a/internal/app.go +++ b/internal/app.go @@ -20,6 +20,7 @@ import ( "github.com/debridmediamanager/zurg/pkg/utils" "github.com/go-chi/chi/v5" "github.com/panjf2000/ants/v2" + "go.uber.org/zap/zapcore" ) func MainApp(configPath string) { @@ -28,6 +29,10 @@ func MainApp(configPath string) { log := logutil.NewLogger(logPath) zurglog := log.Named("zurg") + if log.Level() == zapcore.DebugLevel { + zurglog.Infof("debug logging is enabled; if you are not debugging please set LOG_LEVEL=info in your environment") + } + zurglog.Debugf("PID: %d", os.Getpid()) zurglog.Infof("Version: %s", version.GetVersion()) zurglog.Infof("GitCommit: %s", version.GetGitCommit()) diff --git a/internal/config/v1.go b/internal/config/v1.go index 175e85c..46a42d7 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -246,9 +246,6 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentSize int64, torren fileNames[i] = "" } } - if strings.Contains(torrentName, "Hellraiser") { - fmt.Printf("+%v\n", fileNames) - } return checkArithmeticSequenceInFilenames(fileNames) } if filter.FileInsideSizeGreaterThanOrEqual > 0 { diff --git a/internal/torrent/delete.go b/internal/torrent/delete.go index a42a72f..bb46b4b 100644 --- a/internal/torrent/delete.go +++ b/internal/torrent/delete.go @@ -12,9 +12,7 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { if len(unselectedIDs) == torrent.SelectedFiles.Count() && len(unselectedIDs) > 0 { return true } else if len(unselectedIDs) > 0 { - infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) - torrent.DownloadedIDs.Each(func(id string) bool { - info, _ := infoCache.Get(id) + t.saveTorrentChangesToDisk(torrent, func(info *Torrent) { info.SelectedFiles.IterCb(func(_ string, file *File) { for _, unselectedID := range unselectedIDs { if file.ID == unselectedID { @@ -23,8 +21,6 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { } } }) - t.writeTorrentToFile(id, info) - return false }) } return false diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 92d6b71..223bf51 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -201,3 +201,18 @@ func (t *TorrentManager) initializeDirectories() { t.DirectoryMap.Set(directory, cmap.New[*Torrent]()) } } + +func (t *TorrentManager) saveTorrentChangesToDisk(torrent *Torrent, cb func(*Torrent)) { + infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) + torrent.DownloadedIDs.Union(torrent.InProgressIDs).Each(func(id string) bool { + info, exists := infoCache.Get(id) + if !exists { + return false + } + if cb != nil { + cb(info) + } + t.writeTorrentToFile(id, info) + return false + }) +} diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 8358479..af8c427 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -234,7 +234,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { torrent.BrokenLinks = mapset.NewSet[string]() infoCache.Set(rdTorrent.ID, &torrent) - t.writeTorrentToFile(rdTorrent.ID, &torrent) + t.saveTorrentChangesToDisk(&torrent, nil) return &torrent } @@ -290,18 +290,6 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { } }) - // broken links - if mainTorrent.BrokenLinks.Cardinality() > 0 { - mainTorrent.SelectedFiles.IterCb(func(_ string, file *File) { - mainTorrent.BrokenLinks.Each(func(brokenLink string) bool { - if file.Link == brokenLink { - file.Link = "" - } - return file.Link == brokenLink - }) - }) - } - if existing.Added < toMerge.Added { mainTorrent.Added = toMerge.Added } else { diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 2dc6ee0..d94dc05 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -92,9 +92,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) { // save the broken files to the file cache // broken files are also added when trying to open a file if torrent.BrokenLinks.Cardinality() > 0 { - infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) - torrent.DownloadedIDs.Each(func(id string) bool { - info, _ := infoCache.Get(id) + t.saveTorrentChangesToDisk(torrent, func(info *Torrent) { hasBrokenFiles := false info.SelectedFiles.IterCb(func(_ string, file *File) { torrent.BrokenLinks.Each(func(brokenLink string) bool { @@ -108,9 +106,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) { }) if hasBrokenFiles { info.BrokenLinks = torrent.BrokenLinks - t.writeTorrentToFile(id, info) } - return false }) } @@ -183,6 +179,7 @@ func (t *TorrentManager) repair(torrent *Torrent) { // handle torrents with incomplete links for selected files if !t.assignUnassignedLinks(torrent) { + t.log.Debugf("Ending repair process early for torrent %s", t.GetKey(torrent)) return } @@ -206,6 +203,9 @@ func (t *TorrentManager) repair(torrent *Torrent) { ix++ }) torrent.BrokenLinks = mapset.NewSet[string]() + t.saveTorrentChangesToDisk(torrent, func(info *Torrent) { + info.BrokenLinks = mapset.NewSet[string]() + }) t.log.Infof("Successfully repaired torrent %s using repair_method#1", t.GetKey(torrent)) return } @@ -294,17 +294,12 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool { t.markAsUnfixable(torrent, "rar'ed by RD") t.markAsUnplayable(torrent, "rar'ed by RD") } - t.log.Debugf("Ending repair process early for torrent %s", t.GetKey(torrent)) return false } // empty the unassigned links as we have assigned them if torrent.UnassignedLinks.Cardinality() > 0 { - infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) - torrent.DownloadedIDs.Each(func(id string) bool { - info, _ := infoCache.Get(id) + t.saveTorrentChangesToDisk(torrent, func(info *Torrent) { info.UnassignedLinks = mapset.NewSet[string]() - t.writeTorrentToFile(id, info) - return false }) } @@ -469,12 +464,8 @@ func (t *TorrentManager) markAsUnplayable(torrent *Torrent, reason string) { func (t *TorrentManager) markAsUnfixable(torrent *Torrent, reason string) { t.log.Warnf("Marking torrent %s as unfixable - %s", t.GetKey(torrent), reason) torrent.UnrepairableReason = reason - infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) - torrent.DownloadedIDs.Each(func(id string) bool { - info, _ := infoCache.Get(id) - info.UnrepairableReason = reason - t.writeTorrentToFile(id, info) - return false + t.saveTorrentChangesToDisk(torrent, func(t *Torrent) { + t.UnrepairableReason = reason }) } @@ -562,6 +553,9 @@ func (t *TorrentManager) handleFixers(fixer realdebrid.Torrent) *Torrent { } }) torrent.BrokenLinks = mapset.NewSet[string]() + t.saveTorrentChangesToDisk(torrent, func(info *Torrent) { + info.BrokenLinks = mapset.NewSet[string]() + }) t.log.Infof("Successfully repaired torrent %s using repair_method#2", t.GetKey(torrent)) } else { t.log.Warnf("repair_method#2: Fixer is done but torrent %s is still broken; let's keep the fixer", t.GetKey(torrent))