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