diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index 2cd5fe8..84755a7 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -24,7 +24,16 @@ func NewDownloader(client *zurghttp.HTTPClient) *Downloader { } // DownloadFile handles a GET request for files in torrents -func (dl *Downloader) 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) @@ -89,7 +98,15 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp } // DownloadLink handles a GET request for downloads -func (dl *Downloader) 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, +) { // log.Debugf("Opening file %s (%s)", fileName, link) unrestrict := torMgr.UnrestrictLinkUntilOk(link) if unrestrict == nil { @@ -106,7 +123,16 @@ func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWrit } } -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) { +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 { @@ -130,7 +156,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor // log.Debugf("Downloading unrestricted link %s (%s)%s", unrestrict.Download, unrestrict.Link, rangeLog) // } - download, err := dl.client.Do(dlReq) + downloadResp, err := dl.client.Do(dlReq) if err != nil { if file != nil && unrestrict.Streamable == 1 { file.IsBroken = true @@ -146,25 +172,25 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor http.Error(resp, "File is not available", http.StatusNotFound) return } - defer download.Body.Close() + defer downloadResp.Body.Close() - if download.StatusCode/100 != 2 { + if downloadResp.StatusCode/100 != 2 { if file != nil && unrestrict.Streamable == 1 { file.IsBroken = true if cfg.EnableRepair() && torrent != nil { - log.Warnf("Received a %s status code for file %s (repairing...)", download.Status, file.Path) + log.Warnf("Received a %s status code for file %s (repairing...)", downloadResp.Status, file.Path) torMgr.TriggerRepair(torrent) } else { log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link) } } else { - log.Warnf("Received a %s status code for file %s", download.Status, unrestrict.Download) + log.Warnf("Received a %s status code for file %s", downloadResp.Status, unrestrict.Download) } http.Error(resp, "File is not available", http.StatusNotFound) return } - for k, vv := range download.Header { + for k, vv := range downloadResp.Header { for _, v := range vv { resp.Header().Add(k, v) } @@ -172,8 +198,9 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor // log.Debugf("Serving file %s%s", unrestrict.Download, rangeLog) - buf := make([]byte, cfg.GetNetworkBufferSize()) - io.CopyBuffer(resp, download.Body, buf) + // buf := make([]byte, cfg.GetNetworkBufferSize()) + // io.CopyBuffer(resp, downloadResp.Body, buf) + io.Copy(resp, downloadResp.Body) } func redirect(resp http.ResponseWriter, req *http.Request, url string) {