Handle preferred hosts in redirects too

This commit is contained in:
Ben Sarmiento
2023-11-24 22:03:31 +01:00
parent 595040ad7e
commit cbd291400f
2 changed files with 23 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ package universal
import (
"io"
"net/http"
"net/url"
"path"
"path/filepath"
"strings"
@@ -75,7 +76,7 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
if url, exists := cache.Get(requestPath); exists {
if c.ShouldServeFromRclone() {
http.Redirect(w, r, url, http.StatusFound)
redirect(w, r, url, c)
} else {
gf.streamFileToResponse(file, url, w, r, t, c, log)
}
@@ -112,7 +113,7 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
}
cache.Add(requestPath, resp.Download)
if c.ShouldServeFromRclone() {
http.Redirect(w, r, resp.Download, http.StatusFound)
redirect(w, r, resp.Download, c)
} else {
gf.streamFileToResponse(file, resp.Download, w, r, t, c, log)
}
@@ -174,8 +175,24 @@ func (gf *GetFile) playErrorVideo(link string, w http.ResponseWriter, r *http.Re
return
}
if c.ShouldServeFromRclone() {
http.Redirect(w, r, resp.Download, http.StatusFound)
return
redirect(w, r, resp.Download, c)
}
gf.streamFileToResponse(nil, resp.Download, w, r, t, c, log)
}
func redirect(w http.ResponseWriter, r *http.Request, url string, c config.ConfigInterface) {
prefHost := c.GetRandomPreferredHost()
if prefHost != "" {
url = replaceHostInURL(url, prefHost)
}
http.Redirect(w, r, url, http.StatusFound)
}
func replaceHostInURL(inputURL string, newHost string) string {
u, err := url.Parse(inputURL)
if err != nil {
return ""
}
u.Host = newHost
return u.String()
}