From 8a76cb026795ca471bf24794106abf304649c552 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 8 Jan 2024 23:17:03 +0100 Subject: [PATCH] Refactor to downloader --- internal/app.go | 4 +-- internal/handlers/router.go | 26 ++++++++++---------- internal/universal/{get.go => downloader.go} | 18 +++++++------- internal/universal/head.go | 2 ++ 4 files changed, 26 insertions(+), 24 deletions(-) rename internal/universal/{get.go => downloader.go} (86%) diff --git a/internal/app.go b/internal/app.go index 88a6381..4271818 100644 --- a/internal/app.go +++ b/internal/app.go @@ -49,10 +49,10 @@ func MainApp(configPath string) { torrentMgr := torrent.NewTorrentManager(config, rd, p, log.Named("manager")) downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, config, log.Named("dlclient")) - getfile := universal.NewGetFile(downloadClient) + downloader := universal.NewDownloader(downloadClient) router := chi.NewRouter() - handlers.AttachHandlers(router, getfile, torrentMgr, config, rd, log.Named("router")) + handlers.AttachHandlers(router, downloader, torrentMgr, config, rd, log.Named("router")) // go func() { // if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed { diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 190382c..6089229 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -17,11 +17,11 @@ import ( ) type Handlers struct { - getfile *universal.GetFile - torMgr *torrent.TorrentManager - cfg config.ConfigInterface - api *realdebrid.RealDebrid - log *logutil.Logger + downloader *universal.Downloader + torMgr *torrent.TorrentManager + cfg config.ConfigInterface + api *realdebrid.RealDebrid + log *logutil.Logger } func init() { @@ -30,13 +30,13 @@ func init() { chi.RegisterMethod("MOVE") } -func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) { +func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) { hs := &Handlers{ - getfile: getfile, - torMgr: torMgr, - cfg: cfg, - api: api, - log: log, + downloader: downloader, + torMgr: torMgr, + cfg: cfg, + api: api, + log: log, } router.Use(optionsMiddleware) @@ -294,7 +294,7 @@ func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Reque directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") fileName := chi.URLParam(req, "filename") - hs.getfile.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log) + hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log) } func (hs *Handlers) handleCheckCachedLink(resp http.ResponseWriter, req *http.Request) { @@ -307,7 +307,7 @@ func (hs *Handlers) handleCheckCachedLink(resp http.ResponseWriter, req *http.Re func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) { filename := chi.URLParam(req, "filename") if download, ok := hs.torMgr.DownloadMap.Get(filename); ok { - hs.getfile.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log) + hs.downloader.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log) } else { http.NotFound(resp, req) } diff --git a/internal/universal/get.go b/internal/universal/downloader.go similarity index 86% rename from internal/universal/get.go rename to internal/universal/downloader.go index 1e746e4..a0ab862 100644 --- a/internal/universal/get.go +++ b/internal/universal/downloader.go @@ -13,16 +13,16 @@ import ( "github.com/debridmediamanager/zurg/pkg/realdebrid" ) -type GetFile struct { +type Downloader struct { client *zurghttp.HTTPClient } -func NewGetFile(client *zurghttp.HTTPClient) *GetFile { - return &GetFile{client: client} +func NewDownloader(client *zurghttp.HTTPClient) *Downloader { + return &Downloader{client: client} } // DownloadFile handles a GET request for files in torrents -func (gf *GetFile) DownloadFile(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { +func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { log.Warnf("Cannot find directory %s", directory) @@ -87,14 +87,14 @@ func (gf *GetFile) DownloadFile(directory, torrentName, fileName string, resp ht } redirect(resp, req, unrestrict.Download, cfg) } else { - gf.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log) + dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log) } return } } // DownloadLink handles a GET request for downloads -func (gf *GetFile) DownloadLink(fileName, link string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { +func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { if !strings.HasPrefix(link, "http") { // This is a dead file, serve an alternate file log.Warnf("File %s is not available", fileName) @@ -132,13 +132,13 @@ func (gf *GetFile) DownloadLink(fileName, link string, resp http.ResponseWriter, } redirect(resp, req, unrestrict.Download, cfg) } else { - gf.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log) + dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log) } return } } -func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.File, unrestrict *realdebrid.Download, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { +func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor.File, unrestrict *realdebrid.Download, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { // Create a new request for the file download. dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil) if err != nil { @@ -154,7 +154,7 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi dlReq.Header.Add("Range", req.Header.Get("Range")) } - download, err := gf.client.Do(dlReq) + download, err := dl.client.Do(dlReq) if err != nil { if file != nil && unrestrict.Streamable == 1 { log.Warnf("Cannot download file %s: %v", file.Path, err) diff --git a/internal/universal/head.go b/internal/universal/head.go index 2f32b94..370dffd 100644 --- a/internal/universal/head.go +++ b/internal/universal/head.go @@ -64,6 +64,8 @@ func getContentMimeType(filePath string) string { return "application/x-rar-compressed" case ".zip": return "application/zip" + case ".txt": + return "text/plain" default: return "application/octet-stream" }