Update returned error codes

This commit is contained in:
Ben Adrian Sarmiento
2024-06-26 16:28:19 +02:00
parent d5e3665a53
commit f4910b8e87
5 changed files with 19 additions and 13 deletions

View File

@@ -198,6 +198,10 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
} }
efficiency := response.ServedMB * 100 / denominator efficiency := response.ServedMB * 100 / denominator
if zr.initialTraffic > response.TrafficLogged {
zr.initialTraffic = response.TrafficLogged
}
out += fmt.Sprintf(` out += fmt.Sprintf(`
<tr> <tr>
<td>Library Size</td> <td>Library Size</td>

View File

@@ -26,7 +26,7 @@ type Handlers struct {
api *realdebrid.RealDebrid api *realdebrid.RealDebrid
workerPool *ants.Pool workerPool *ants.Pool
hosts []string hosts []string
initialTraffic int64 initialTraffic uint64
log *logutil.Logger log *logutil.Logger
} }
@@ -54,7 +54,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
} }
hs.initialTraffic = 0 hs.initialTraffic = 0
if _, ok := trafficDetails["real-debrid.com"]; ok { if _, ok := trafficDetails["real-debrid.com"]; ok {
hs.initialTraffic = trafficDetails["real-debrid.com"] hs.initialTraffic = uint64(trafficDetails["real-debrid.com"])
} }
if cfg.GetUsername() != "" { if cfg.GetUsername() != "" {

View File

@@ -90,15 +90,15 @@ func (dl *Downloader) DownloadFile(
unrestrict, err := torMgr.UnrestrictFile(file, cfg.ShouldServeFromRclone()) unrestrict, err := torMgr.UnrestrictFile(file, cfg.ShouldServeFromRclone())
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" { 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") // 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 return
} }
if err != nil { if err != nil {
log.Errorf("Error unrestricting file %s: %v", file.Path, err)
if file.State.Event(context.Background(), "break_file") == nil { if file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent) 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 return
} }
@@ -134,12 +134,12 @@ func (dl *Downloader) DownloadLink(
unrestrict, err := torMgr.UnrestrictLink(link, cfg.ShouldServeFromRclone()) unrestrict, err := torMgr.UnrestrictLink(link, cfg.ShouldServeFromRclone())
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" { 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") // 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 return
} }
if err != nil { if err != nil {
log.Errorf("Error unrestricting link %s: %v", link, err) 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 return
} }
if cfg.ShouldServeFromRclone() { if cfg.ShouldServeFromRclone() {
@@ -165,7 +165,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", http.StatusNotFound) http.Error(resp, "File is not available (can't create request)", http.StatusBadRequest)
return return
} }
@@ -179,7 +179,7 @@ func (dl *Downloader) streamFileToResponse(
downloadResp, err := dl.client.Do(dlReq) downloadResp, err := dl.client.Do(dlReq)
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" { 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") // 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 return
} }
if err != nil { if err != nil {
@@ -187,7 +187,7 @@ func (dl *Downloader) streamFileToResponse(
if file != nil && file.State.Event(context.Background(), "break_file") == nil { if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent) 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 return
} }
defer downloadResp.Body.Close() defer downloadResp.Body.Close()
@@ -198,7 +198,7 @@ func (dl *Downloader) streamFileToResponse(
if file != nil && file.State.Event(context.Background(), "break_file") == nil { if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent) torMgr.EnqueueForRepair(torrent)
} }
http.Error(resp, "File is not available", http.StatusNotFound) http.Error(resp, "File is not available (download error)", http.StatusBadRequest)
return return
} }

View File

@@ -10,11 +10,13 @@ import (
"github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/config"
zurghttp "github.com/debridmediamanager/zurg/pkg/http" zurghttp "github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/logutil"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
) )
type RealDebrid struct { type RealDebrid struct {
torrentsCache []Torrent torrentsCache []Torrent
UnrestrictMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Download]]
apiClient *zurghttp.HTTPClient apiClient *zurghttp.HTTPClient
unrestrictClient *zurghttp.HTTPClient unrestrictClient *zurghttp.HTTPClient
downloadClient *zurghttp.HTTPClient downloadClient *zurghttp.HTTPClient
@@ -33,7 +35,7 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli
cfg: cfg, cfg: cfg,
log: log, log: log,
} }
rd.readCachedTorrents() rd.loadCachedTorrents()
return rd return rd
} }

View File

@@ -211,7 +211,7 @@ func (rd *RealDebrid) cacheTorrents(torrents []Torrent) {
rd.torrentsCache = torrents rd.torrentsCache = torrents
} }
func (rd *RealDebrid) readCachedTorrents() { func (rd *RealDebrid) loadCachedTorrents() {
filePath := "data/info/all.json" filePath := "data/info/all.json"
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {