Add configs

This commit is contained in:
Ben Sarmiento
2023-11-28 01:06:11 +01:00
parent 3d380e468f
commit a7fd68b3fd
8 changed files with 89 additions and 40 deletions

View File

@@ -1,6 +1,7 @@
package universal
import (
"fmt"
"io"
"net/http"
"net/url"
@@ -79,12 +80,14 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
link := file.Link
if download, exists := t.DownloadCache.Get(link); exists {
if c.ShouldServeFromRclone() {
if c.ShouldServeFromRclone() && t.Api.CanFetchFirstByte(download.Download) {
redirect(w, r, download.Download, c)
} else {
gf.streamFileToResponse(file, download.Download, w, r, t, c, log)
err := gf.streamCachedLinkToResponse(download.Download, w, r, t, c, log)
if err == nil {
return
}
}
return
}
resp := t.UnrestrictUntilOk(link)
@@ -121,6 +124,39 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
}
}
func (gf *GetFile) streamCachedLinkToResponse(url string, w http.ResponseWriter, r *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) error {
// Create a new request for the file download.
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("file is not available")
}
// copy range header if it exists
if r.Header.Get("Range") != "" {
req.Header.Add("Range", r.Header.Get("Range"))
}
resp, err := gf.client.Do(req)
if err != nil {
return fmt.Errorf("file is not available")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
return fmt.Errorf("file is not available")
}
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
buf := make([]byte, cfg.GetNetworkBufferSize())
io.CopyBuffer(w, resp.Body, buf)
return nil
}
func (gf *GetFile) streamFileToResponse(file *intTor.File, url string, w http.ResponseWriter, r *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) {
// Create a new request for the file download.
req, err := http.NewRequest(http.MethodGet, url, nil)