Skip to content

Instantly share code, notes, and snippets.

@tnoborio
Created August 7, 2011 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnoborio/1130459 to your computer and use it in GitHub Desktop.
Save tnoborio/1130459 to your computer and use it in GitHub Desktop.
Clojure on Heroku
(ns demo.web
(use ring.adapter.jetty
compojure.core
[clojure.java.io :only [input-stream]]
[fleet :only [fleet-ns]]
[ring.util.response :only [response]]
[compojure.response :only [Renderable]])
(require [compojure [route :as route] [handler :as handler]])
(import java.io.ByteArrayOutputStream
javax.imageio.ImageIO
java.awt.image.BufferedImage
java.awt.image.BufferedImage))
(defn make-thumbnail* [in-stream out-stream width]
(let [img (ImageIO/read in-stream)
width (min (.getWidth img) width)
height (* (/ width (.getWidth img)) (.getHeight img))
simg (BufferedImage. width height
(BufferedImage/TYPE_INT_ARGB))
g (.createGraphics simg)]
(do
(.drawImage g img 0 0 width height nil)
(.dispose g)
(ImageIO/write simg "png" out-stream))))
(defn make-thumbnail [{{{f :tempfile} :file} :params :as req}]
(let [out (ByteArrayOutputStream.)]
(make-thumbnail* (input-stream f)
out 100)
(input-stream (.toByteArray out))))
(extend-protocol Renderable
fleet.util.CljString
(render [this _] (response (.toString this))))
(fleet-ns templates "templates" [:fleet :bypass])
(defroutes main-routes
(GET "/" _ (templates.upload/index))
(POST "/" _ make-thumbnail))
(def app
(-> #'main-routes
handler/site))
(defn -main []
(let [port (Integer/parseInt (or (System/getenv "PORT")
"8080"))]
(run-jetty (var app) {:port port})))
;; (-main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment