From 5f65a3873b817641b5f3bdb972dd24c1638a023b Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sun, 26 Nov 2023 13:04:40 +0100 Subject: [PATCH] Proper logging and fixing for unavailable files --- internal/dav/listing.go | 2 +- internal/http/listing.go | 2 +- internal/torrent/manager.go | 4 ++-- internal/torrent/types.go | 12 ++++++++++- internal/universal/get.go | 40 +++++++++++++++++++++++-------------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/internal/dav/listing.go b/internal/dav/listing.go index c1c7ef6..3c1d804 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -72,7 +72,7 @@ func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.To var allTorrents []*torrent.Torrent torrents.IterCb(func(_ string, tor *torrent.Torrent) { - if tor.InProgress() { + if tor.AllInProgress() { return } allTorrents = append(allTorrents, tor) diff --git a/internal/http/listing.go b/internal/http/listing.go index 6f1baa4..b0e2337 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -74,7 +74,7 @@ func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) (*strin var allTorrents []*torrent.Torrent torrents.IterCb(func(_ string, tor *torrent.Torrent) { - if tor.InProgress() { + if tor.AllInProgress() { return } allTorrents = append(allTorrents, tor) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 95e64de..d948348 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -538,7 +538,7 @@ func (t *TorrentManager) repairAll() { allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) allTorrents.IterCb(func(_ string, torrent *Torrent) { - if torrent.InProgress() { + if torrent.AnyInProgress() { t.log.Debugf("Skipping %s for repairs because it is in progress", torrent.AccessKey) return } @@ -588,7 +588,7 @@ func (t *TorrentManager) Repair(accessKey string) { return } - if torrent.InProgress() { + if torrent.AnyInProgress() { t.log.Infof("Torrent %s is in progress, cannot repair", torrent.AccessKey) return } diff --git a/internal/torrent/types.go b/internal/torrent/types.go index 4bc3a59..fab0470 100644 --- a/internal/torrent/types.go +++ b/internal/torrent/types.go @@ -13,7 +13,7 @@ type Torrent struct { Instances []realdebrid.TorrentInfo } -func (t *Torrent) InProgress() bool { +func (t *Torrent) AnyInProgress() bool { for _, instance := range t.Instances { if instance.Progress < 100 { return true @@ -22,6 +22,16 @@ func (t *Torrent) InProgress() bool { return false } +func (t *Torrent) AllInProgress() bool { + count := 0 + for _, instance := range t.Instances { + if instance.Progress < 100 { + count++ + } + } + return count == len(t.Instances) +} + type File struct { realdebrid.File Added string diff --git a/internal/universal/get.go b/internal/universal/get.go index 128a072..f640e29 100644 --- a/internal/universal/get.go +++ b/internal/universal/get.go @@ -85,18 +85,21 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i if !strings.HasPrefix(file.Link, "http") { // This is a dead file, serve an alternate file - log.Warnf("File %s is not yet available, zurg is repairing the torrent", filename) - gf.playErrorVideo("https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, t, c, log) + log.Warnf("File %s is not available", filename) + gf.playErrorVideo(file, "https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, t, c, log) return } link := file.Link resp := t.UnrestrictUntilOk(link) if resp == nil { - log.Warnf("File %s is no longer available, file is marked for repair", filepath.Base(file.Path)) + log.Warnf("File %s is no longer available", filepath.Base(file.Path)) file.Link = "repair" - t.SetChecksum("") // force a recheck - gf.playErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log) + if c.EnableRepair() { + log.Debugf("File %s is marked for repair", filepath.Base(file.Path)) + t.SetChecksum("") // force a recheck + } + gf.playErrorVideo(file, "https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log) } else { if resp.Filename != filename { // this is possible if there's only 1 streamable file in the torrent @@ -105,7 +108,7 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i expectedExt := filepath.Ext(filename) if actualExt != expectedExt && resp.Streamable != 1 { log.Warnf("File was changed and is not streamable: %s and %s", filename, resp.Filename) - gf.playErrorVideo("https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, t, c, log) + gf.playErrorVideo(file, "https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, t, c, log) return } else { log.Warnf("Filename mismatch: %s and %s", filename, resp.Filename) @@ -128,7 +131,7 @@ func (gf *GetFile) streamFileToResponse(file *intTor.File, url string, w http.Re if file != nil { log.Errorf("Error creating new request for file %s: %v", file.Path, err) } - gf.playErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, torMgr, cfg, log) + gf.playErrorVideo(file, "https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, torMgr, cfg, log) return } @@ -140,22 +143,28 @@ func (gf *GetFile) streamFileToResponse(file *intTor.File, url string, w http.Re resp, err := gf.client.Do(req) if err != nil { if file != nil { - log.Warnf("Cannot download file %s, file is marked for repair: %v", file.Path, err) + log.Warnf("Cannot download file %s: %v", file.Path, err) file.Link = "repair" - torMgr.SetChecksum("") // force a recheck + if cfg.EnableRepair() { + log.Debugf("File %s is marked for repair", filepath.Base(file.Path)) + torMgr.SetChecksum("") // force a recheck + } } - gf.playErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, torMgr, cfg, log) + gf.playErrorVideo(file, "https://www.youtube.com/watch?v=FSSd8cponAA", w, r, torMgr, cfg, log) return } defer resp.Body.Close() if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { if file != nil { - log.Warnf("Received a %s status code for file %s, file is marked for repair", resp.Status, file.Path) + log.Warnf("Received a %s status code for file %s", resp.Status, file.Path) file.Link = "repair" - torMgr.SetChecksum("") // force a recheck + if cfg.EnableRepair() { + log.Debugf("File %s is marked for repair", filepath.Base(file.Path)) + torMgr.SetChecksum("") // force a recheck + } } - gf.playErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, torMgr, cfg, log) + gf.playErrorVideo(file, "https://www.youtube.com/watch?v=BcseUxviVqE", w, r, torMgr, cfg, log) return } @@ -169,13 +178,14 @@ func (gf *GetFile) streamFileToResponse(file *intTor.File, url string, w http.Re io.CopyBuffer(w, resp.Body, buf) } -func (gf *GetFile) playErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) { +func (gf *GetFile) playErrorVideo(file *intTor.File, link string, w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) { resp := t.UnrestrictUntilOk(link) if resp == nil { http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError) return } - log.Debugf("Serving error video %s", link) + log.Debugf("Serving error video %s for file %s", link, filepath.Base(file.Path)) + file.Bytes = resp.Filesize if c.ShouldServeFromRclone() { redirect(w, r, resp.Download, c) } else {