Couple of fixes

This commit is contained in:
Ben Sarmiento
2023-11-10 21:25:53 +01:00
parent b97f859a32
commit ab31114f2e
7 changed files with 131 additions and 152 deletions

View File

@@ -6,13 +6,12 @@ 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"
"github.com/debridmediamanager.com/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/hashicorp/golang-lru/v2/expirable"
"go.uber.org/zap"
@@ -73,6 +72,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
resp := t.UnrestrictUntilOk(link)
if resp == nil {
log.Errorf("The link cannot be unrestricted, file %s is no longer available", file.Path)
// TODO: maybe repair the torrent?
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
return
@@ -100,55 +100,36 @@ func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, t
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)
}
// copy range header if it exists
if r.Header.Get("Range") != "" {
req.Header.Add("Range", r.Header.Get("Range"))
}
// Create a custom HTTP client with a timeout.
client := &http.Client{
Timeout: time.Second * 30, // Full request timeout including dial, request and response.
}
client := zurghttp.NewHTTPClient(c.GetToken(), 10)
// 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)
resp, err := client.Do(req)
if err != nil {
log.Errorf("Error downloading file %v", err)
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, t, 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, t, 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, t *torrent.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) {

View File

@@ -1 +0,0 @@
package universal