Use browser headers for image requests

This commit is contained in:
video-prize-ranch
2023-05-26 15:45:59 -04:00
parent 5e7cf6ec52
commit 9525106ccc
3 changed files with 24 additions and 27 deletions

View File

@@ -1,8 +1,6 @@
package pages
import (
"crypto/sha256"
"encoding/hex"
"net/http"
"os"
"strings"
@@ -43,23 +41,12 @@ func handleMedia(c *fiber.Ctx, url string) error {
url = url + "?" + strings.Split(c.OriginalURL(), "?")[1]
}
optionsHash := ""
if utils.Config.ImageCache {
hasher := sha256.New()
hasher.Write([]byte(url))
optionsHash = hex.EncodeToString(hasher.Sum(nil))
image, err := os.ReadFile(utils.Config.CacheDir + "/" + optionsHash)
if err == nil {
_, err := c.Write(image)
return err
}
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
utils.SetReqHeaders(req)
if c.Get("Range") != "" {
req.Header.Set("Range", c.Get("Range"))
@@ -70,9 +57,11 @@ func handleMedia(c *fiber.Ctx, url string) error {
return err
}
c.Status(res.StatusCode)
if res.StatusCode == 404 {
c.Status(404)
return c.Render("errors/404", nil)
} else if res.StatusCode == 429 {
return c.Render("errors/429", nil)
}
c.Set("Accept-Ranges", "bytes")