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
if zr.initialTraffic > response.TrafficLogged {
zr.initialTraffic = response.TrafficLogged
}
out += fmt.Sprintf(`
<tr>
<td>Library Size</td>

View File

@@ -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() != "" {

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {