From 78be877efea68ec082f5c97c9ccdaa8d39681c5b Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Sun, 25 Aug 2024 13:48:01 +0200 Subject: [PATCH] Do not repair if download error, just retry from client side --- internal/handlers/router.go | 2 +- internal/universal/downloader.go | 34 +++++++------------------------- pkg/http/client.go | 16 ++------------- 3 files changed, 10 insertions(+), 42 deletions(-) diff --git a/internal/handlers/router.go b/internal/handlers/router.go index fa3c5ef..5cf9d24 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -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) } diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index c0f341a..d3c2c31 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -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) } diff --git a/pkg/http/client.go b/pkg/http/client.go index d5a87e3..c7671c0 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -298,20 +298,8 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err default: return false } - } else if downloadErr, ok := err.(*DownloadErrorResponse); ok { - switch downloadErr.Message { - 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 - } + } else if _, ok := err.(*DownloadErrorResponse); ok { + return false } // succesful requests