(defn wrap-file-info
"Wrap an app such that responses with a file a body will have corresponding
Content-Type, Content-Length, and Last Modified headers added if they can be
determined from the file.
If the request specifies a If-Modified-Since header that matches the last
modification date of the file, a 304 Not Modified response is returned.
If two arguments are given, the second is taken to be a map of file extensions
to content types that will supplement the default, built-in map."
[app & [custom-mime-types]]
(let [mime-types (merge base-mime-types custom-mime-types)]
(fn [req]
(let [{:keys [headers body] :as response} (app req)]
(if (instance? File body)
(let [file-type (guess-mime-type body mime-types)
file-length (.length #^File body)
lmodified (Date. (.lastModified #^File body))
response (-> response
(content-type file-type)
(header "Last-Modified"
(.format http-format lmodified)))]
(if (not-modified-since? req lmodified)
(-> response (status 304)
(header "Content-Length" 0)
(assoc :body ""))
(-> response (header "Content-Length" file-length))))
response)))))
Used in 0 other vars
Comments top
No comments for wrap-file-info. Log in to add a comment.