How to post a new file?
iMagdy opened this issue · 2 comments
Greetings Vaughn,
I've seen this example in the README on how to POST
on an existing file:
// POST data to a file based on _id and a secret value stored as metadata
// where the secret is supplied as a MIME/Multipart parameter
{ method: 'post',
path: '/post/:_id',
lookup: function (params, query, multipart) {
return { _id: params._id, "metadata.secret": multipart.params.secret} }}
But how to post a new file via HTTP?
Thank you.
Hi, this is at least partially explained in the security section:
https://github.com/vsivsi/meteor-file-collection#security
You have to insert
an empty file somehow. If the client is Meteor, you can use fc.insert()
on the client itself, paired with an appropriate insert allow/deny rule.
You can see that here, for example:
https://github.com/vsivsi/meteor-file-job-sample-app/blob/master/sample.coffee#L95-L111
If you wish to do it purely with HTTP, you can implement the insert
as part of the handling, for example by adding it to the lookup
function (assuming reasonable validation of the POST request).
So for example, something like this:
// POST data to a file based on _id and a secret value stored as metadata
// where the secret is supplied as a MIME/Multipart parameter
{ method: 'post',
path: '/post/:_id',
lookup: function (params, query, multipart) {
// Multi-part parameters before are "for example". You define these...
fc.insert({ _id: params._id,
filename: multipart.params.filename,
contentType: multipart.params.filetype});
// Obviously you'll need to handle any errors from above insert...
return { _id: params._id, "metadata.secret": multipart.params.secret}
}}
Hope that helps.
Sound great. Thanks a lot.