···
mimeType = "image/svg+xml"
// allow image, video, and text/plain files to be served directly
-
case strings.HasPrefix(mimeType, "image/"):
-
case strings.HasPrefix(mimeType, "video/"):
case strings.HasPrefix(mimeType, "text/plain"):
l.Error("attempted to serve disallowed file type", "mimetype", mimeType)
writeError(w, "only image, video, and text files can be accessed directly", http.StatusForbidden)
-
w.Header().Set("Cache-Control", "public, max-age=86400") // cache for 24 hours
-
w.Header().Set("ETag", fmt.Sprintf("%x", sha256.Sum256(contents)))
w.Header().Set("Content-Type", mimeType)
···
mimeType = "image/svg+xml"
+
contentHash := sha256.Sum256(contents)
+
eTag := fmt.Sprintf("\"%x\"", contentHash)
// allow image, video, and text/plain files to be served directly
+
case strings.HasPrefix(mimeType, "image/"), strings.HasPrefix(mimeType, "video/"):
+
if clientETag := r.Header.Get("If-None-Match"); clientETag == eTag {
+
w.WriteHeader(http.StatusNotModified)
+
w.Header().Set("ETag", eTag)
case strings.HasPrefix(mimeType, "text/plain"):
+
w.Header().Set("Cache-Control", "public, no-cache")
l.Error("attempted to serve disallowed file type", "mimetype", mimeType)
writeError(w, "only image, video, and text files can be accessed directly", http.StatusForbidden)
w.Header().Set("Content-Type", mimeType)