Previews: use ETag header (#51008) (#51041)

* use etag header for previews

* use unix millis rather than tz-dependant string

(cherry picked from commit 644503f5e6)

Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2022-06-17 16:16:24 +02:00
committed by GitHub
co-authored by Artur Wierzbicki
parent 89d6f7aba1
commit 75501eac48
+10 -1
View File
@@ -227,12 +227,21 @@ func (hs *thumbService) GetImage(c *models.ReqContext) {
return
}
if err != nil {
if err != nil || res == nil {
hs.log.Error("Error when retrieving thumbnail", "dashboardUid", req.UID, "err", err.Error())
c.JSON(500, map[string]string{"dashboardUID": req.UID, "error": "unknown"})
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)
if _, err := c.Resp.Write(res.Image); err != nil {
hs.log.Error("Error writing to response", "dashboardUid", req.UID, "err", err)