Refactor to downloader

This commit is contained in:
Ben Sarmiento
2024-01-08 23:17:03 +01:00
parent 5a23d0ff7b
commit 8a76cb0267
4 changed files with 26 additions and 24 deletions

View File

@@ -49,10 +49,10 @@ func MainApp(configPath string) {
torrentMgr := torrent.NewTorrentManager(config, rd, p, log.Named("manager")) torrentMgr := torrent.NewTorrentManager(config, rd, p, log.Named("manager"))
downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, config, log.Named("dlclient")) downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, config, log.Named("dlclient"))
getfile := universal.NewGetFile(downloadClient) downloader := universal.NewDownloader(downloadClient)
router := chi.NewRouter() 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() { // go func() {
// if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed { // if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed {

View File

@@ -17,11 +17,11 @@ import (
) )
type Handlers struct { type Handlers struct {
getfile *universal.GetFile downloader *universal.Downloader
torMgr *torrent.TorrentManager torMgr *torrent.TorrentManager
cfg config.ConfigInterface cfg config.ConfigInterface
api *realdebrid.RealDebrid api *realdebrid.RealDebrid
log *logutil.Logger log *logutil.Logger
} }
func init() { func init() {
@@ -30,13 +30,13 @@ func init() {
chi.RegisterMethod("MOVE") 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{ hs := &Handlers{
getfile: getfile, downloader: downloader,
torMgr: torMgr, torMgr: torMgr,
cfg: cfg, cfg: cfg,
api: api, api: api,
log: log, log: log,
} }
router.Use(optionsMiddleware) router.Use(optionsMiddleware)
@@ -294,7 +294,7 @@ func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Reque
directory := chi.URLParam(req, "directory") directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent") torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "filename") 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) { 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) { func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename := chi.URLParam(req, "filename") filename := chi.URLParam(req, "filename")
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok { 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 { } else {
http.NotFound(resp, req) http.NotFound(resp, req)
} }

View File

@@ -13,16 +13,16 @@ import (
"github.com/debridmediamanager/zurg/pkg/realdebrid" "github.com/debridmediamanager/zurg/pkg/realdebrid"
) )
type GetFile struct { type Downloader struct {
client *zurghttp.HTTPClient client *zurghttp.HTTPClient
} }
func NewGetFile(client *zurghttp.HTTPClient) *GetFile { func NewDownloader(client *zurghttp.HTTPClient) *Downloader {
return &GetFile{client: client} return &Downloader{client: client}
} }
// DownloadFile handles a GET request for files in torrents // 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) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
log.Warnf("Cannot find directory %s", directory) 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) redirect(resp, req, unrestrict.Download, cfg)
} else { } else {
gf.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log) dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
} }
return return
} }
} }
// DownloadLink handles a GET request for downloads // 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") { if !strings.HasPrefix(link, "http") {
// This is a dead file, serve an alternate file // This is a dead file, serve an alternate file
log.Warnf("File %s is not available", fileName) 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) redirect(resp, req, unrestrict.Download, cfg)
} else { } else {
gf.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log) dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
} }
return 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. // Create a new request for the file download.
dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil) dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil)
if err != 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")) dlReq.Header.Add("Range", req.Header.Get("Range"))
} }
download, err := gf.client.Do(dlReq) download, err := dl.client.Do(dlReq)
if err != nil { if err != nil {
if file != nil && unrestrict.Streamable == 1 { if file != nil && unrestrict.Streamable == 1 {
log.Warnf("Cannot download file %s: %v", file.Path, err) log.Warnf("Cannot download file %s: %v", file.Path, err)

View File

@@ -64,6 +64,8 @@ func getContentMimeType(filePath string) string {
return "application/x-rar-compressed" return "application/x-rar-compressed"
case ".zip": case ".zip":
return "application/zip" return "application/zip"
case ".txt":
return "text/plain"
default: default:
return "application/octet-stream" return "application/octet-stream"
} }