Skip to content

Commit

Permalink
Add cache headers to static files and etags to performer images (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteTF committed Jan 31, 2020
1 parent 6a6e8d8 commit cd9c6e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/api/routes_performer.go
Expand Up @@ -2,10 +2,13 @@ package api

import (
"context"
"crypto/md5"
"fmt"
"github.com/go-chi/chi"
"github.com/stashapp/stash/pkg/models"
"net/http"
"strconv"
"strings"
)

type performerRoutes struct{}
Expand All @@ -23,6 +26,16 @@ func (rs performerRoutes) Routes() chi.Router {

func (rs performerRoutes) Image(w http.ResponseWriter, r *http.Request) {
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)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/api/server.go
Expand Up @@ -196,6 +196,10 @@ func Start() {
data, _ := uiBox.Find("index.html")
_, _ = w.Write(data)
} else {
isStatic, _ := path.Match("/static/*/*", r.URL.Path)
if isStatic {
w.Header().Add("Cache-Control", "max-age=604800000")
}
http.FileServer(uiBox).ServeHTTP(w, r)
}
})
Expand Down

0 comments on commit cd9c6e9

Please sign in to comment.