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") filename = chi.URLParam(req, "filename")
} }
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok { 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 { } else {
http.NotFound(resp, req) http.NotFound(resp, req)
} }

View File

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

View File

@@ -298,20 +298,8 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err
default: default:
return false return false
} }
} else if downloadErr, ok := err.(*DownloadErrorResponse); ok { } else if _, ok := err.(*DownloadErrorResponse); ok {
switch downloadErr.Message { return false
case "invalid_download_code": // 404
if attempts >= r.maxRetries {
r.log.Debugf("Invalid download code, attempt #%d", attempts+1)
return false
}
secs := r.backoff(attempts, rateLimitSleep)
r.log.Debugf("Invalid download code, attempt #%d, retrying in %d seconds", attempts+1, secs/time.Second)
time.Sleep(secs)
return true
default:
return false
}
} }
// succesful requests // succesful requests