Add a cache for gallery thumbnails (#496)

This commit is contained in:
bnkai
2020-05-11 10:20:08 +03:00
committed by GitHub
parent 8ba76783b0
commit bd45daacf3
36 changed files with 319 additions and 1200 deletions

View File

@@ -23,11 +23,15 @@ func (rs galleryRoutes) Routes() chi.Router {
func (rs galleryRoutes) File(w http.ResponseWriter, r *http.Request) {
gallery := r.Context().Value(galleryKey).(*models.Gallery)
if gallery == nil {
http.Error(w, http.StatusText(404), 404)
return
}
fileIndex, _ := strconv.Atoi(chi.URLParam(r, "fileIndex"))
thumb := r.URL.Query().Get("thumb")
w.Header().Add("Cache-Control", "max-age=604800000") // 1 Week
if thumb == "true" {
_, _ = w.Write(gallery.GetThumbnail(fileIndex, 200))
_, _ = w.Write(cacheGthumb(gallery, fileIndex, models.DefaultGthumbWidth))
} else if thumb == "" {
_, _ = w.Write(gallery.GetImage(fileIndex))
} else {
@@ -36,7 +40,7 @@ func (rs galleryRoutes) File(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(400), 400)
return
}
_, _ = w.Write(gallery.GetThumbnail(fileIndex, int(width)))
_, _ = w.Write(cacheGthumb(gallery, fileIndex, int(width)))
}
}