From 1373bbb9750ba37bf8849d227ed9ba6f7f49b31e Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Sat, 6 Jul 2024 23:56:12 +0200 Subject: [PATCH] Add setting for hiding broken torrents --- internal/config/types.go | 6 ++++++ internal/dav/infuse.go | 5 ++++- internal/dav/listing.go | 5 ++++- internal/handlers/router.go | 4 ++-- internal/http/listing.go | 5 ++++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/config/types.go b/internal/config/types.go index 04cdd21..594c0de 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -39,6 +39,7 @@ type ConfigInterface interface { ShouldIgnoreRenames() bool ShouldLogRequests() bool ShouldServeFromRclone() bool + ShouldHideBrokenTorrents() bool } type ZurgConfig struct { @@ -71,6 +72,7 @@ type ZurgConfig struct { RetainRDTorrentName bool `yaml:"retain_rd_torrent_name" json:"retain_rd_torrent_name"` RetriesUntilFailed int `yaml:"retries_until_failed" json:"retries_until_failed"` ServeFromRclone bool `yaml:"serve_from_rclone" json:"serve_from_rclone"` + HideBrokenTorrents bool `yaml:"show_broken_torrents" json:"show_broken_torrents"` Username string `yaml:"username" json:"username"` } @@ -232,3 +234,7 @@ func (z *ZurgConfig) ShouldLogRequests() bool { func (z *ZurgConfig) GetDownloadTokens() []string { return z.DownloadTokens } + +func (z *ZurgConfig) ShouldHideBrokenTorrents() bool { + return z.HideBrokenTorrents +} diff --git a/internal/dav/infuse.go b/internal/dav/infuse.go index ca2fcbf..a3f1c78 100644 --- a/internal/dav/infuse.go +++ b/internal/dav/infuse.go @@ -59,7 +59,7 @@ func ServeTorrentsListForInfuse(directory string, torMgr *torrent.TorrentManager return buf.Bytes(), nil } -func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -88,6 +88,9 @@ func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.Torr if file.State.Is("deleted_file") { continue } + if file.State.Is("broken_file") && shouldHideBrokenTorrents { + continue + } if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize { continue } diff --git a/internal/dav/listing.go b/internal/dav/listing.go index 267332a..e26f2a9 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -54,7 +54,7 @@ func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager) ([]byte return buf.Bytes(), nil } -func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -80,6 +80,9 @@ func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManage if file.State.Is("deleted_file") { continue } + if file.State.Is("broken_file") && shouldHideBrokenTorrents { + continue + } if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize { continue } diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 062d5b9..69d24a2 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -220,7 +220,7 @@ func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http // handle files list request -func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) { +func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager, bool) ([]byte, error), contentType string) { directory, err := url.PathUnescape(chi.URLParam(req, "directory")) if err != nil { directory = chi.URLParam(req, "directory") @@ -229,7 +229,7 @@ func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Re if err != nil { torrentName = chi.URLParam(req, "torrent") } - out, err := handleFunc(directory, torrentName, hs.torMgr) + out, err := handleFunc(directory, torrentName, hs.torMgr, hs.cfg.ShouldHideBrokenTorrents()) if err != nil && strings.Contains(contentType, "xml") { hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL) resp.WriteHeader(http.StatusNotImplemented) diff --git a/internal/http/listing.go b/internal/http/listing.go index d5b2b08..b403f8d 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -51,7 +51,7 @@ func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager) ([]byte return buf.Bytes(), nil } -func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -76,6 +76,9 @@ func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManage if !ok || file.State.Is("deleted_file") { continue } + if file.State.Is("broken_file") && shouldHideBrokenTorrents { + continue + } if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize { continue }