Catch bw limit errors and prevent repair loops

This commit is contained in:
Ben Adrian Sarmiento
2024-06-24 00:38:57 +02:00
parent a0c13af94b
commit 449c0f71cf
5 changed files with 139 additions and 83 deletions

View File

@@ -61,35 +61,36 @@ func (dl *Downloader) DownloadFile(
return
}
unrestrict := torMgr.UnrestrictFile(file, cfg.ShouldServeFromRclone())
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, file.Link)
if err := file.State.Event(context.Background(), "break_file"); err != nil {
log.Errorf("File %s is stale: %v", fileName, err)
http.Error(resp, "File is stale, please try again", http.StatusLocked)
return
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", http.StatusLocked)
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)
}
torMgr.EnqueueForRepair(torrent)
http.Error(resp, "File is not available", http.StatusNotFound)
return
} else {
if unrestrict.Filesize != file.Bytes {
// this is possible if there's only 1 streamable file in the torrent
// and then suddenly it's a rar file
actualExt := strings.ToLower(filepath.Ext(unrestrict.Filename))
expectedExt := strings.ToLower(filepath.Ext(fileName))
if actualExt != expectedExt && unrestrict.Streamable != 1 {
log.Warnf("File was changed and is not streamable: %s and %s (link=%s)", fileName, unrestrict.Filename, unrestrict.Link)
} else {
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
}
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download)
}
if unrestrict.Filesize != file.Bytes {
// this is possible if there's only 1 streamable file in the torrent
// and then suddenly it's a rar file
actualExt := strings.ToLower(filepath.Ext(unrestrict.Filename))
expectedExt := strings.ToLower(filepath.Ext(fileName))
if actualExt != expectedExt && unrestrict.Streamable != 1 {
log.Warnf("File was changed and is not streamable: %s and %s (link=%s)", fileName, unrestrict.Filename, unrestrict.Link)
} else {
dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
return
}
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download)
} else {
dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
}
}
@@ -104,18 +105,21 @@ func (dl *Downloader) DownloadLink(
log *logutil.Logger,
) {
// log.Debugf("Opening file %s (%s)", fileName, link)
unrestrict := torMgr.UnrestrictLink(link, cfg.ShouldServeFromRclone())
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link)
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", http.StatusLocked)
return
}
if err != nil {
log.Errorf("Error unrestricting link %s: %v", link, err)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download)
} else {
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download)
} else {
dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
}
return
dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
}
}
@@ -149,12 +153,7 @@ func (dl *Downloader) streamFileToResponse(
downloadResp, err := dl.client.Do(dlReq)
if err != nil {
log.Warnf("Cannot download file %s: %v", unrestrict.Download, err)
if file != nil {
if err := file.State.Event(context.Background(), "break_file"); err != nil {
log.Errorf("File %s is stale: %v", file.Path, err)
http.Error(resp, "File is stale, please try again", http.StatusLocked)
return
}
if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available", http.StatusNotFound)
@@ -165,12 +164,7 @@ func (dl *Downloader) streamFileToResponse(
// Check if the download was not successful
if downloadResp.StatusCode != http.StatusOK && downloadResp.StatusCode != http.StatusPartialContent {
log.Warnf("Received a %s status code for file %s", downloadResp.Status, unrestrict.Filename)
if file != nil {
if err := file.State.Event(context.Background(), "break_file"); err != nil {
log.Errorf("File %s is stale: %v", file.Path, err)
http.Error(resp, "File is stale, please try again", http.StatusLocked)
return
}
if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available", http.StatusNotFound)