Skip to content

Instantly share code, notes, and snippets.

@irneh
Created April 4, 2014 00:57
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 irneh/9965973 to your computer and use it in GitHub Desktop.
Save irneh/9965973 to your computer and use it in GitHub Desktop.
(ns guestbook.routes.home
(:use compojure.core)
(:use hiccup.core)
(require [me.raynes.conch :as conch]
[amazonica.aws.s3 :as s3]
[clojure.data.json :as json]
[noir.io :as io]
[clojurewerkz.cassaforte.query :as cql-query]
[clojurewerkz.cassaforte.client :as cql-client]))
(def s3-bucket "phototimeline")
(def tmp-dir "/tmp/")
(defn time-now []
(java.util.Date.))
(defn uuid []
(str (java.util.UUID/randomUUID)))
(defn str->java-uuid [s]
(java.util.UUID/fromString s))
(defn str->java-date [s]
(let [df (java.text.SimpleDateFormat. "yyyy:MM:dd HH:mm:ssXXX")]
(.parse df s)))
(defn file->exif [path]
(conch/programs exiftool)
(first (json/read-str (exiftool "-json" path))))
(defn resize-img [source target width height]
(let [dimensions (str width "x" height)]
(conch/programs convert)
(convert source
"-resize"
dimensions
target)))
(defn write-img [k f]
(s3/put-object :bucket-name s3-bucket
:key k
:file f))
(defn get-exif-date [exif-map]
;; TODO: Ask Chris for fallback options.
(get exif-map "FileModifyDate"))
(defn write-db [id userid taken]
(let [values {:id (str->java-uuid id)
:userid (str->java-uuid userid)
:taken (str->java-date taken)
:inserted (time-now)}]
(cql-client/prepared
(cql/insert "photos" values))))
(defn upload [userid file]
(let [filename (uuid)
exts [".0.jpg" ".1.jpg" ".2.jpg"]
names (into [] (map #(str filename %) exts))
paths (into [] (map #(str tmp-dir %) names))
;; Rename the uploading file.
file (assoc-in file [:filename] (names 0))]
(io/upload-file tmp-dir file)
(resize-img (paths 0) (paths 1) 595 296)
(resize-img (paths 1) (paths 2) 180 120)
(eval (map write-img names paths))
(let [taken (-> (paths 0)
file->exif
get-exif-date)]
(write-db filename userid taken)
(html taken))))
(defroutes home-routes
(POST "/upload" [userid file] (upload userid file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment