Refactor file links and merges

This commit is contained in:
Ben Sarmiento
2024-01-27 17:38:26 +01:00
parent 1913498cbe
commit 1aabcfd322
5 changed files with 59 additions and 104 deletions

View File

@@ -49,17 +49,17 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
link := file.Link
unrestrict := torMgr.UnrestrictUntilOk(link)
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, link)
torrent.BrokenLinks.Add(file.Link)
// file.Link = "repair"
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, file.Link)
file.Link = ""
if cfg.EnableRepair() {
torMgr.TriggerRepair(torrent)
} else {
log.Debugf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", fileName, link)
log.Debugf("Repair is disabled, skipping repair for unavailable file %s (link=%s)", fileName, file.Link)
}
http.Error(resp, "File is not available", http.StatusNotFound)
return
@@ -100,6 +100,8 @@ func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWrit
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)
@@ -155,17 +157,16 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
}
if torrent != nil {
log.Debugf("Opening file %s from torrent %s (%s)%s", unrestrict.Download, torMgr.GetKey(torrent), unrestrict.Link, rangeLog)
log.Debugf("Downloading unrestricted link %s from torrent %s (%s)%s", unrestrict.Download, torMgr.GetKey(torrent), unrestrict.Link, rangeLog)
} else {
log.Debugf("Opening file %s (%s)%s", unrestrict.Download, unrestrict.Link, rangeLog)
log.Debugf("Downloading unrestricted link %s (%s)%s", unrestrict.Download, unrestrict.Link, rangeLog)
}
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 {
torrent.BrokenLinks.Add(file.Link)
// file.Link = "repair"
file.Link = ""
if cfg.EnableRepair() && torrent != nil {
torMgr.TriggerRepair(torrent)
} else {
@@ -181,8 +182,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
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)
torrent.BrokenLinks.Add(file.Link)
// file.Link = "repair"
file.Link = ""
if cfg.EnableRepair() && torrent != nil {
torMgr.TriggerRepair(torrent)
} else {
@@ -199,7 +199,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
}
}
log.Debugf("Started serving file %s%s", unrestrict.Filename, rangeLog)
log.Debugf("Serving file %s%s", unrestrict.Download, rangeLog)
buf := make([]byte, cfg.GetNetworkBufferSize())
io.CopyBuffer(resp, download.Body, buf)