diff --git a/internal/handlers/home.go b/internal/handlers/home.go index bf6035e..2d0ac93 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -198,6 +198,10 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { } efficiency := response.ServedMB * 100 / denominator + if zr.initialTraffic > response.TrafficLogged { + zr.initialTraffic = response.TrafficLogged + } + out += fmt.Sprintf(` Library Size diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 8dd5c5e..7bcc79a 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -26,7 +26,7 @@ type Handlers struct { api *realdebrid.RealDebrid workerPool *ants.Pool hosts []string - initialTraffic int64 + initialTraffic uint64 log *logutil.Logger } @@ -54,7 +54,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t } hs.initialTraffic = 0 if _, ok := trafficDetails["real-debrid.com"]; ok { - hs.initialTraffic = trafficDetails["real-debrid.com"] + hs.initialTraffic = uint64(trafficDetails["real-debrid.com"]) } if cfg.GetUsername() != "" { diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index aea9bc6..ea82e60 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -90,15 +90,15 @@ func (dl *Downloader) DownloadFile( unrestrict, err := torMgr.UnrestrictFile(file, cfg.ShouldServeFromRclone()) if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" { // log.Warnf("Your account has reached the bandwidth limit, please try again after 12AM CET") - http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusLocked) + http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest) return } if err != nil { - log.Errorf("Error unrestricting file %s: %v", file.Path, err) if file.State.Event(context.Background(), "break_file") == nil { torMgr.EnqueueForRepair(torrent) } - http.Error(resp, "File is not available", http.StatusNotFound) + log.Errorf("Error unrestricting file %s: %v", file.Path, err) + http.Error(resp, "File is not available (can't unrestrict)", http.StatusBadRequest) return } @@ -134,12 +134,12 @@ func (dl *Downloader) DownloadLink( unrestrict, err := torMgr.UnrestrictLink(link, cfg.ShouldServeFromRclone()) if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" { // log.Warnf("Your account has reached the bandwidth limit, please try again after 12AM CET") - http.Error(resp, "Link is not available (bandwidth limit reached)", http.StatusLocked) + http.Error(resp, "Link is not available (bandwidth limit reached)", http.StatusBadRequest) return } if err != nil { log.Errorf("Error unrestricting link %s: %v", link, err) - http.Error(resp, "File is not available", http.StatusInternalServerError) + http.Error(resp, "File is not available (can't unrestrict)", http.StatusInternalServerError) return } if cfg.ShouldServeFromRclone() { @@ -165,7 +165,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", http.StatusNotFound) + http.Error(resp, "File is not available (can't create request)", http.StatusBadRequest) return } @@ -179,7 +179,7 @@ func (dl *Downloader) streamFileToResponse( downloadResp, err := dl.client.Do(dlReq) if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" { // log.Warnf("Your account has reached the bandwidth limit, please try again after 12AM CET") - http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusLocked) + http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest) return } if err != nil { @@ -187,7 +187,7 @@ func (dl *Downloader) streamFileToResponse( if file != nil && file.State.Event(context.Background(), "break_file") == nil { torMgr.EnqueueForRepair(torrent) } - http.Error(resp, "File is not available", http.StatusNotFound) + http.Error(resp, "File is not available (can't download)", http.StatusBadRequest) return } defer downloadResp.Body.Close() @@ -198,7 +198,7 @@ func (dl *Downloader) streamFileToResponse( if file != nil && file.State.Event(context.Background(), "break_file") == nil { torMgr.EnqueueForRepair(torrent) } - http.Error(resp, "File is not available", http.StatusNotFound) + http.Error(resp, "File is not available (download error)", http.StatusBadRequest) return } diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index aa35585..156200b 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -10,11 +10,13 @@ import ( "github.com/debridmediamanager/zurg/internal/config" zurghttp "github.com/debridmediamanager/zurg/pkg/http" "github.com/debridmediamanager/zurg/pkg/logutil" + cmap "github.com/orcaman/concurrent-map/v2" "github.com/panjf2000/ants/v2" ) type RealDebrid struct { torrentsCache []Torrent + UnrestrictMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Download]] apiClient *zurghttp.HTTPClient unrestrictClient *zurghttp.HTTPClient downloadClient *zurghttp.HTTPClient @@ -33,7 +35,7 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli cfg: cfg, log: log, } - rd.readCachedTorrents() + rd.loadCachedTorrents() return rd } diff --git a/pkg/realdebrid/torrents.go b/pkg/realdebrid/torrents.go index 619a338..c992190 100644 --- a/pkg/realdebrid/torrents.go +++ b/pkg/realdebrid/torrents.go @@ -211,7 +211,7 @@ func (rd *RealDebrid) cacheTorrents(torrents []Torrent) { rd.torrentsCache = torrents } -func (rd *RealDebrid) readCachedTorrents() { +func (rd *RealDebrid) loadCachedTorrents() { filePath := "data/info/all.json" file, err := os.Open(filePath) if err != nil {