martinklepsch / s3-beam

🚀 direct-to-S3 uploading using ClojureScript
Eclipse Public License 1.0
92 stars 17 forks source link

Reagent/Re-frame example? #8

Open rej156 opened 9 years ago

rej156 commented 9 years ago

What the title says, if plausible?

GetContented commented 8 years ago

I use Reagent in my app's front end currently, and I'm just at the point where I'm going to integrate s3-beam into my app, so I can probably provide an example for that sometime soon, with any luck :)

jdkealy commented 7 years ago

This is how i do it

REAGENT

(defn sign-and-upload [id file callback & [{:keys [file-name]}]]
  (let [params {:size (.-size file)
                :content/collection id
                :type (.-type file)
                :name (or file-name (.-name file))}]
    (POST "/signer" {:params params
                     :format :edn
                     :response-type :edn
                     :handler (fn [_params]
                                (let [params (assoc _params
                                                    "AWSAccessKeyId" util/access-key)
                                      params (doto
                                                 (reduce (fn [memo item]
                                                           (doto memo
                                                             (.append item (get params item)))) (js/FormData.) (keys params))
                                               (.append "file" file))
                                      xhr       (js/XMLHttpRequest.)]
                                  (-> xhr
                                      .-upload
                                      (.addEventListener "progress"
                                                         (fn [e]
                                                           (reset! loader [(.-loaded e)
                                                                           (.-total e)]))))
                                  (POST util/base-bucket {:params params
                                                          :api xhr
                                                          :response-format :raw
                                                          :handler (fn [e]
                                                                     (callback _params ))
                                                          :error-handler (fn [e]
                                                                           (.alert js/window "ERROR"))})))})))

clj

(POST "/signer" [] (fn [{:keys [edn-params user]}]
                       (let [fname (or (:name edn-params)
                                       "test")
                             newp {:metadata {:Content-Disposition "attachment"}
                                   :Content-Disposition "attachment"
                                   :file-name (str "uploads/"
                                                   (:db/id user)
                                                   "/"
                                                   (:content/collection edn-params)
                                                   "/"
                                                   (clojure.string/replace
                                                    fname
                                                    " "
                                                    "-"))
                                   :mime-type (:type edn-params)}]
                         (g/generate-json-response (sign-it newp)))))

(defn sign-it [newp]
  (dissoc
   (s3b/sign-upload
    newp
    {:bucket "secretbucket"
     :aws-key "itsasecret"
     :aws-zone "us-east-1"
     :aws-secret-key "secret-key"})
   :action
   :api_key))