From b323b26b571101c48b5d4273cac6e639995a2d3e Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Sat, 4 Jun 2022 20:44:54 -0400 Subject: [PATCH] Use WebP if the browser supports it (#32) --- README.md | 1 + pages/media.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 2f6728b..2c3762f 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ rimgo can be configured using environment variables. | PORT | 3000 | | ADDRESS | 0.0.0.0 | | IMGUR_CLIENT_ID | 546c25a59c58ad7 | +| FORCE_WEBP | 0 | ## Contributing diff --git a/pages/media.go b/pages/media.go index ac4bcd4..f02ba09 100644 --- a/pages/media.go +++ b/pages/media.go @@ -2,7 +2,9 @@ package pages import ( "net/http" + "os" "strconv" + "strings" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/gofiber/fiber/v2" @@ -27,6 +29,12 @@ func handleMedia(c *fiber.Ctx, url string) error { utils.SetHeaders(c) c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content") + if os.Getenv("FORCE_WEBP") == "1" && c.Query("no_webp") != "" && c.Accepts("image/webp") == "image/webp" { + url = strings.ReplaceAll(url, ".png", ".webp") + url = strings.ReplaceAll(url, ".jpg", ".webp") + url = strings.ReplaceAll(url, ".jpeg", ".webp") + } + res, err := http.Get(url) if err != nil { return err