Add http client for streaming
This commit is contained in:
6
go.mod
6
go.mod
@@ -8,4 +8,8 @@ require (
|
|||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require go.uber.org/multierr v1.10.0 // indirect
|
require (
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||||
|
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
|
||||||
|
go.uber.org/multierr v1.10.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,3 +1,7 @@
|
|||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||||
|
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
|
||||||
|
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/cenkalti/backoff"
|
||||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||||
"github.com/debridmediamanager.com/zurg/internal/dav"
|
"github.com/debridmediamanager.com/zurg/internal/dav"
|
||||||
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
|
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) {
|
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)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != 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)
|
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, c, log)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copy the headers from the incoming request to the new request.
|
||||||
for k, values := range r.Header {
|
for k, values := range r.Header {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
req.Header.Add(k, v)
|
req.Header.Add(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
// Create a custom HTTP client with a timeout.
|
||||||
if err != nil {
|
client := &http.Client{
|
||||||
log.Errorf("Error downloading file %v", err)
|
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)
|
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, c, log)
|
||||||
return
|
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) {
|
func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) {
|
||||||
|
|||||||
Reference in New Issue
Block a user