From 57e6b0b86026ce252e9bb940d0f7b2d0afba247b Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sun, 7 Jan 2024 23:09:02 +0100 Subject: [PATCH] Add downloads dir to http, not sure if working --- internal/config/v1.go | 1 + internal/dav/infuse.go | 5 ++--- internal/dav/listing.go | 7 +++---- internal/http/listing.go | 21 ++++++++++++++++++--- internal/router/router.go | 19 +++++++++++++------ 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/internal/config/v1.go b/internal/config/v1.go index 634fcf0..de7c9ee 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -15,6 +15,7 @@ import ( const ( ALL_TORRENTS = "__all__" UNPLAYABLE_TORRENTS = "__unplayable__" + DOWNLOADS = "__downloads__" ) func loadV1Config(content []byte, log *logutil.Logger) (*ZurgConfigV1, error) { diff --git a/internal/dav/infuse.go b/internal/dav/infuse.go index c02261e..fd409a1 100644 --- a/internal/dav/infuse.go +++ b/internal/dav/infuse.go @@ -9,7 +9,6 @@ import ( "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/pkg/dav" - "github.com/debridmediamanager/zurg/pkg/logutil" ) func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) { @@ -27,7 +26,7 @@ func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) return buf.Bytes(), nil } -func ServeTorrentsListForInfuse(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func ServeTorrentsListForInfuse(directory string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -48,7 +47,7 @@ func ServeTorrentsListForInfuse(directory string, torMgr *torrent.TorrentManager return buf.Bytes(), nil } -func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) diff --git a/internal/dav/listing.go b/internal/dav/listing.go index bbb9f91..e3de5f4 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -10,7 +10,6 @@ import ( "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/pkg/dav" - "github.com/debridmediamanager/zurg/pkg/logutil" ) func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) { @@ -29,7 +28,7 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) { return buf.Bytes(), nil } -func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -51,7 +50,7 @@ func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager, log *lo return buf.Bytes(), nil } -func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -92,7 +91,7 @@ func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManage return buf.Bytes(), nil } -func HandleSingleFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func HandleSingleFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) diff --git a/internal/http/listing.go b/internal/http/listing.go index cb9c4b4..4160746 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -10,7 +10,6 @@ import ( "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/torrent" - "github.com/debridmediamanager/zurg/pkg/logutil" ) func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) { @@ -25,11 +24,14 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) { directoryPath := url.PathEscape(directory) buf.WriteString(fmt.Sprintf("
  • %s
  • ", directoryPath, directory)) } + if torMgr.Config.GetConfig().UseDownloadCache { + buf.WriteString(fmt.Sprintf("
  • %s
  • ", config.DOWNLOADS, config.DOWNLOADS)) + } return buf.Bytes(), nil } -func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -49,7 +51,7 @@ func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager, log *lo return buf.Bytes(), nil } -func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { +func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -88,3 +90,16 @@ func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManage } return buf.Bytes(), nil } + +func ServeDownloadsList(torMgr *torrent.TorrentManager) ([]byte, error) { + var buf bytes.Buffer + if !torMgr.Config.GetConfig().UseDownloadCache { + buf.WriteString("Enable download cache in config to use this feature") + return buf.Bytes(), nil + } + buf.WriteString("
      ") + for _, download := range torMgr.DownloadCache.Items() { + buf.WriteString(fmt.Sprintf("
    1. %s
    2. ", download.Download, download.Filename)) + } + return buf.Bytes(), nil +} diff --git a/internal/router/router.go b/internal/router/router.go index 6053270..4696f73 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -105,9 +105,9 @@ func (zr *ZurgRouter) infuseDavRootHandler(resp http.ResponseWriter, req *http.R zr.handleRootRequest(resp, req, params, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"") } -func (zr *ZurgRouter) handleTorrentsListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, *torrent.TorrentManager, *logutil.Logger) ([]byte, error), contentType string) { +func (zr *ZurgRouter) handleTorrentsListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) { directory := params.ByName("directory") - out, err := handleFunc(directory, zr.torMgr, zr.log) + out, err := handleFunc(directory, zr.torMgr) if err != nil { http.Error(resp, "Not Found", http.StatusNotFound) return @@ -118,7 +118,14 @@ func (zr *ZurgRouter) handleTorrentsListRequest(resp http.ResponseWriter, req *h } func (zr *ZurgRouter) httpTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - zr.handleTorrentsListRequest(resp, req, params, intHttp.ServeTorrentsList, "text/html; charset=\"utf-8\"") + directory := params.ByName("directory") + handlerFunc := intHttp.ServeTorrentsList + if directory == config.DOWNLOADS { + handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { + return intHttp.ServeDownloadsList(torMgr) + } + } + zr.handleTorrentsListRequest(resp, req, params, handlerFunc, "text/html; charset=\"utf-8\"") } func (zr *ZurgRouter) davTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { @@ -129,10 +136,10 @@ func (zr *ZurgRouter) infuseDavTorrentsListHandler(resp http.ResponseWriter, req zr.handleTorrentsListRequest(resp, req, params, dav.ServeTorrentsListForInfuse, "text/xml; charset=\"utf-8\"") } -func (zr *ZurgRouter) handleFilesListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, string, *torrent.TorrentManager, *logutil.Logger) ([]byte, error), contentType string) { +func (zr *ZurgRouter) handleFilesListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) { directory := params.ByName("directory") torrentName := params.ByName("torrent") - out, err := handleFunc(directory, torrentName, zr.torMgr, zr.log) + out, err := handleFunc(directory, torrentName, zr.torMgr) if err != nil { http.Error(resp, "Not Found", http.StatusNotFound) return @@ -179,7 +186,7 @@ func (zr *ZurgRouter) davCheckSingleFileHandler(resp http.ResponseWriter, req *h directory := params.ByName("directory") torrentName := params.ByName("torrent") fileName := params.ByName("file") - out, err := dav.HandleSingleFile(directory, torrentName, fileName, zr.torMgr, zr.log) + out, err := dav.HandleSingleFile(directory, torrentName, fileName, zr.torMgr) if err != nil { fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err) http.Error(resp, "Not Found", http.StatusNotFound)