Proxy movie request

This commit is contained in:
Ben Sarmiento
2023-10-28 06:14:13 +02:00
parent ce6729ade9
commit 96b85c8f79
2 changed files with 55 additions and 2 deletions

9
curl-format.txt Normal file
View File

@@ -0,0 +1,9 @@
time_namelookup: %{time_namelookup}s\n
time_connect: %{time_connect}s\n
time_appconnect: %{time_appconnect}s\n
time_pretransfer: %{time_pretransfer}s\n
time_redirect: %{time_redirect}s\n
time_starttransfer: %{time_starttransfer}s\n
----------\n
time_total: %{time_total}s\n

View File

@@ -1,23 +1,26 @@
package universal
import (
"io"
"log"
"net/http"
"path"
"path/filepath"
"strings"
"sync"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/dav"
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/netutils"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/hashicorp/golang-lru/v2/expirable"
)
// HandleGetRequest handles a GET request universally for both WebDAV and HTTP
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string], master *netutils.NetMaster) {
requestPath := path.Clean(r.URL.Path)
isDav := true
if strings.Contains(requestPath, "/http") {
@@ -87,6 +90,47 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
}
} else {
cache.Add(requestPath, resp.Download)
http.Redirect(w, r, resp.Download, http.StatusFound)
streamFileToResponse(resp.Download, w, master)
}
}
func streamFileToResponse(url string, w http.ResponseWriter, master *netutils.NetMaster) {
resp, err := http.Get(url) // HTTP/2 is used by default if the server supports it
if err != nil {
log.Println("Error downloading file:", err)
http.Error(w, "Error downloading file", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Received a non-OK status code: %d", resp.StatusCode)
http.Error(w, "Error downloading file", http.StatusInternalServerError)
return
}
// Parallelize header copying to reduce waiting time
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
}()
// If Content-Length is available from the original response, set it
if val, ok := resp.Header["Content-Length"]; ok {
w.Header().Set("Content-Length", val[0])
}
// Wait for the header copying to complete
// Stream the response with a buffer for better performance
bufferSize := 32 * 1024 // 16KB buffer
buf := make([]byte, bufferSize)
wg.Wait()
io.CopyBuffer(w, resp.Body, buf)
}