add response streaming to gif->ascii
This commit is contained in:
parent
a6ddbe8129
commit
1732ccf341
|
@ -1,8 +1,10 @@
|
||||||
(ns toascii.models.image
|
(ns toascii.models.image
|
||||||
(:import (java.awt.image BufferedImage)
|
(:import (java.awt.image BufferedImage)
|
||||||
|
(java.io Writer)
|
||||||
(javax.imageio.stream ImageInputStream))
|
(javax.imageio.stream ImageInputStream))
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
|
[ring.util.io :refer [piped-input-stream]]
|
||||||
[clj-image2ascii.core :as i2a]
|
[clj-image2ascii.core :as i2a]
|
||||||
[toascii.util :refer [query-param-url->java-url]])
|
[toascii.util :refer [query-param-url->java-url]])
|
||||||
(:use hiccup.core))
|
(:use hiccup.core))
|
||||||
|
@ -19,7 +21,7 @@
|
||||||
(let [java-url (query-param-url->java-url url)]
|
(let [java-url (query-param-url->java-url url)]
|
||||||
(i2a/get-image-stream-by-url java-url)))
|
(i2a/get-image-stream-by-url java-url)))
|
||||||
|
|
||||||
(defn wrap-pre-tag [s & {:keys [hidden? delay]}]
|
(defn- wrap-pre-tag [s & {:keys [hidden? delay]}]
|
||||||
(str
|
(str
|
||||||
"<pre style=\""
|
"<pre style=\""
|
||||||
ascii-pre-css
|
ascii-pre-css
|
||||||
|
@ -30,6 +32,17 @@
|
||||||
(str " data-delay=\"" delay "\""))
|
(str " data-delay=\"" delay "\""))
|
||||||
">" s "</pre>"))
|
">" s "</pre>"))
|
||||||
|
|
||||||
|
(defn- get-open-pre-tag [hidden? delay]
|
||||||
|
(str
|
||||||
|
"<pre style=\""
|
||||||
|
ascii-pre-css
|
||||||
|
(if hidden?
|
||||||
|
" display: none;")
|
||||||
|
"\""
|
||||||
|
(if delay
|
||||||
|
(str " data-delay=\"" delay "\""))
|
||||||
|
">"))
|
||||||
|
|
||||||
(defn image->ascii [^BufferedImage image scale-to-width color? html?]
|
(defn image->ascii [^BufferedImage image scale-to-width color? html?]
|
||||||
(let [converted (i2a/convert-image image scale-to-width color?)
|
(let [converted (i2a/convert-image image scale-to-width color?)
|
||||||
ascii (:image converted)]
|
ascii (:image converted)]
|
||||||
|
@ -37,14 +50,21 @@
|
||||||
(wrap-pre-tag ascii)
|
(wrap-pre-tag ascii)
|
||||||
ascii)))
|
ascii)))
|
||||||
|
|
||||||
|
(defn- write-out [^Writer w s]
|
||||||
|
(.write w (str s)))
|
||||||
|
|
||||||
(defn gif->ascii [^ImageInputStream image-stream scale-to-width color?]
|
(defn gif->ascii [^ImageInputStream image-stream scale-to-width color?]
|
||||||
(as-> image-stream x
|
(piped-input-stream
|
||||||
(i2a/convert-animated-gif-frames x scale-to-width color?)
|
(fn [output-stream]
|
||||||
(:frames x)
|
(with-open [^Writer w (io/writer output-stream :append true)]
|
||||||
(map
|
(write-out w "<div class=\"animated-gif-frames\">")
|
||||||
(fn [frame]
|
(i2a/stream-animated-gif-frames!
|
||||||
(wrap-pre-tag (:image frame) :delay (:delay frame) :hidden? true))
|
image-stream scale-to-width color?
|
||||||
x)
|
(fn [{:keys [image delay]}]
|
||||||
(str/join x)
|
(write-out w (get-open-pre-tag true delay))
|
||||||
(str "<div class=\"animated-gif-frames\">" x "</div>")
|
(write-out w image)
|
||||||
(str x "<script type=\"text/javascript\">" js-gif-animation "</script>")))
|
(write-out w "</pre>")))
|
||||||
|
(write-out w "</div>")
|
||||||
|
(write-out w "<script type=\"text/javascript\">")
|
||||||
|
(write-out w js-gif-animation)
|
||||||
|
(write-out w "</script>")))))
|
Reference in a new issue