stream file fix
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/internal/dav"
|
||||
@@ -40,7 +39,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
return
|
||||
}
|
||||
if data, exists := cache.Get(requestPath); exists {
|
||||
http.Redirect(w, r, data, http.StatusFound)
|
||||
streamFileToResponse(data, w, r, c.GetNetworkBufferSize())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -51,7 +50,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
|
||||
if torrents == nil {
|
||||
log.Println("Cannot find torrent", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -87,49 +86,45 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
} else {
|
||||
log.Println("Filename mismatch", resp.Filename, filenameV2)
|
||||
}
|
||||
} else {
|
||||
cache.Add(requestPath, resp.Download)
|
||||
streamFileToResponse(resp.Download, w)
|
||||
}
|
||||
cache.Add(requestPath, resp.Download)
|
||||
streamFileToResponse(resp.Download, w, r, c.GetNetworkBufferSize())
|
||||
}
|
||||
|
||||
func streamFileToResponse(url string, w http.ResponseWriter) {
|
||||
resp, err := http.Get(url) // HTTP/2 is used by default if the server supports it
|
||||
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bufferSize int) {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
log.Println("Error creating new request:", err)
|
||||
http.Redirect(w, r, "https://send.nukes.wtf/ARjVWb", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
for k, values := range r.Header {
|
||||
for _, v := range values {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("Error downloading file:", err)
|
||||
http.Error(w, "Error downloading file", http.StatusInternalServerError)
|
||||
http.Redirect(w, r, "https://send.nukes.wtf/TB2u2n", http.StatusFound)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
||||
log.Printf("Received a non-OK status code: %d", resp.StatusCode)
|
||||
http.Error(w, "Error downloading file", http.StatusInternalServerError)
|
||||
http.Redirect(w, r, "https://send.nukes.wtf/b5AiON", http.StatusFound)
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user