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
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"codeberg.org/rimgo/rimgo/utils"
|
"codeberg.org/rimgo/rimgo/utils"
|
||||||
@@ -34,10 +34,17 @@ func HandleUserAvatar(c *fiber.Ctx) error {
|
|||||||
func handleMedia(c *fiber.Ctx, url string) error {
|
func handleMedia(c *fiber.Ctx, url string) error {
|
||||||
utils.SetHeaders(c)
|
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, ".png", ".webp")
|
||||||
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
||||||
url = strings.ReplaceAll(url, ".jpeg", ".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(), "?") {
|
if strings.HasPrefix(c.Path(), "/stack") && strings.Contains(c.OriginalURL(), "?") {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type config struct {
|
|||||||
ProtocolDetection bool
|
ProtocolDetection bool
|
||||||
Secure bool
|
Secure bool
|
||||||
FiberPrefork bool
|
FiberPrefork bool
|
||||||
|
ForceWebp bool
|
||||||
ImageCache bool
|
ImageCache bool
|
||||||
CleanupInterval time.Duration
|
CleanupInterval time.Duration
|
||||||
CacheDir string
|
CacheDir string
|
||||||
@@ -52,18 +53,19 @@ func LoadConfig() {
|
|||||||
ProtocolDetection: os.Getenv("PROTOCOL_DETECTION") == "true" || os.Getenv("PROTOCOL_DETECTION") == "1",
|
ProtocolDetection: os.Getenv("PROTOCOL_DETECTION") == "true" || os.Getenv("PROTOCOL_DETECTION") == "1",
|
||||||
Secure: os.Getenv("SECURE") == "true",
|
Secure: os.Getenv("SECURE") == "true",
|
||||||
FiberPrefork: os.Getenv("FIBER_PREFORK") == "true",
|
FiberPrefork: os.Getenv("FIBER_PREFORK") == "true",
|
||||||
|
ForceWebp: os.Getenv("FORCE_WEBP") == "true" || os.Getenv("FORCE_WEBP") == "1",
|
||||||
Privacy: map[string]interface{}{
|
Privacy: map[string]interface{}{
|
||||||
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
||||||
"policy": os.Getenv("PRIVACY_POLICY"),
|
"policy": os.Getenv("PRIVACY_POLICY"),
|
||||||
"message": os.Getenv("PRIVACY_MESSAGE"),
|
"message": os.Getenv("PRIVACY_MESSAGE"),
|
||||||
"country": os.Getenv("PRIVACY_COUNTRY"),
|
"country": os.Getenv("PRIVACY_COUNTRY"),
|
||||||
"provider": os.Getenv("PRIVACY_PROVIDER"),
|
"provider": os.Getenv("PRIVACY_PROVIDER"),
|
||||||
"cloudflare": os.Getenv("PRIVACY_CLOUDFLARE") == "true",
|
"cloudflare": os.Getenv("PRIVACY_CLOUDFLARE") == "true" || os.Getenv("PRIVACY_CLOUDFLARE") == "1",
|
||||||
"not_collected": os.Getenv("PRIVACY_NOT_COLLECTED") == "true",
|
"not_collected": os.Getenv("PRIVACY_NOT_COLLECTED") == "true" || os.Getenv("PRIVACY_NOT_COLLECTED") == "1",
|
||||||
"ip": os.Getenv("PRIVACY_IP") == "true",
|
"ip": os.Getenv("PRIVACY_IP") == "true" || os.Getenv("PRIVACY_IP") == "1",
|
||||||
"url": os.Getenv("PRIVACY_URL") == "true",
|
"url": os.Getenv("PRIVACY_URL") == "true" || os.Getenv("PRIVACY_URL") == "1",
|
||||||
"device": os.Getenv("PRIVACY_DEVICE") == "true",
|
"device": os.Getenv("PRIVACY_DEVICE") == "true" || os.Getenv("PRIVACY_DEVICE") == "1",
|
||||||
"diagnostics": os.Getenv("PRIVACY_DIAGNOSTICS") == "true",
|
"diagnostics": os.Getenv("PRIVACY_DIAGNOSTICS") == "true" || os.Getenv("PRIVACY_DIAGNOSTICS") == "1",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user