Add http client for streaming

This commit is contained in:
Ben Sarmiento
2023-11-08 07:07:55 +01:00
parent 56e3540583
commit 17cd7ae1f4
3 changed files with 53 additions and 21 deletions

View File

@@ -6,7 +6,9 @@ import (
"path"
"path/filepath"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/dav"
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
@@ -98,41 +100,63 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
}
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) {
// Create a new request for the file download.
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Errorf("Error creating new request %v", err)
log.Errorf("Error creating new request: %v", err)
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, c, log)
return
}
// Copy the headers from the incoming request to the new request.
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.Errorf("Error downloading file %v", err)
// Create a custom HTTP client with a timeout.
client := &http.Client{
Timeout: time.Second * 30, // Full request timeout including dial, request and response.
}
// Define a retry policy with exponential backoff.
retryPolicy := backoff.NewExponentialBackOff()
retryPolicy.MaxElapsedTime = time.Minute * 2 // Set the maximum elapsed time for retries.
// Use an operation with retries.
operation := func() error {
resp, err := client.Do(req)
if err != nil {
log.Errorf("Error downloading file: %v", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
log.Errorf("Received a 5XX status code: %d", resp.StatusCode)
return backoff.Permanent(err) // Stop retrying on bad status code.
}
// Copy the headers from the response to the ResponseWriter.
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
// Stream the content to the ResponseWriter.
buf := make([]byte, c.GetNetworkBufferSize())
_, err = io.CopyBuffer(w, resp.Body, buf)
return err
}
// Perform the operation with the retry policy.
if err := backoff.Retry(operation, retryPolicy); err != nil {
log.Errorf("Failed after retries: %v", err)
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, c, log)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
log.Errorf("Received a nonOK status code %d", resp.StatusCode)
streamErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, c, log)
return
}
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
buf := make([]byte, c.GetNetworkBufferSize())
io.CopyBuffer(w, resp.Body, buf)
}
func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) {