Return a 404 when file is unavailable (or being repaired)

This commit is contained in:
Ben Sarmiento
2024-03-03 17:11:01 +01:00
parent edb229f079
commit 0c937968b7

View File

@@ -49,7 +49,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
// log.Debugf("Opening file %s from torrent %s (%s)", fileName, torMgr.GetKey(torrent), file.Link) // log.Debugf("Opening file %s from torrent %s (%s)", fileName, torMgr.GetKey(torrent), file.Link)
if file.IsBroken { if file.IsBroken {
if cfg.EnableRepair() { if cfg.EnableRepair() {
http.Error(resp, "File is temporarily unavailable", http.StatusLocked) http.Error(resp, "File is temporarily unavailable", http.StatusNotFound)
} else { } else {
http.Error(resp, "File is not available", http.StatusNotFound) http.Error(resp, "File is not available", http.StatusNotFound)
} }
@@ -65,7 +65,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
} else { } else {
log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", fileName, file.Link) log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", fileName, file.Link)
} }
http.Error(resp, "File is not available", http.StatusInternalServerError) http.Error(resp, "File is not available", http.StatusNotFound)
return return
} else { } else {
if unrestrict.Filesize != file.Bytes { if unrestrict.Filesize != file.Bytes {
@@ -80,7 +80,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
} }
} }
if cfg.ShouldServeFromRclone() { if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download, cfg) redirect(resp, req, unrestrict.Download)
} else { } else {
dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log) dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
} }
@@ -98,7 +98,7 @@ func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWrit
return return
} else { } else {
if cfg.ShouldServeFromRclone() { if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download, cfg) redirect(resp, req, unrestrict.Download)
} else { } else {
dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log) dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
} }
@@ -143,7 +143,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
} else { } else {
log.Warnf("Cannot download file %s: %v", unrestrict.Download, err) log.Warnf("Cannot download file %s: %v", unrestrict.Download, err)
} }
http.Error(resp, "File is not available", http.StatusInternalServerError) http.Error(resp, "File is not available", http.StatusNotFound)
return return
} }
defer download.Body.Close() defer download.Body.Close()
@@ -160,7 +160,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
} else { } else {
log.Warnf("Received a %s status code for file %s", download.Status, unrestrict.Download) log.Warnf("Received a %s status code for file %s", download.Status, unrestrict.Download)
} }
http.Error(resp, "File is not available", http.StatusInternalServerError) http.Error(resp, "File is not available", http.StatusNotFound)
return return
} }
@@ -176,6 +176,6 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
io.CopyBuffer(resp, download.Body, buf) io.CopyBuffer(resp, download.Body, buf)
} }
func redirect(resp http.ResponseWriter, req *http.Request, url string, cfg config.ConfigInterface) { func redirect(resp http.ResponseWriter, req *http.Request, url string) {
http.Redirect(resp, req, url, http.StatusFound) http.Redirect(resp, req, url, http.StatusFound)
} }