Reimplement deletes and marking files as broken

This commit is contained in:
Ben Sarmiento
2024-01-29 22:28:27 +01:00
parent 1615c9e121
commit b505400f60
12 changed files with 64 additions and 112 deletions

View File

@@ -31,6 +31,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
http.Error(resp, "File not found", http.StatusNotFound)
return
}
torrent, ok := torrents.Get(torrentName)
if !ok {
log.Warnf("Cannot find torrent %sfrom path %s", torrentName, req.URL.Path)
@@ -39,31 +40,28 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
}
file, ok := torrent.SelectedFiles.Get(fileName)
if !ok {
if !ok || file.IsDeleted {
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
if !strings.HasPrefix(file.Link, "http") {
// This is a dead file, serve an alternate file
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
// log.Debugf("Opening file %s from torrent %s (%s)", fileName, torMgr.GetKey(torrent), file.Link)
if file.IsBroken {
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}
// log.Debugf("Opening file %s from torrent %s (%s)", fileName, torMgr.GetKey(torrent), file.Link)
unrestrict := torMgr.UnrestrictUntilOk(file.Link)
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, file.Link)
file.Link = ""
file.IsBroken = true
if cfg.EnableRepair() {
log.Warnf("File %s cannot be unrestricted (link=%s) (repairing...)", fileName, file.Link)
torMgr.TriggerRepair(torrent)
} else {
log.Debugf("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.StatusNotFound)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
} else {
if unrestrict.Filesize != file.Bytes {
@@ -81,7 +79,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
if cfg.ShouldVerifyDownloadLink() {
if !dl.client.CanFetchFirstByte(unrestrict.Download) {
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}
}
@@ -95,19 +93,11 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
// DownloadLink handles a GET request for downloads
func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) {
if !strings.HasPrefix(link, "http") {
// This is a dead file, serve an alternate file
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
// log.Debugf("Opening file %s (%s)", fileName, link)
unrestrict := torMgr.UnrestrictUntilOk(link)
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link)
http.Error(resp, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
} else {
lFilename := strings.ToLower(fileName)
@@ -127,7 +117,7 @@ func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWrit
if cfg.ShouldVerifyDownloadLink() {
if !dl.client.CanFetchFirstByte(unrestrict.Download) {
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}
}
@@ -146,7 +136,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
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", http.StatusInternalServerError)
return
}
@@ -165,31 +155,35 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
download, err := dl.client.Do(dlReq)
if err != nil {
log.Warnf("Cannot download file %s: %v", unrestrict.Download, err)
if file != nil && unrestrict.Streamable == 1 {
file.Link = ""
file.IsBroken = true
if cfg.EnableRepair() && torrent != nil {
log.Warnf("Cannot download file %s: %v (repairing...)", unrestrict.Download, err)
torMgr.TriggerRepair(torrent)
} else {
log.Debugf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link)
log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link)
}
} else {
log.Warnf("Cannot download file %s: %v", unrestrict.Download, err)
}
http.Error(resp, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}
defer download.Body.Close()
if download.StatusCode/100 != 2 {
if file != nil && unrestrict.Streamable == 1 {
log.Warnf("Received a %s status code for file %s", download.Status, file.Path)
file.Link = ""
file.IsBroken = true
if cfg.EnableRepair() && torrent != nil {
log.Warnf("Received a %s status code for file %s (repairing...)", download.Status, file.Path)
torMgr.TriggerRepair(torrent)
} else {
log.Debugf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link)
log.Warnf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", file.Path, file.Link)
}
} else {
log.Warnf("Received a %s status code for file %s", download.Status, unrestrict.Download)
}
http.Error(resp, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}