mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-03-03 04:39:30 +00:00
Force WebP improvements (#237)
- Allow using `true` or `1` in configuration - Set Content-Disposition header to use correct file extension when using "Save image as" - Only rewrite requests from img elements using Sec-Fetch-Dest header #200 #201 Reviewed-on: https://codeberg.org/rimgo/rimgo/pulls/237 Reviewed-by: orangix <orangix@noreply.codeberg.org> Co-authored-by: video-prize-ranch <cb.8a3w5@simplelogin.co> Co-committed-by: video-prize-ranch <cb.8a3w5@simplelogin.co>
This commit is contained in:
committed by
video-prize-ranch
parent
e8ed026962
commit
3b8ad3f360
@@ -1,8 +1,8 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/rimgo/rimgo/utils"
|
||||
@@ -34,10 +34,17 @@ func HandleUserAvatar(c *fiber.Ctx) error {
|
||||
func handleMedia(c *fiber.Ctx, url string) error {
|
||||
utils.SetHeaders(c)
|
||||
|
||||
if os.Getenv("FORCE_WEBP") == "1" && c.Query("no_webp") == "" && c.Accepts("image/webp") == "image/webp" && !strings.HasPrefix(c.Path(), "/stack") {
|
||||
if utils.Config.ForceWebp &&
|
||||
!strings.HasSuffix(c.Path(), ".webp") &&
|
||||
c.Get("Sec-Fetch-Dest") == "image" &&
|
||||
c.Query("no_webp") == "" &&
|
||||
c.Accepts("image/webp") == "image/webp" &&
|
||||
!strings.HasPrefix(c.Path(), "/stack") {
|
||||
url = strings.ReplaceAll(url, ".png", ".webp")
|
||||
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
||||
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
||||
filename := strings.TrimPrefix(c.Path(), "/")
|
||||
c.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename*": filename}))
|
||||
}
|
||||
|
||||
if strings.HasPrefix(c.Path(), "/stack") && strings.Contains(c.OriginalURL(), "?") {
|
||||
|
||||
Reference in New Issue
Block a user