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

@@ -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)

View File

@@ -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"
}