diff --git a/src/toascii/models/image.clj b/src/toascii/models/image.clj index 3557ade..c9bcac2 100644 --- a/src/toascii/models/image.clj +++ b/src/toascii/models/image.clj @@ -3,9 +3,8 @@ (javax.imageio.stream ImageInputStream) (java.io Writer)) (:require [clojure.java.io :as io] - [ring.util.io :refer [piped-input-stream]] [clj-image2ascii.core :as i2a] - [toascii.util :refer [query-param-url->java-url]])) + [toascii.util :refer [query-param-url->java-url stream-response]])) (def js-gif-animation (slurp (io/resource "gif-animation.js"))) @@ -26,21 +25,17 @@ (str "
" ascii "
") ascii))) -(defn- write-out [^Writer w s] - (.write w (str s))) - (defn gif->ascii [^ImageInputStream image-stream scale-to-width color?] - (piped-input-stream - (fn [output-stream] - (with-open [^Writer w (io/writer output-stream :append true)] - (write-out w "
") - (i2a/stream-animated-gif-frames! - image-stream scale-to-width color? - (fn [{:keys [image delay]}] - (write-out w (str "
"))
-            (write-out w image)
-            (write-out w "
"))) - (write-out w "
") - (write-out w ""))))) \ No newline at end of file + (stream-response + (fn [^Writer w] + (.write w "
") + (i2a/stream-animated-gif-frames! + image-stream scale-to-width color? + (fn [{:keys [^String image delay]}] + (.write w (str "
"))
+          (.write w image)
+          (.write w "
"))) + (.write w "
") + (.write w "")))) \ No newline at end of file diff --git a/src/toascii/util.clj b/src/toascii/util.clj index 0e3bd4d..bb4aa86 100644 --- a/src/toascii/util.clj +++ b/src/toascii/util.clj @@ -1,7 +1,10 @@ (ns toascii.util - (:import (java.net URL)) + (:import (java.net URL) + (java.io Writer)) (:require [clojure.string :as str] + [clojure.java.io :as io] [clojure.stacktrace :refer [print-stack-trace]] + [ring.util.io :refer [piped-input-stream]] [cemerick.url :as url])) (defn get-filename-without-ext [^String filename] @@ -76,4 +79,10 @@ "0" false 0 false nil false - true))) \ No newline at end of file + true))) + +(defn stream-response [f] + (piped-input-stream + (fn [output-stream] + (with-open [^Writer w (io/writer output-stream)] + (f w))))) \ No newline at end of file