Server: Exclude .mp4 and .zip from compression and refactor vary #4018

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2024-01-20 12:48:42 +01:00
parent 58bb52022f
commit 2df0b6e4b1
4 changed files with 17 additions and 16 deletions

View file

@ -12,8 +12,12 @@ import (
// Api is a middleware that sets additional response headers when serving REST API requests.
var Api = func(conf *config.Config) gin.HandlerFunc {
return func(c *gin.Context) {
// Set vary response header.
c.Header(header.Vary, header.DefaultVary)
// Add a vary response header for authentication, if any.
if c.GetHeader(header.XAuthToken) != "" {
c.Writer.Header().Add(header.Vary, header.XAuthToken)
} else if c.GetHeader(header.XSessionID) != "" {
c.Writer.Header().Add(header.Vary, header.XSessionID)
}
// If permitted, set CORS headers (Cross-Origin Resource Sharing).
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

View file

@ -52,6 +52,9 @@ func Start(ctx context.Context, conf *config.Config) {
case "gzip":
router.Use(gzip.Gzip(
gzip.DefaultCompression,
gzip.WithExcludedExtensions([]string{
".png", ".gif", ".jpeg", ".jpg", ".webp", ".mp3", ".mp4", ".zip", ".gz",
}),
gzip.WithExcludedPaths([]string{
conf.BaseUri(config.ApiUri + "/t"),
conf.BaseUri(config.ApiUri + "/folders/t"),
@ -59,7 +62,8 @@ func Start(ctx context.Context, conf *config.Config) {
conf.BaseUri(config.ApiUri + "/albums"),
conf.BaseUri(config.ApiUri + "/labels"),
conf.BaseUri(config.ApiUri + "/videos"),
})))
}),
))
log.Infof("server: enabled gzip compression")
}

View file

@ -64,8 +64,12 @@ func WebDAVAuth(conf *config.Config) gin.HandlerFunc {
return
}
// Set vary response header.
c.Header(header.Vary, header.DefaultVary)
// Add a vary response header for authentication, if any.
if c.GetHeader(header.XAuthToken) != "" {
c.Writer.Header().Add(header.Vary, header.XAuthToken)
} else if c.GetHeader(header.XSessionID) != "" {
c.Writer.Header().Add(header.Vary, header.XSessionID)
}
// Get basic authentication credentials, if any.
username, password, cacheKey, authorized := basicAuth(c)

View file

@ -1,7 +1,5 @@
package header
import "strings"
// Standard content request and response header names.
const (
Accept = "Accept"
@ -23,12 +21,3 @@ const (
ContentTypeJson = "application/json"
ContentTypeJsonUtf8 = "application/json; charset=utf-8"
)
// Vary response header defaults.
//
// Requests that include a standard authorization header should be automatically excluded
// from public caches: https://datatracker.ietf.org/doc/html/rfc7234#section-3
var (
DefaultVaryHeaders = []string{AcceptEncoding, XAuthToken}
DefaultVary = strings.Join(DefaultVaryHeaders, ", ")
)