fix outputting image as plain text or html
This commit is contained in:
parent
e91a6f50a7
commit
171299159e
|
@ -6,7 +6,8 @@
|
||||||
(javax.imageio ImageIO)
|
(javax.imageio ImageIO)
|
||||||
(java.io File)
|
(java.io File)
|
||||||
(java.net URL URLEncoder URI))
|
(java.net URL URLEncoder URI))
|
||||||
(:require [cemerick.url :as url]
|
(:require [clojure.string :as str]
|
||||||
|
[cemerick.url :as url]
|
||||||
[toascii.util :refer [query-param-url->java-url]])
|
[toascii.util :refer [query-param-url->java-url]])
|
||||||
(:use hiccup.core))
|
(:use hiccup.core))
|
||||||
|
|
||||||
|
@ -51,11 +52,8 @@
|
||||||
(map #(conj % (if color? [:br] \newline)))
|
(map #(conj % (if color? [:br] \newline)))
|
||||||
(apply concat))]
|
(apply concat))]
|
||||||
(if color?
|
(if color?
|
||||||
(html
|
(html output)
|
||||||
[:pre
|
(str/join output))))
|
||||||
{:style "font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;"}
|
|
||||||
output])
|
|
||||||
output)))
|
|
||||||
|
|
||||||
(defn get-image-by-url
|
(defn get-image-by-url
|
||||||
(^BufferedImage [^String url]
|
(^BufferedImage [^String url]
|
||||||
|
@ -99,3 +97,6 @@
|
||||||
(scale-image image new-width)
|
(scale-image image new-width)
|
||||||
image)]
|
image)]
|
||||||
(pixels->ascii final-image color?))))
|
(pixels->ascii final-image color?))))
|
||||||
|
|
||||||
|
(defn wrap-pre-tag [s]
|
||||||
|
(str "<pre style=\"font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;\">" s "</pre>"))
|
|
@ -4,7 +4,7 @@
|
||||||
[liberator.core :refer [defresource]]
|
[liberator.core :refer [defresource]]
|
||||||
[compojure.core :refer [ANY]]
|
[compojure.core :refer [ANY]]
|
||||||
[toascii.route-utils :refer [register-routes]]
|
[toascii.route-utils :refer [register-routes]]
|
||||||
[toascii.models.image :refer [convert-image get-image-by-url]]
|
[toascii.models.image :refer [convert-image get-image-by-url wrap-pre-tag]]
|
||||||
[toascii.util :refer [parse-int parse-boolean]]))
|
[toascii.util :refer [parse-int parse-boolean]]))
|
||||||
|
|
||||||
(defn- color? [color]
|
(defn- color? [color]
|
||||||
|
@ -25,11 +25,9 @@
|
||||||
:malformed?
|
:malformed?
|
||||||
(fn [_]
|
(fn [_]
|
||||||
(cond
|
(cond
|
||||||
(str/blank? url)
|
(str/blank? url) {:error "Missing image url"}
|
||||||
{:error "Missing image url"}
|
(and width
|
||||||
|
(nil? (parse-int width))) {:error "Invalid image width."}))
|
||||||
(and width (nil? (parse-int width)))
|
|
||||||
{:error "Invalid image width."}))
|
|
||||||
:exists?
|
:exists?
|
||||||
(fn [_]
|
(fn [_]
|
||||||
(if-let [image (get-image-by-url url)]
|
(if-let [image (get-image-by-url url)]
|
||||||
|
@ -37,10 +35,14 @@
|
||||||
[false {:error "Image could not be loaded."}]))
|
[false {:error "Image could not be loaded."}]))
|
||||||
:handle-ok
|
:handle-ok
|
||||||
(fn [ctx]
|
(fn [ctx]
|
||||||
(convert-image
|
(let [html? (= "text/html" (get-in ctx [:representation :media-type]))
|
||||||
|
output (convert-image
|
||||||
(:image ctx)
|
(:image ctx)
|
||||||
(parse-int width)
|
(parse-int width)
|
||||||
(color? color)))
|
(color? color))]
|
||||||
|
(if html?
|
||||||
|
(wrap-pre-tag output)
|
||||||
|
output)))
|
||||||
:handle-malformed
|
:handle-malformed
|
||||||
(fn [ctx]
|
(fn [ctx]
|
||||||
(:error ctx))
|
(:error ctx))
|
||||||
|
|
Reference in a new issue