From 92567c7ec9aa0cc7906edc2714483ffc9544cbd2 Mon Sep 17 00:00:00 2001 From: Sebastian Bensusan Date: Sat, 23 May 2015 12:40:36 +0200 Subject: [PATCH 1/3] Added server-url option and goog.Uri to build the query string --- src/cljs/s3_beam/client.cljs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/cljs/s3_beam/client.cljs b/src/cljs/s3_beam/client.cljs index f47a1a1..310208b 100644 --- a/src/cljs/s3_beam/client.cljs +++ b/src/cljs/s3_beam/client.cljs @@ -1,4 +1,5 @@ (ns s3-beam.client + (:import (goog Uri)) (:require-macros [cljs.core.async.macros :refer [go]]) (:require [cljs.reader :as reader] [cljs.core.async :as async :refer [chan put! close! pipeline-async]] @@ -10,13 +11,16 @@ :type (.-type f) :size (.-size f)}) -(defn signing-url [fname fmime] - (str "/sign?file-name=" fname "&mime-type=" fmime)) +(defn signing-url [server-url fname fmime] + {:pre [(string? server-url) (string? fname) (string? fmime)]} + (.toString (doto (Uri. server-url) + (.setParameterValue "file-name" fname) + (.setParameterValue "mime-type" fmime)))) -(defn sign-file [file ch] +(defn sign-file [server-url file ch] (let [fmap (file->map file) edn-ize #(reader/read-string (.getResponseText (.-target %)))] - (xhr/send (signing-url (:name fmap) (:type fmap)) + (xhr/send (signing-url server-url (:name fmap) (:type fmap)) (fn [res] (put! ch {:f file :signature (edn-ize res)}) (close! ch))))) @@ -40,9 +44,15 @@ "POST" form-data))) -(defn s3-pipe [report-chan] - (let [to-process (chan) - signed (chan)] - (pipeline-async 3 signed sign-file to-process) - (pipeline-async 3 report-chan upload-file signed) - to-process)) +(defn s3-pipe + "Takes a channel where files to upload will be placed. + Returns a channel where it puts the files that were successfully uploaded. + May also take an optiosn map with: + :server-url - the sign server url, defaults to \"/sign\"" + ([report-chan] (s3-pipe report-chan {:server-url "/sign"})) + ([report-chan opts] + (let [to-process (chan) + signed (chan)] + (pipeline-async 3 signed (partial sign-file (:server-url opts)) to-process) + (pipeline-async 3 report-chan upload-file signed) + to-process))) From 5d1d8d15073b72e3e187daba0159aacbb2403eea Mon Sep 17 00:00:00 2001 From: Sebastian Bensusan Date: Sat, 23 May 2015 21:27:41 +0200 Subject: [PATCH 2/3] Added server-url option to the README --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d816bf2..d774649 100644 --- a/README.md +++ b/README.md @@ -44,14 +44,19 @@ Please follow Amazon's [official documentation](http://docs.aws.amazon.com/Amazo (GET "/sign" {params :params} (s3b/s3-sign bucket aws-zone access-key secret-key))) ``` -**NOTE: for now the only supported route to sign uploads is `/sign`. In a future -release this will be customizable.** +If you want to use a route different than `/sign`, define it in the +handler, `(GET "/my-cool-route" ...)`, and then pass it in the options +map to `s3-pipe` in the frontend. ### 3. Integrate the upload pipeline into your frontend -In your frontend code you can now use `s3-beam.client/s3-pipe`. `s3-pipe`'s argument is a channel -where completed uploads will be reported. The function returns a channel where you can put File objects -that should get uploaded. +In your frontend code you can now use `s3-beam.client/s3-pipe`. +`s3-pipe`'s argument is a channel where completed uploads will be +reported. The function returns a channel where you can put File +objects that should get uploaded. It might also take an extra options +map with the previously mentioned `:server-url` like so: + + (s3/s3-pipe uploaded {:server-url "/my-cool-route"}) An example using it within an Om component: From 4b335623620b13849b08b20daae74bbf4b60046d Mon Sep 17 00:00:00 2001 From: Sebastian Bensusan Date: Sat, 23 May 2015 21:30:43 +0200 Subject: [PATCH 3/3] Improved s3-pipe docstring --- src/cljs/s3_beam/client.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cljs/s3_beam/client.cljs b/src/cljs/s3_beam/client.cljs index 310208b..9e95e82 100644 --- a/src/cljs/s3_beam/client.cljs +++ b/src/cljs/s3_beam/client.cljs @@ -45,8 +45,8 @@ form-data))) (defn s3-pipe - "Takes a channel where files to upload will be placed. - Returns a channel where it puts the files that were successfully uploaded. + "Takes a channel where completed uploads will be reported and + returns a channel where you can put File objects that should get uploaded. May also take an optiosn map with: :server-url - the sign server url, defaults to \"/sign\"" ([report-chan] (s3-pipe report-chan {:server-url "/sign"}))