|
65 | 65 |
|
66 | 66 | (defn get-file-content-type [^Path p]
|
67 | 67 | (or (get known-content-types (split-last (.toString p) "."))
|
68 |
| - (Files/probeContentType p))) |
| 68 | + (Files/probeContentType p) |
| 69 | + "application/octet-stream")) |
69 | 70 |
|
70 | 71 | ;; FIXME (arrdem 2018-04-11):
|
71 | 72 | ;; Remove this if-class when we have jdk1.8 min
|
|
74 | 75 | (if-class java.util.Base64
|
75 | 76 | (.encodeToString (Base64/getEncoder) buff)))
|
76 | 77 |
|
77 |
| -(defn slurp-reply [content-type buff] |
| 78 | +(defn slurp-reply [location content-type buff] |
78 | 79 | (let [^String real-type (first content-type)
|
| 80 | + binary? (= "application/octet-stream" real-type) |
79 | 81 | text? (.contains real-type "text")]
|
80 |
| - (if-not text? |
| 82 | + (cond |
| 83 | + binary? |
81 | 84 | {:content-type content-type
|
82 |
| - :content-transfer-encoding "base64" |
83 |
| - :body (base64-bytes buff)} |
| 85 | + :body (str "#binary[location=" location ",size=" (count buff) "]")} |
| 86 | + |
| 87 | + text? |
| 88 | + {:content-type content-type |
| 89 | + :body (String. buff "utf-8")} |
| 90 | + |
| 91 | + :default |
84 | 92 | {:content-type content-type
|
85 |
| - :body (String. buff "utf-8")}))) |
| 93 | + :content-transfer-encoding "base64" |
| 94 | + :body (base64-bytes buff)}))) |
86 | 95 |
|
87 | 96 | (defn slurp-url-to-content+body
|
88 | 97 | "Attempts to parse and then to slurp a URL, producing a content-typed response."
|
|
91 | 100 | (catch MalformedURLException e nil))]
|
92 | 101 | (if (= (.getProtocol url) "file") ;; expected common case
|
93 | 102 | (let [^Path p (Paths/get (.toURI url))
|
94 |
| - content-type (normalize-content-type |
95 |
| - (get-file-content-type p)) |
| 103 | + content-type (normalize-content-type (get-file-content-type p)) |
96 | 104 | buff (Files/readAllBytes p)]
|
97 |
| - (slurp-reply content-type buff)) |
| 105 | + (slurp-reply p content-type buff)) |
98 | 106 |
|
99 | 107 | ;; It's not a file, so just try to open it on up
|
100 | 108 | (let [conn (.openConnection url)
|
|
109 | 117 | (when (<= 0 b)
|
110 | 118 | (.write os b)
|
111 | 119 | (recur))))
|
112 |
| - (slurp-reply content-type (.toByteArray os)))))) |
| 120 | + (slurp-reply url content-type (.toByteArray os)))))) |
113 | 121 |
|
114 | 122 | ;; FIXME (arrdem 2018-04-11):
|
115 | 123 | ;; Remove this if-class when we have jdk1.8 min
|
|
0 commit comments