Skip to content

Commit

Permalink
Merge pull request #5 from bensu/master
Browse files Browse the repository at this point in the history
Add custom sign files route
  • Loading branch information
Martin Klepsch committed May 25, 2015
2 parents 6d2cefb + 4b33562 commit 1cb9b27
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
30 changes: 20 additions & 10 deletions src/cljs/s3_beam/client.cljs
Original file line number Diff line number Diff line change
@@ -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]]
Expand All @@ -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)))))
Expand All @@ -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 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"}))
([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)))

0 comments on commit 1cb9b27

Please sign in to comment.