Repair adjustments 2

This commit is contained in:
Ben Sarmiento
2024-05-01 14:06:54 +02:00
parent 130cc0d7b3
commit b6b59b22e6
6 changed files with 71 additions and 85 deletions

View File

@@ -29,7 +29,7 @@ func CheckFile(directory, torrentName, fileName string, w http.ResponseWriter, r
file, ok := torrent.SelectedFiles.Get(fileName)
if !ok || file.IsDeleted {
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
http.Error(w, "Cannot find file", http.StatusNotFound)
http.Error(w, "File not found", http.StatusNotFound)
return
}
contentType := getContentMimeType(fileName)

View File

@@ -55,13 +55,8 @@ func (dl *Downloader) DownloadFile(
return
}
// log.Debugf("Opening file %s from torrent %s (%s)", fileName, torMgr.GetKey(torrent), file.Link)
if file.IsBroken {
if cfg.EnableRepair() {
http.Error(resp, "File is temporarily unavailable", http.StatusNotFound)
} else {
http.Error(resp, "File is not available", http.StatusNotFound)
}
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
@@ -85,7 +80,7 @@ func (dl *Downloader) DownloadFile(
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("File mismatch: %s and %s", fileName, unrestrict.Filename)
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
}
if cfg.ShouldServeFromRclone() {
@@ -133,29 +128,23 @@ func (dl *Downloader) streamFileToResponse(
cfg config.ConfigInterface,
log *logutil.Logger,
) {
// Create a new request for the file download.
// Create a new request for the file download
dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil)
if err != nil {
if file != nil {
log.Errorf("Error creating new request for file %s: %v", file.Path, err)
}
http.Error(resp, "File is not available", http.StatusInternalServerError)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
// copy range header if it exists
// rangeLog := ""
// Add the range header if it exists
if req.Header.Get("Range") != "" {
dlReq.Header.Add("Range", req.Header.Get("Range"))
// rangeLog = " (range: " + req.Header.Get("Range") + ")"
log.Debugf("Range request for file %s: %s", unrestrict.Download, req.Header.Get("Range"))
}
// if torrent != nil {
// log.Debugf("Downloading unrestricted link %s from torrent %s (%s)%s", unrestrict.Download, torMgr.GetKey(torrent), unrestrict.Link, rangeLog)
// } else {
// log.Debugf("Downloading unrestricted link %s (%s)%s", unrestrict.Download, unrestrict.Link, rangeLog)
// }
// Perform the request
downloadResp, err := dl.client.Do(dlReq)
if err != nil {
if file != nil && unrestrict.Streamable == 1 {
@@ -174,6 +163,7 @@ func (dl *Downloader) streamFileToResponse(
}
defer downloadResp.Body.Close()
// Check if the download was not successful
if downloadResp.StatusCode/100 != 2 {
if file != nil && unrestrict.Streamable == 1 {
file.IsBroken = true
@@ -190,14 +180,13 @@ func (dl *Downloader) streamFileToResponse(
return
}
// Copy the headers from the download response to the response
for k, vv := range downloadResp.Header {
for _, v := range vv {
resp.Header().Add(k, v)
}
}
// log.Debugf("Serving file %s%s", unrestrict.Download, rangeLog)
buf := make([]byte, cfg.GetNetworkBufferSize())
io.CopyBuffer(resp, downloadResp.Body, buf)
}