From 62de3723c1cff1b546a50bfc8a86056491e8a9ee Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sun, 7 Jan 2024 23:44:26 +0100 Subject: [PATCH] Serve downloads list --- internal/dav/infuse.go | 15 +++++++++++++++ internal/dav/listing.go | 16 ++++++++++++++++ internal/router/router.go | 23 +++++++++++++++++++++-- pkg/realdebrid/types.go | 16 ++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/internal/dav/infuse.go b/internal/dav/infuse.go index fd409a1..fbd7cb2 100644 --- a/internal/dav/infuse.go +++ b/internal/dav/infuse.go @@ -22,6 +22,7 @@ func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) } buf.WriteString(dav.BaseDirectory(directory, "")) } + buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, "")) buf.WriteString("") return buf.Bytes(), nil } @@ -86,3 +87,17 @@ func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.Torr buf.WriteString("") return buf.Bytes(), nil } + +func ServeDownloadsListForInfuse(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(dav.File(download.Filename, download.Filesize, download.Generated)) + } + buf.WriteString("") + return buf.Bytes(), nil +} diff --git a/internal/dav/listing.go b/internal/dav/listing.go index e3de5f4..5c61742 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -24,6 +24,7 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) { } buf.WriteString(dav.Directory(directory, "")) } + buf.WriteString(dav.Directory(config.DOWNLOADS, "")) buf.WriteString("") return buf.Bytes(), nil } @@ -112,3 +113,18 @@ func HandleSingleFile(directory, torrentName, fileName string, torMgr *torrent.T buf.WriteString("") 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("") + buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, "")) + for _, download := range torMgr.DownloadCache.Items() { + buf.WriteString(dav.File(download.Filename, download.Filesize, download.Generated)) + } + buf.WriteString("") + return buf.Bytes(), nil +} diff --git a/internal/router/router.go b/internal/router/router.go index 4696f73..96571ef 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -57,6 +57,10 @@ func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torM router.GET("/dav/", zr.davRootHandler) router.GET("/dav/:directory/", zr.davTorrentsListHandler) router.GET("/dav/:directory/:torrent/", zr.davFilesListHandler) + // EXTRA: for browser handling of infuse + router.GET("/infuse/", zr.infuseDavRootHandler) + router.GET("/infuse/:directory/", zr.infuseDavTorrentsListHandler) + router.GET("/infuse/:directory/:torrent/", zr.infuseDavFilesListHandler) // DELETE routes router.DELETE("/dav/:directory/:torrent/", zr.deleteTorrentHandler) router.DELETE("/dav/:directory/:torrent/:file", zr.deleteFileHandler) @@ -129,11 +133,25 @@ func (zr *ZurgRouter) httpTorrentsListHandler(resp http.ResponseWriter, req *htt } func (zr *ZurgRouter) davTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - zr.handleTorrentsListRequest(resp, req, params, dav.ServeTorrentsList, "text/xml; charset=\"utf-8\"") + directory := params.ByName("directory") + handlerFunc := dav.ServeTorrentsList + if directory == config.DOWNLOADS { + handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { + return dav.ServeDownloadsList(torMgr) + } + } + zr.handleTorrentsListRequest(resp, req, params, handlerFunc, "text/xml; charset=\"utf-8\"") } func (zr *ZurgRouter) infuseDavTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - zr.handleTorrentsListRequest(resp, req, params, dav.ServeTorrentsListForInfuse, "text/xml; charset=\"utf-8\"") + directory := params.ByName("directory") + handlerFunc := dav.ServeTorrentsListForInfuse + if directory == config.DOWNLOADS { + handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { + return dav.ServeDownloadsListForInfuse(torMgr) + } + } + zr.handleTorrentsListRequest(resp, req, params, handlerFunc, "text/xml; charset=\"utf-8\"") } func (zr *ZurgRouter) handleFilesListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) { @@ -158,6 +176,7 @@ func (zr *ZurgRouter) davFilesListHandler(resp http.ResponseWriter, req *http.Re } func (zr *ZurgRouter) infuseDavFilesListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + fmt.Println(">>>>>>>>>>>>>>>>>>> infuseDavFilesListHandler", params.ByName("directory"), params.ByName("torrent")) zr.handleFilesListRequest(resp, req, params, dav.ServeFilesListForInfuse, "text/xml; charset=\"utf-8\"") } diff --git a/pkg/realdebrid/types.go b/pkg/realdebrid/types.go index 0dedd42..be13262 100644 --- a/pkg/realdebrid/types.go +++ b/pkg/realdebrid/types.go @@ -22,6 +22,22 @@ type Download struct { Host string `json:"host"` // Host main domain Download string `json:"download"` // Generated link Streamable int `json:"streamable"` + Generated string `json:"-"` // jsonDate +} + +func (d *Download) UnmarshalJSON(data []byte) error { + type Alias Download + aux := &struct { + Generated string `json:"generated"` + *Alias + }{ + Alias: (*Alias)(d), + } + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + d.Generated = strings.Replace(aux.Generated, "Z", "+01:00", 1) + return nil } type Torrent struct {