fix outputting image as plain text or html

This commit is contained in:
Gered 2014-03-30 11:33:58 -04:00
parent e91a6f50a7
commit 171299159e
2 changed files with 20 additions and 17 deletions

View file

@ -6,7 +6,8 @@
(javax.imageio ImageIO)
(java.io File)
(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]])
(:use hiccup.core))
@ -51,11 +52,8 @@
(map #(conj % (if color? [:br] \newline)))
(apply concat))]
(if color?
(html
[:pre
{:style "font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;"}
output])
output)))
(html output)
(str/join output))))
(defn get-image-by-url
(^BufferedImage [^String url]
@ -99,3 +97,6 @@
(scale-image image new-width)
image)]
(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>"))

View file

@ -4,7 +4,7 @@
[liberator.core :refer [defresource]]
[compojure.core :refer [ANY]]
[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]]))
(defn- color? [color]
@ -25,11 +25,9 @@
:malformed?
(fn [_]
(cond
(str/blank? url)
{:error "Missing image url"}
(and width (nil? (parse-int width)))
{:error "Invalid image width."}))
(str/blank? url) {:error "Missing image url"}
(and width
(nil? (parse-int width))) {:error "Invalid image width."}))
:exists?
(fn [_]
(if-let [image (get-image-by-url url)]
@ -37,10 +35,14 @@
[false {:error "Image could not be loaded."}]))
:handle-ok
(fn [ctx]
(convert-image
(:image ctx)
(parse-int width)
(color? color)))
(let [html? (= "text/html" (get-in ctx [:representation :media-type]))
output (convert-image
(:image ctx)
(parse-int width)
(color? color))]
(if html?
(wrap-pre-tag output)
output)))
:handle-malformed
(fn [ctx]
(:error ctx))