Use new router

This commit is contained in:
Ben Sarmiento
2023-11-30 22:46:29 +01:00
parent 5472f7d08d
commit 0ad879066e
13 changed files with 311 additions and 384 deletions

View File

@@ -5,13 +5,10 @@ import (
"io"
"net/http"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/dav"
intHttp "github.com/debridmediamanager/zurg/internal/http"
intTor "github.com/debridmediamanager/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
"go.uber.org/zap"
@@ -26,155 +23,131 @@ func NewGetFile(client *zurghttp.HTTPClient) *GetFile {
}
// HandleGetRequest handles a GET request universally for both WebDAV and HTTP
func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) {
requestPath := path.Clean(r.URL.Path)
isDav := true
if strings.Contains(requestPath, "/http") {
requestPath = strings.Replace(requestPath, "/http", "/", 1)
isDav = false
}
if requestPath == "/favicon.ico" {
func (gf *GetFile) HandleGetRequest(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
log.Warnf("Cannot find directory %s", directory)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
segments := strings.Split(requestPath, "/")
// If there are less than 3 segments, return an error or adjust as needed
if len(segments) <= 3 {
if isDav {
dav.HandlePropfindRequest(w, r, t, log)
} else {
intHttp.HandleDirectoryListing(w, r, t, log)
}
torrent, ok := torrents.Get(torrentName)
if !ok {
log.Warnf("Cannot find torrent %sfrom path %s", torrentName, req.URL.Path)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
baseDirectory := segments[len(segments)-3]
accessKey := segments[len(segments)-2]
filename := segments[len(segments)-1]
torrents, ok := t.DirectoryMap.Get(baseDirectory)
file, ok := torrent.SelectedFiles.Get(fileName)
if !ok {
log.Warnf("Cannot find directory %s", baseDirectory)
http.Error(w, "File not found", http.StatusNotFound)
return
}
torrent, ok := torrents.Get(accessKey)
if !ok {
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
http.Error(w, "File not found", http.StatusNotFound)
return
}
file, ok := torrent.SelectedFiles.Get(filename)
if !ok {
log.Warnf("Cannot find file from path %s", requestPath)
http.Error(w, "File not found", http.StatusNotFound)
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
if !strings.HasPrefix(file.Link, "http") {
// This is a dead file, serve an alternate file
log.Warnf("File %s is not available", filename)
http.Error(w, "File is not available", http.StatusNotFound)
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
link := file.Link
if download, exists := t.DownloadCache.Get(link); exists {
if c.ShouldServeFromRclone() && t.Api.CanFetchFirstByte(download.Download) {
redirect(w, r, download.Download, c)
if download, exists := torMgr.DownloadCache.Get(link); exists {
if cfg.ShouldServeFromRclone() && torMgr.Api.CanFetchFirstByte(download.Download) {
redirect(resp, req, download.Download, cfg)
return
} else {
err := gf.streamCachedLinkToResponse(download.Download, w, r, t, c, log)
err := gf.streamCachedLinkToResponse(download.Download, resp, req, torMgr, cfg, log)
if err == nil {
return
}
}
}
resp := t.UnrestrictUntilOk(link)
if resp == nil {
unrestrict := torMgr.UnrestrictUntilOk(link)
if unrestrict == nil {
// log.Warnf("File %s is no longer available, link %s", filepath.Base(file.Path), link)
file.Link = "repair"
if c.EnableRepair() {
if cfg.EnableRepair() {
// log.Debugf("File %s is marked for repair", filepath.Base(file.Path))
t.ScheduleForRefresh() // force a recheck
torMgr.ScheduleForRefresh() // force a recheck
}
http.Error(w, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusNotFound)
return
} else {
if resp.Filename != filename {
if unrestrict.Filename != fileName {
// this is possible if there's only 1 streamable file in the torrent
// and then suddenly it's a rar file
actualExt := filepath.Ext(resp.Filename)
expectedExt := filepath.Ext(filename)
if actualExt != expectedExt && resp.Streamable != 1 {
log.Warnf("File was changed and is not streamable: %s and %s", filename, resp.Filename)
http.Error(w, "File is not available", http.StatusNotFound)
actualExt := filepath.Ext(unrestrict.Filename)
expectedExt := filepath.Ext(fileName)
if actualExt != expectedExt && unrestrict.Streamable != 1 {
log.Warnf("File was changed and is not streamable: %s and %s", fileName, unrestrict.Filename)
http.Error(resp, "File is not available", http.StatusNotFound)
return
} else {
log.Warnf("Filename mismatch: %s and %s", filename, resp.Filename)
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
}
t.DownloadCache.Set(link, resp)
if c.ShouldServeFromRclone() {
redirect(w, r, resp.Download, c)
torMgr.DownloadCache.Set(link, unrestrict)
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download, cfg)
} else {
gf.streamFileToResponse(file, resp.Download, w, r, t, c, log)
gf.streamFileToResponse(file, unrestrict.Download, resp, req, torMgr, cfg, log)
}
return
}
}
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)
func (gf *GetFile) streamCachedLinkToResponse(url string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) 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 r.Header.Get("Range") != "" {
req.Header.Add("Range", r.Header.Get("Range"))
if req.Header.Get("Range") != "" {
dlReq.Header.Add("Range", req.Header.Get("Range"))
}
resp, err := gf.client.Do(req)
download, err := gf.client.Do(dlReq)
if err != nil {
return fmt.Errorf("file is not available")
}
defer resp.Body.Close()
defer download.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
if download.StatusCode != http.StatusOK && download.StatusCode != http.StatusPartialContent {
return fmt.Errorf("file is not available")
}
for k, vv := range resp.Header {
for k, vv := range download.Header {
for _, v := range vv {
w.Header().Add(k, v)
resp.Header().Add(k, v)
}
}
buf := make([]byte, cfg.GetNetworkBufferSize())
io.CopyBuffer(w, resp.Body, buf)
io.CopyBuffer(resp, download.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) {
func (gf *GetFile) streamFileToResponse(file *intTor.File, url string, resp http.ResponseWriter, req *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)
dlReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
if file != nil {
log.Errorf("Error creating new request for file %s: %v", file.Path, err)
}
http.Error(w, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
// copy range header if it exists
if r.Header.Get("Range") != "" {
req.Header.Add("Range", r.Header.Get("Range"))
if req.Header.Get("Range") != "" {
dlReq.Header.Add("Range", req.Header.Get("Range"))
}
resp, err := gf.client.Do(req)
download, err := gf.client.Do(dlReq)
if err != nil {
if file != nil {
log.Warnf("Cannot download file %s: %v", file.Path, err)
@@ -184,40 +157,40 @@ func (gf *GetFile) streamFileToResponse(file *intTor.File, url string, w http.Re
torMgr.ScheduleForRefresh() // force a recheck
}
}
http.Error(w, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
defer resp.Body.Close()
defer download.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
if download.StatusCode != http.StatusOK && download.StatusCode != http.StatusPartialContent {
if file != nil {
log.Warnf("Received a %s status code for file %s", resp.Status, file.Path)
log.Warnf("Received a %s status code for file %s", download.Status, file.Path)
file.Link = "repair"
if cfg.EnableRepair() {
// log.Debugf("File %s is marked for repair", filepath.Base(file.Path))
torMgr.ScheduleForRefresh() // force a recheck
}
}
http.Error(w, "File is not available", http.StatusNotFound)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
for k, vv := range resp.Header {
for k, vv := range download.Header {
for _, v := range vv {
w.Header().Add(k, v)
resp.Header().Add(k, v)
}
}
buf := make([]byte, cfg.GetNetworkBufferSize())
io.CopyBuffer(w, resp.Body, buf)
io.CopyBuffer(resp, download.Body, buf)
}
func redirect(w http.ResponseWriter, r *http.Request, url string, c config.ConfigInterface) {
prefHost := c.GetRandomPreferredHost()
func redirect(resp http.ResponseWriter, req *http.Request, url string, cfg config.ConfigInterface) {
prefHost := cfg.GetRandomPreferredHost()
if prefHost != "" {
url = replaceHostInURL(url, prefHost)
}
http.Redirect(w, r, url, http.StatusFound)
http.Redirect(resp, req, url, http.StatusFound)
}
func replaceHostInURL(inputURL string, newHost string) string {