Use WebP if the browser supports it (#32)

This commit is contained in:
video-prize-ranch 2022-06-04 20:44:54 -04:00
parent f3ac0369b1
commit b323b26b57
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
2 changed files with 9 additions and 0 deletions

View File

@ -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

View File

@ -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