Permalink
Browse files

Factored out mime-type functions into ring.util.mime-type

  • Loading branch information...
1 parent 9a3cc92 commit b38ad2f95548246a90aaba3ad7f5337c17e15c6a @weavejester weavejester committed Feb 27, 2011
View
99 ring-core/src/ring/middleware/file_info.clj
@@ -1,76 +1,17 @@
(ns ring.middleware.file-info
"Augment Ring File responses."
(:require [ring.util.response :as res])
+ (:use [ring.util.mime-type :only (ext-mime-type)])
(:import java.io.File
(java.util Date Locale TimeZone)
java.text.SimpleDateFormat))
-(def ^{:private true} base-mime-types
- {"ai" "application/postscript"
- "asc" "text/plain"
- "avi" "video/x-msvideo"
- "bin" "application/octet-stream"
- "bmp" "image/bmp"
- "class" "application/octet-stream"
- "cer" "application/pkix-cert"
- "crl" "application/pkix-crl"
- "crt" "application/x-x509-ca-cert"
- "css" "text/css"
- "dms" "application/octet-stream"
- "doc" "application/msword"
- "dvi" "application/x-dvi"
- "eps" "application/postscript"
- "etx" "text/x-setext"
- "exe" "application/octet-stream"
- "gif" "image/gif"
- "htm" "text/html"
- "html" "text/html"
- "jpe" "image/jpeg"
- "jpeg" "image/jpeg"
- "jpg" "image/jpeg"
- "js" "text/javascript"
- "lha" "application/octet-stream"
- "lzh" "application/octet-stream"
- "mov" "video/quicktime"
- "mpe" "video/mpeg"
- "mpeg" "video/mpeg"
- "mpg" "video/mpeg"
- "pbm" "image/x-portable-bitmap"
- "pdf" "application/pdf"
- "pgm" "image/x-portable-graymap"
- "png" "image/png"
- "pnm" "image/x-portable-anymap"
- "ppm" "image/x-portable-pixmap"
- "ppt" "application/vnd.ms-powerpoint"
- "ps" "application/postscript"
- "qt" "video/quicktime"
- "ras" "image/x-cmu-raster"
- "rb" "text/plain"
- "rd" "text/plain"
- "rtf" "application/rtf"
- "sgm" "text/sgml"
- "sgml" "text/sgml"
- "swf" "application/x-shockwave-flash"
- "tif" "image/tiff"
- "tiff" "image/tiff"
- "txt" "text/plain"
- "xbm" "image/x-xbitmap"
- "xls" "application/vnd.ms-excel"
- "xml" "text/xml"
- "xpm" "image/x-xpixmap"
- "xwd" "image/x-xwindowdump"
- "zip" "application/zip"})
-
-(defn- get-extension
- "Returns the file extension of a file."
- [^File file]
- (second (re-find #"\.([^./\\]+)$" (.getPath file))))
-
(defn- guess-mime-type
"Returns a String corresponding to the guessed mime type for the given file,
or application/octet-stream if a type cannot be guessed."
[^File file mime-types]
- (get mime-types (get-extension file) "application/octet-stream"))
+ (or (ext-mime-type (.getPath file) mime-types)
+ "application/octet-stream"))
(defn make-http-format
"Formats or parses dates into HTTP date format (RFC 822/1123)."
@@ -94,21 +35,21 @@
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
+ [app & [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
(res/content-type file-type)
- (res/header "Last-Modified"
- (.format (make-http-format) lmodified)))]
- (if (not-modified-since? req lmodified)
- (-> response (res/status 304)
- (res/header "Content-Length" 0)
- (assoc :body ""))
- (-> response (res/header "Content-Length" file-length))))
- response)))))
+ (res/header
+ "Last-Modified"
+ (.format (make-http-format) lmodified)))]
+ (if (not-modified-since? req lmodified)
+ (-> response (res/status 304)
+ (res/header "Content-Length" 0)
+ (assoc :body ""))
+ (-> response (res/header "Content-Length" file-length))))
+ response))))
View
69 ring-core/src/ring/util/mime_type.clj
@@ -0,0 +1,69 @@
+(ns ring.util.mime-type)
+
+(def default-mime-types
+ {"ai" "application/postscript"
+ "asc" "text/plain"
+ "avi" "video/x-msvideo"
+ "bin" "application/octet-stream"
+ "bmp" "image/bmp"
+ "class" "application/octet-stream"
+ "cer" "application/pkix-cert"
+ "crl" "application/pkix-crl"
+ "crt" "application/x-x509-ca-cert"
+ "css" "text/css"
+ "dms" "application/octet-stream"
+ "doc" "application/msword"
+ "dvi" "application/x-dvi"
+ "eps" "application/postscript"
+ "etx" "text/x-setext"
+ "exe" "application/octet-stream"
+ "gif" "image/gif"
+ "htm" "text/html"
+ "html" "text/html"
+ "jpe" "image/jpeg"
+ "jpeg" "image/jpeg"
+ "jpg" "image/jpeg"
+ "js" "text/javascript"
+ "lha" "application/octet-stream"
+ "lzh" "application/octet-stream"
+ "mov" "video/quicktime"
+ "mpe" "video/mpeg"
+ "mpeg" "video/mpeg"
+ "mpg" "video/mpeg"
+ "pbm" "image/x-portable-bitmap"
+ "pdf" "application/pdf"
+ "pgm" "image/x-portable-graymap"
+ "png" "image/png"
+ "pnm" "image/x-portable-anymap"
+ "ppm" "image/x-portable-pixmap"
+ "ppt" "application/vnd.ms-powerpoint"
+ "ps" "application/postscript"
+ "qt" "video/quicktime"
+ "ras" "image/x-cmu-raster"
+ "rb" "text/plain"
+ "rd" "text/plain"
+ "rtf" "application/rtf"
+ "sgm" "text/sgml"
+ "sgml" "text/sgml"
+ "swf" "application/x-shockwave-flash"
+ "tif" "image/tiff"
+ "tiff" "image/tiff"
+ "txt" "text/plain"
+ "xbm" "image/x-xbitmap"
+ "xls" "application/vnd.ms-excel"
+ "xml" "text/xml"
+ "xpm" "image/x-xpixmap"
+ "xwd" "image/x-xwindowdump"
+ "zip" "application/zip"})
+
+(defn- filename-ext
+ "Returns the file extension of a filename or filepath."
+ [filename]
+ (second (re-find #"\.([^./\\]+)$" filename)))
+
+(defn ext-mime-type
+ "Get the mimetype from the filename extension. Takes an optional map of
+ extensions to mimetypes that overrides values in the default-mime-types map."
+ [filename & [mime-types]]
+ (let [mime-types (merge default-mime-types mime-types)]
+ (mime-types (filename-ext filename))))
View
15 ring-core/test/ring/util/mime_type_test.clj
@@ -0,0 +1,15 @@
+(ns ring.util.mime-type-test
+ (:use clojure.test
+ ring.util.mime-type))
+
+(deftest ext-mime-type-test
+ (testing "default mime types"
+ (are [f m] (= (ext-mime-type f) m)
+ "foo.txt" "text/plain"
+ "foo.html" "text/html"
+ "foo.png" "image/png"))
+ (testing "custom mime types"
+ (is (= (ext-mime-type "foo.bar" {"bar" "application/bar"})
+ "application/bar"))
+ (is (= (ext-mime-type "foo.txt" {"txt" "application/text"})
+ "application/text"))))

0 comments on commit b38ad2f

Please sign in to comment.