Permalink
Newer
Older
100644 87 lines (82 sloc) 2.84 KB
cbeebac @mmcgrana Initial refactor towards 0.1.
authored Jul 11, 2009
1 (ns ring.middleware.file-info
9fb5ee7 @weavejester Removed Apache Commons IO dependency
weavejester authored Jan 24, 2010
2 (:use clojure.contrib.def)
3 (:import java.io.File))
68e08fb @mmcgrana Initial import from http://github.com/mmcgrana/clj-garden
authored Jan 12, 2009
4
e4a74d5 @mmcgrana Remove legacy support for built-in currying in ring.file, add support…
authored Apr 10, 2009
5 (defvar- base-mime-types
68e08fb @mmcgrana Initial import from http://github.com/mmcgrana/clj-garden
authored Jan 12, 2009
6 {"ai" "application/postscript"
7 "asc" "text/plain"
8 "avi" "video/x-msvideo"
9 "bin" "application/octet-stream"
10 "bmp" "image/bmp"
11 "class" "application/octet-stream"
12 "cer" "application/pkix-cert"
13 "crl" "application/pkix-crl"
14 "crt" "application/x-x509-ca-cert"
15 "css" "text/css"
16 "dms" "application/octet-stream"
17 "doc" "application/msword"
18 "dvi" "application/x-dvi"
19 "eps" "application/postscript"
20 "etx" "text/x-setext"
21 "exe" "application/octet-stream"
22 "gif" "image/gif"
23 "htm" "text/html"
24 "html" "text/html"
25 "jpe" "image/jpeg"
26 "jpeg" "image/jpeg"
27 "jpg" "image/jpeg"
28 "js" "text/javascript"
29 "lha" "application/octet-stream"
30 "lzh" "application/octet-stream"
31 "mov" "video/quicktime"
32 "mpe" "video/mpeg"
33 "mpeg" "video/mpeg"
34 "mpg" "video/mpeg"
35 "pbm" "image/x-portable-bitmap"
36 "pdf" "application/pdf"
37 "pgm" "image/x-portable-graymap"
38 "png" "image/png"
39 "pnm" "image/x-portable-anymap"
40 "ppm" "image/x-portable-pixmap"
41 "ppt" "application/vnd.ms-powerpoint"
42 "ps" "application/postscript"
43 "qt" "video/quicktime"
44 "ras" "image/x-cmu-raster"
45 "rb" "text/plain"
46 "rd" "text/plain"
47 "rtf" "application/rtf"
48 "sgm" "text/sgml"
49 "sgml" "text/sgml"
d2a3017 @mmcgrana Add default content type info for swf flash files
authored Apr 10, 2009
50 "swf" "application/x-shockwave-flash"
68e08fb @mmcgrana Initial import from http://github.com/mmcgrana/clj-garden
authored Jan 12, 2009
51 "tif" "image/tiff"
52 "tiff" "image/tiff"
53 "txt" "text/plain"
54 "xbm" "image/x-xbitmap"
55 "xls" "application/vnd.ms-excel"
56 "xml" "text/xml"
57 "xpm" "image/x-xpixmap"
58 "xwd" "image/x-xwindowdump"
59 "zip" "application/zip"})
60
9fb5ee7 @weavejester Removed Apache Commons IO dependency
weavejester authored Jan 24, 2010
61 (defn- get-extension
62 "Returns the file extension of a file."
f2f4229 @mmcgrana Doc and example tweaks.
authored Feb 23, 2010
63 [#^File file]
9fb5ee7 @weavejester Removed Apache Commons IO dependency
weavejester authored Jan 24, 2010
64 (second (re-find #"\.([^./\\]+)$" (.getPath file))))
65
68e08fb @mmcgrana Initial import from http://github.com/mmcgrana/clj-garden
authored Jan 12, 2009
66 (defn- guess-mime-type
67 "Returns a String corresponding to the guessed mime type for the given file,
68 or application/octet-stream if a type cannot be guessed."
e4a74d5 @mmcgrana Remove legacy support for built-in currying in ring.file, add support…
authored Apr 10, 2009
69 [#^File file mime-types]
9fb5ee7 @weavejester Removed Apache Commons IO dependency
weavejester authored Jan 24, 2010
70 (get mime-types (get-extension file) "application/octet-stream"))
68e08fb @mmcgrana Initial import from http://github.com/mmcgrana/clj-garden
authored Jan 12, 2009
71
cbeebac @mmcgrana Initial refactor towards 0.1.
authored Jul 11, 2009
72 (defn wrap-file-info
68e08fb @mmcgrana Initial import from http://github.com/mmcgrana/clj-garden
authored Jan 12, 2009
73 "Wrap an app such that responses with a file a body will have
74 corresponding Content-Type and Content-Length headers added if they are not
e4a74d5 @mmcgrana Remove legacy support for built-in currying in ring.file, add support…
authored Apr 10, 2009
75 allready present and can be determined from the file. If two arguments are
cbeebac @mmcgrana Initial refactor towards 0.1.
authored Jul 11, 2009
76 given, the second is taken to be a map of file extensions to content types
77 that will supplement the default, built-in map."
78 [app & [custom-mime-types]]
79 (let [mime-types (merge base-mime-types custom-mime-types)]
80 (fn [req]
81 (let [{:keys [headers body] :as response} (app req)]
82 (if (instance? File body)
83 (assoc response :headers
fa4ce79 @mmcgrana Avoid all reflection. Also, avoid some potential NPE problems.
authored Dec 6, 2009
84 (assoc headers "Content-Length" (str (.length #^File body))
cbeebac @mmcgrana Initial refactor towards 0.1.
authored Jul 11, 2009
85 "Content-Type" (guess-mime-type body mime-types)))
86 response)))))