Fork me on GitHub
#yada
<
2017-06-01
>
malcolmsparks08:06:02

That's a final draft ready for public review - I would you really appreciate any bits of feedback - if anyone want to take a bit of time to run through the examples that would be really great

dominicm10:06:10

Have you not tested the examples? For shame! 😉

malcolmsparks10:06:02

Yes I have. In Edge

maleghast10:06:51

I'll have a look now, @malcolmsparks

maleghast10:06:57

Reads well, seems very comprehensive. I haven't tried the examples (yet), clearly, but I will be referring back to this later in the week 😉

maleghast11:06:01

@malcolmsparks - I got my file upload working, but the start of the file that is written to the disk looks like this:

------WebKitFormBoundary7Svm17OQpKR5BTBx^M
Content-Disposition: form-data; name="upload-filename"^M
^M
gsod.csv^M
------WebKitFormBoundary7Svm17OQpKR5BTBx^M
Content-Disposition: form-data; name="rawdata-file-upload"; filename="GSOD_Data_PT_1992_to_2016_CDO8286807349283.csv"^M
Content-Type: text/csv^M
^M

maleghast11:06:57

Clearly I am getting something a bit wrong at the final hurdle... Not only is that ^^ happening, but I can't get the filename value from the form in the "ctx"

maleghast11:06:03

I've done a side-by-side between the example that you posted any my code and I admit I am defeated...

maleghast11:06:26

(I mean it looks as though it ought to work, so I can only assume that I am doing something dumb)

malcolmsparks11:06:27

What content type are you using?

maleghast11:06:28

The form has the enctype="multipart/form-data" attribute and the yada resource looks like this:

(defn ingest-rawdata-upload
  []
  (yada/resource
   {:id :edge.resources/ingest-rawdata-upload
    :description "Raw data Upload Resource"
    :produces [{:media-type #{"text/html" "application/json"}
                :charset "UTF-8"}]
    :methods
    {:get
     {:swagger/tags ["default" "getters"]
      :response
      (fn [ctx]
        (case (yada/content-type ctx)
                           "application/json" {:body "To upload POST to this endpoint instead with a body of appropriate JSON
                                                     metadata and a multi-part attachment of the data - see example"}
                           "text/html" (selmer/render-file
                                        "ingest-rawdata-upload.html"
                                        {:title "Cervest Ingest Platform - Raw Data Upload"
                                         :ctx ctx})))}
     :post
     {:swagger/tags ["default" "posters"]
      :parameters {:form {:upload-filename String}}
      :consumes #{"application/x-www-form-urlencoded"
                  "multipart/form-data"
                  "application/octet-stream"}
      :consumer (fn [ctx _ body-stream]
                  (println (get ctx :parameters)
                   )
                  (let [fname (get-in ctx [:parameters :form :upload-filename])
                        f (io/file (str (:external-files-path ingest-cfg) "/rawdata/gsod.csv"))]
                   (consume/save-to-file ctx body-stream f)))
       :response (fn [ctx]
                     (case (yada/content-type ctx)
                           "application/json" {:body "Upload complete"}
                           "text/html" (selmer/render-file
                                        "ingest-rawdata-upload-complete.html"
                                        {:title "Cervest Ingest Platform - Raw Data Upload"
                                         :ctx ctx})))}}}))

maleghast11:06:03

(I am pretty sure that the

:swagger/tags ["default" "posters"]
is bollocks too, been meaning to figure that out properly

maleghast11:06:28

I started out with just :application/octet-stream, but got loads of errors (HTTP 415 if memory serves)

maleghast11:06:16

Adding in "multipart/form-data" seemed to fix that, and I added "application/x-www-form-urlencoded" in the hope of getting the non-file form fields in the ctx

maleghast11:06:26

The idea was going to be that the user could supply a filename in a form field and then the "let" inside the consumer would go from this:

(let [fname (get-in ctx [:parameters :form :upload-filename])
                        f (io/file (str (:external-files-path ingest-cfg) "/rawdata/gsod.csv"))]
to this:
(let [fname (get-in ctx [:parameters :form :upload-filename])
                        f (io/file (str (:external-files-path ingest-cfg) fname))]

maleghast11:06:03

but I had to hard-code the filename, for now, because the value of the form-field is not in the ctx - i.e.

(get-in ctx [:parameters :form :upload-filename])
comes back with nil__