Permalink
Please sign in to comment.
Browse files
Factored out mime-type functions into ring.util.mime-type
- Loading branch information...
Showing
with
104 additions
and 79 deletions.
99
ring-core/src/ring/middleware/file_info.clj
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)))) |
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