Previews: use ETag header (#51008)

* use etag header for previews

* use unix millis rather than tz-dependant string
This commit is contained in:
Artur Wierzbicki 2022-06-17 18:02:03 +04:00 committed by GitHub
parent 204e9754da
commit 644503f5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -227,12 +227,21 @@ func (hs *thumbService) GetImage(c *models.ReqContext) {
return return
} }
if err != nil { if err != nil || res == nil {
hs.log.Error("Error when retrieving thumbnail", "dashboardUid", req.UID, "err", err.Error()) hs.log.Error("Error when retrieving thumbnail", "dashboardUid", req.UID, "err", err.Error())
c.JSON(500, map[string]string{"dashboardUID": req.UID, "error": "unknown"}) c.JSON(500, map[string]string{"dashboardUID": req.UID, "error": "unknown"})
return return
} }
currentEtag := fmt.Sprintf("%d", res.Updated.Unix())
c.Resp.Header().Set("ETag", currentEtag)
previousEtag := c.Req.Header.Get("If-None-Match")
if previousEtag == currentEtag {
c.Resp.WriteHeader(http.StatusNotModified)
return
}
c.Resp.Header().Set("Content-Type", res.MimeType) c.Resp.Header().Set("Content-Type", res.MimeType)
if _, err := c.Resp.Write(res.Image); err != nil { if _, err := c.Resp.Write(res.Image); err != nil {
hs.log.Error("Error writing to response", "dashboardUid", req.UID, "err", err) hs.log.Error("Error writing to response", "dashboardUid", req.UID, "err", err)