mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Add cache headers to static files and etags to performer images (#337)
This commit is contained in:
@@ -2,10 +2,13 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/stashapp/stash/pkg/models"
|
"github.com/stashapp/stash/pkg/models"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type performerRoutes struct{}
|
type performerRoutes struct{}
|
||||||
@@ -23,6 +26,16 @@ func (rs performerRoutes) Routes() chi.Router {
|
|||||||
|
|
||||||
func (rs performerRoutes) Image(w http.ResponseWriter, r *http.Request) {
|
func (rs performerRoutes) Image(w http.ResponseWriter, r *http.Request) {
|
||||||
performer := r.Context().Value(performerKey).(*models.Performer)
|
performer := r.Context().Value(performerKey).(*models.Performer)
|
||||||
|
etag := fmt.Sprintf("%x", md5.Sum(performer.Image))
|
||||||
|
|
||||||
|
if match := r.Header.Get("If-None-Match"); match != "" {
|
||||||
|
if strings.Contains(match, etag) {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Add("Etag", etag)
|
||||||
_, _ = w.Write(performer.Image)
|
_, _ = w.Write(performer.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -196,6 +196,10 @@ func Start() {
|
|||||||
data, _ := uiBox.Find("index.html")
|
data, _ := uiBox.Find("index.html")
|
||||||
_, _ = w.Write(data)
|
_, _ = w.Write(data)
|
||||||
} else {
|
} else {
|
||||||
|
isStatic, _ := path.Match("/static/*/*", r.URL.Path)
|
||||||
|
if isStatic {
|
||||||
|
w.Header().Add("Cache-Control", "max-age=604800000")
|
||||||
|
}
|
||||||
http.FileServer(uiBox).ServeHTTP(w, r)
|
http.FileServer(uiBox).ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user