Do not repair if download error, just retry from client side

This commit is contained in:
Ben Adrian Sarmiento
2024-08-25 13:48:01 +02:00
parent 54230c9eaa
commit 78be877efe
3 changed files with 10 additions and 42 deletions

View File

@@ -396,7 +396,7 @@ func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Reque
filename = chi.URLParam(req, "filename")
}
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
hs.downloader.DownloadLink(download, resp, req, hs.torMgr, hs.cfg, hs.log)
hs.downloader.DownloadLink(download, resp, req, hs.cfg, hs.log)
} else {
http.NotFound(resp, req)
}

View File

@@ -116,10 +116,9 @@ func (dl *Downloader) DownloadFile(
unrestrict, err := torMgr.UnrestrictFile(file)
if utils.AreAllTokensExpired(err) {
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
http.Error(resp, "File is not available (bandwidth limit reached, all tokens are expired)", http.StatusBadRequest)
return
}
if err != nil {
} else if err != nil {
if file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
@@ -144,7 +143,7 @@ func (dl *Downloader) DownloadFile(
redirect(resp, req, unrestrict.Download)
} else {
// log.Debugf("Streaming %s", unrestrict.Download)
dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
dl.streamFileToResponse(file, unrestrict, resp, req, cfg, log)
}
}
@@ -153,7 +152,6 @@ func (dl *Downloader) DownloadLink(
unrestrict *realdebrid.Download,
resp http.ResponseWriter,
req *http.Request,
torMgr *intTor.TorrentManager,
cfg config.ConfigInterface,
log *logutil.Logger,
) {
@@ -162,17 +160,15 @@ func (dl *Downloader) DownloadLink(
redirect(resp, req, unrestrict.Download)
} else {
log.Debugf("Streaming %s", unrestrict.Download)
dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
dl.streamFileToResponse(nil, unrestrict, resp, req, cfg, log)
}
}
func (dl *Downloader) streamFileToResponse(
torrent *intTor.Torrent, // can be nil if downloading a link
file *intTor.File, // can be nil if downloading a link
unrestrict *realdebrid.Download,
resp http.ResponseWriter,
req *http.Request,
torMgr *intTor.TorrentManager,
cfg config.ConfigInterface,
log *logutil.Logger,
) {
@@ -182,7 +178,7 @@ func (dl *Downloader) streamFileToResponse(
if file != nil {
log.Errorf("Error creating new request for file %s: %v", file.Path, err)
}
http.Error(resp, "File is not available (can't create request)", http.StatusBadRequest)
http.Error(resp, "File is not available (can't create request)", http.StatusInternalServerError)
return
}
@@ -200,31 +196,15 @@ func (dl *Downloader) streamFileToResponse(
if utils.IsBytesLimitReached(err) {
dl.rd.TokenManager.SetTokenAsExpired(unrestrict.Token, "bandwidth limit exceeded")
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
return
} else if utils.IsInvalidDownloadCode(err) {
http.Error(resp, "File is not available (invalid download code)", http.StatusInternalServerError)
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusInternalServerError)
return
} else if err != nil {
log.Errorf("Cannot download file %s: %v", unrestrict.Download, err)
if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available (can't download)", http.StatusBadRequest)
http.Error(resp, "File is not available (can't download)", http.StatusInternalServerError)
return
}
defer downloadResp.Body.Close()
// Check if the download was not successful
if downloadResp.StatusCode != http.StatusOK && downloadResp.StatusCode != http.StatusPartialContent {
log.Errorf("Received a %s status code for file %s", downloadResp.Status, unrestrict.Filename)
if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available (download error)", http.StatusBadRequest)
return
}
if cr := downloadResp.Header.Get("Content-Range"); cr != "" {
resp.Header().Set("Content-Range", cr)
}