Prepare materials for auto heal functionality

This commit is contained in:
Ben Sarmiento
2023-10-23 16:13:04 +02:00
parent 693f8dde8a
commit 6298830ea6
5 changed files with 312 additions and 123 deletions

View File

@@ -2,6 +2,7 @@ package realdebrid
import (
"math"
"net/http"
"strings"
"time"
)
@@ -18,3 +19,36 @@ func RetryUntilOk[T any](fn func() (T, error)) T {
time.Sleep(delay)
}
}
func canFetchFirstByte(url string) bool {
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false
}
// Set the Range header to request only the first byte
req.Header.Set("Range", "bytes=0-0")
// Execute the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
// If server supports partial content
if resp.StatusCode == http.StatusPartialContent {
buffer := make([]byte, 1)
_, err := resp.Body.Read(buffer)
return err == nil
}
if resp.StatusCode != http.StatusOK {
return false
}
// If server doesn't support partial content, try reading the first byte and immediately close
buffer := make([]byte, 1)
_, err = resp.Body.Read(buffer)
resp.Body.Close() // Close immediately after reading
return err == nil
}