Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns creator.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [om.core :as om :include-macros true]
[cljs.core.async :as async :refer [chan <! >! put!]]
[om-tools.core :refer-macros [defcomponent]]
[cljs.reader :as reader]
[goog.dom :as gdom]
[om-tools.dom :as dom :include-macros true])
(:import [goog.net XhrIo]))
(enable-console-print!)
(defn log [o]
(.log js/console o))
(defn file->map [f]
{:name (.-name f)
:type (.-type f)
:size (.-size f)})
(defn sign-file [file ch]
(let [fmap (file->map file)
edn-ize #(reader/read-string (.getResponseText (.-target %)))]
(.send XhrIo (str "/sign?file-name=" (:name fmap) "&mime-type=" (:type fmap))
(fn [res] (put! ch {:f file :signature (edn-ize res)})))))
(defn formdata-from-map [m]
(let [fd (new js/FormData)]
(doseq [[k v] m]
(.append fd (name k) v))
fd))
(defn upload-file [upload-info ch]
(let [sig (select-keys (:signature upload-info) [:key :Content-Type :success_action_status :policy :AWSAccessKeyId :signature :acl])
fd (formdata-from-map (merge sig {:file (:f upload-info)}))]
(.send XhrIo
(:action (:signature upload-info))
(fn [res] (put! ch (.getResponseXml (.-target res))))
"POST"
fd)))
(defn s3-upload [report-chan]
(let [upload-files (map #(upload-file % report-chan))
upload-chan (chan 10 upload-files)
sign-files (map #(sign-file % upload-chan))
signing-chan (chan 10 sign-files)]
(go (while true
(let [[v ch] (alts! [signing-chan upload-chan])]
; that's not really required but has been useful
(log v))))
signing-chan))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment