Repair rework for rar files

This commit is contained in:
Ben Sarmiento
2024-01-07 14:24:21 +01:00
parent e5f0f4d7bd
commit 72f7b0f1e5
5 changed files with 97 additions and 73 deletions

View File

@@ -1,7 +1,6 @@
package universal
import (
"fmt"
"io"
"net/http"
"net/url"
@@ -53,25 +52,6 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.
}
link := file.Link
if download, exists := torMgr.DownloadCache.Get(link); exists {
if cfg.ShouldServeFromRclone() {
if cfg.ShouldVerifyDownloadLink() {
if torMgr.Api.CanFetchFirstByte(download.Download) {
redirect(resp, req, download.Download, cfg)
return
}
} else {
redirect(resp, req, download.Download, cfg)
return
}
} else {
err := gf.streamCachedLinkToResponse(download.Download, resp, req, torMgr, cfg, log)
if err == nil {
return
}
}
}
log.Debugf("Opening file %s from torrent %s (%s)", fileName, torrentName, link)
unrestrict := torMgr.UnrestrictUntilOk(link)
if unrestrict == nil || unrestrict.Link == "" {
@@ -98,7 +78,6 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
}
torMgr.DownloadCache.Set(link, unrestrict)
if cfg.ShouldServeFromRclone() {
if cfg.ShouldVerifyDownloadLink() {
if !torMgr.Api.CanFetchFirstByte(unrestrict.Download) {
@@ -115,39 +94,6 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.
}
}
func (gf *GetFile) streamCachedLinkToResponse(url string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) error {
// Create a new dlReq for the file download.
dlReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("file is not available")
}
// copy range header if it exists
if req.Header.Get("Range") != "" {
dlReq.Header.Add("Range", req.Header.Get("Range"))
}
download, err := gf.client.Do(dlReq)
if err != nil {
return fmt.Errorf("file is not available")
}
defer download.Body.Close()
if download.StatusCode != http.StatusOK && download.StatusCode != http.StatusPartialContent {
return fmt.Errorf("file is not available")
}
for k, vv := range download.Header {
for _, v := range vv {
resp.Header().Add(k, v)
}
}
buf := make([]byte, cfg.GetNetworkBufferSize())
io.CopyBuffer(resp, download.Body, buf)
return nil
}
func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.File, unrestrict *realdebrid.Download, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) {
// Create a new request for the file download.
dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil)