thx ;)
I have converted it into a blob using js/fetch
(reg-event-fx
:upload-shot-video-server
(fn [coeffects [_ blob]]
(let [body (js/FormData.)]
(.append body "video" blob)
{:http-xhrio {:method :post
:uri (str "<http://d18a6571c2e5.ngrok.io>" "/api/upload-shot-video")
:body body
:on-success [:upload-success]
:on-failure [:upload-error]
:response-format (edn/edn-response-format)}}))
)
(reg-event-fx
:upload-shot-video
(fn [coeffects _]
(prn "uploading video")
(let [response (js/fetch (-> coeffects :db :shot-video-uri))]
(try
(go
(let [blob (<p! (. (<p! response) blob))]
(js/console.log "blob is " blob)
(dispatch [:upload-shot-video-server blob])))
(catch js/Error e (js/console.log "Error is " e)))
{})))
And the handler is as follows:
(defn upload-shot-video [req]
(prn "uploading video")
(prn "video is! " (-> req :params))
(prn "video is " (-> req :body))
(<http://clojure.java.io/copy|clojure.java.io/copy> (-> req :body) (<http://clojure.java.io/file|clojure.java.io/file> "./resources/public/video.mov"))
(let [filename (str (rand-str 100) ".mov")]
(s3/put-object
:bucket-name "humboi-videos"
:key filename
:file "./resources/public/video.mov"
:access-control-list {:grant-permission ["AllUsers" "Read"]})
(db/add-video {:name (-> req :params :name)
:uri (str "<https://humboi-videos.s3-us-west-1.amazonaws.com/>" filename)}))
(r/response {:res "okay!"}))
But the saved file is still 0 bytes
One small issue - don't call dispatch
in event handler. Use effect handlers for that. Move all promise machinery into an effect handler.
But that's not what causing you problems.
(js/console.log "blob is " blob)
- does this print out a blob of the correct non-zero size?
it prints ellipses for everything Blob related
Those ellipses are usually clickable.
not in this case
also how can the dispatch be in an effect handler if it has to be called in a go block?
if it has to be called inside a go block, go returns nil, which means that the effect can’t be specified in the re-frame map
Try logging (.-size blob)
then.
Move the whole go
block into an effect handler.
yes the size is showing as 4462885
which means that the blob is correct
In that case I have no idea, sorry. Maybe it's something about React Native, maybe I missed something, maybe you haven't shown something that's important. Or maybe it's something else altogether. The next step would be to confirm that the data is sent to the backend. The Network tab in the DevTools should help with that. If the data is not sent, then I have no clue what's going on. If the data is sent, then it's something on your backend.
there are absolutely no new entries showing up in the network tab
during the request
what’s the best way to debug this?
No idea. Especially given that you do receive the request on your backend.
another strange thing that happens is that the (-> req :body) only contains a single input stream even when I add multiple key value pairs with (.append …)
if the data is multipart, then why only a single input stream in body?
and why no data in (-> req :params) after adding the wrap-multipart-params middleware?
after changing to
(.append body "video" blob "video.mov")
from
(.append body "video" blob)
I get the error:
unrecognized request format nil
How to fix this?
the server is definitely receiving the request. After adding the request format as (json-request-format), I am seeing the following on (-> req :params)
{:parts [[“video” {:data {:collector nil, :offset 0, :name “D7A13384-801E-4D72-8912-9FA967BD61AB.mov”, :type “video/quicktime”, :size 1286383, :blobId “17FC5EC0-7B3E-4F9E-A666-0A46AD771FD2"}}]]}