0.2.3 permalink Arrow_down_16x16

wrap-file-info

ring.middleware.file-info

  • (wrap-file-info app & [custom-mime-types])
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.

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source ring/middleware/file_info.clj:88 top

(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)))))
Vars in ring.middleware.file-info/wrap-file-info: -> assoc defn fn instance? let
Used in 0 other vars

Comments top

No comments for wrap-file-info. Log in to add a comment.