Created
August 30, 2014 14:37
-
-
Save martinklepsch/4d731346e7693b6401a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn file->map [f] | |
{:name (.-name f) | |
:type (.-type f) | |
:size (.-size f)}) | |
(defn sign-file [file] | |
(let [out (chan) | |
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! out {:f file :signature (edn-ize res)}))) | |
out)) | |
(defn queue-file [e owner {:keys [upload-queue]}] | |
(put! upload-queue (sign-file (first (array-seq (.. e -target -files)))))) | |
(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] | |
(let [out (chan) | |
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! out (.getResponseText (.-target res)))) | |
"POST" | |
fd) | |
out)) | |
(defcomponent upload-form [text owner] | |
(init-state [_] | |
{:upload-queue (chan)}) | |
(did-mount [_] | |
(let [{:keys [upload-queue]} (om/get-state owner)] | |
(go (while true (let [upload (<! upload-queue) | |
resp (upload-file (<! upload))] | |
(.log js/console (<! resp))))))) | |
(render-state [this state] | |
(dom/form | |
(dom/input {:type "file" :name "file" :on-change #(queue-file % owner state)} | |
nil)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment