Use copy instead of copybuffer
This commit is contained in:
@@ -24,7 +24,16 @@ func NewDownloader(client *zurghttp.HTTPClient) *Downloader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DownloadFile handles a GET request for files in torrents
|
// 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)
|
torrents, ok := torMgr.DirectoryMap.Get(directory)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Warnf("Cannot find directory %s", directory)
|
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
|
// 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)
|
// log.Debugf("Opening file %s (%s)", fileName, link)
|
||||||
unrestrict := torMgr.UnrestrictLinkUntilOk(link)
|
unrestrict := torMgr.UnrestrictLinkUntilOk(link)
|
||||||
if unrestrict == nil {
|
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.
|
// 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 {
|
||||||
@@ -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)
|
// 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 err != nil {
|
||||||
if file != nil && unrestrict.Streamable == 1 {
|
if file != nil && unrestrict.Streamable == 1 {
|
||||||
file.IsBroken = true
|
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)
|
http.Error(resp, "File is not available", http.StatusNotFound)
|
||||||
return
|
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 {
|
if file != nil && unrestrict.Streamable == 1 {
|
||||||
file.IsBroken = true
|
file.IsBroken = true
|
||||||
if cfg.EnableRepair() && torrent != nil {
|
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)
|
torMgr.TriggerRepair(torrent)
|
||||||
} else {
|
} else {
|
||||||
log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link)
|
log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link)
|
||||||
}
|
}
|
||||||
} else {
|
} 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)
|
http.Error(resp, "File is not available", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, vv := range download.Header {
|
for k, vv := range downloadResp.Header {
|
||||||
for _, v := range vv {
|
for _, v := range vv {
|
||||||
resp.Header().Add(k, v)
|
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)
|
// log.Debugf("Serving file %s%s", unrestrict.Download, rangeLog)
|
||||||
|
|
||||||
buf := make([]byte, cfg.GetNetworkBufferSize())
|
// buf := make([]byte, cfg.GetNetworkBufferSize())
|
||||||
io.CopyBuffer(resp, download.Body, buf)
|
// io.CopyBuffer(resp, downloadResp.Body, buf)
|
||||||
|
io.Copy(resp, downloadResp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirect(resp http.ResponseWriter, req *http.Request, url string) {
|
func redirect(resp http.ResponseWriter, req *http.Request, url string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user