Fix location of pref hosts injection

This commit is contained in:
Ben Sarmiento
2024-01-07 16:17:31 +01:00
parent 120df54f35
commit 769c4a6803
3 changed files with 22 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package torrent
import (
"io"
"net/url"
"os"
"path/filepath"
"strings"
@@ -71,6 +72,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
}
for i := range downloads {
if !t.DownloadCache.Has(downloads[i].Link) {
if strings.Contains(downloads[i].Download, "download.real-debrid.") {
prefHost := t.Config.GetRandomPreferredHost()
if prefHost != "" {
downloads[i].Download = replaceHostInURL(downloads[i].Download, prefHost)
}
}
t.DownloadCache.Set(downloads[i].Link, &downloads[i])
}
}
@@ -114,6 +121,12 @@ func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
}
ret := t.Api.UnrestrictUntilOk(link, t.Config.ShouldServeFromRclone())
if ret != nil {
if strings.Contains(ret.Download, "download.real-debrid.") {
prefHost := t.Config.GetRandomPreferredHost()
if prefHost != "" {
ret.Download = replaceHostInURL(ret.Download, prefHost)
}
}
t.DownloadCache.Set(link, ret)
}
return ret
@@ -231,3 +244,12 @@ func (t *TorrentManager) deleteTorrentFile(torrentID string) {
t.log.Warnf("Cannot delete file %s: %v", filePath, err)
}
}
func replaceHostInURL(inputURL string, newHost string) string {
u, err := url.Parse(inputURL)
if err != nil {
return ""
}
u.Host = newHost
return u.String()
}

View File

@@ -3,7 +3,6 @@ package universal
import (
"io"
"net/http"
"net/url"
"path/filepath"
"strings"
@@ -151,18 +150,5 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi
}
func redirect(resp http.ResponseWriter, req *http.Request, url string, cfg config.ConfigInterface) {
prefHost := cfg.GetRandomPreferredHost()
if prefHost != "" {
url = replaceHostInURL(url, prefHost)
}
http.Redirect(resp, req, 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()
}