vsivsi / meteor-file-collection

Extends Meteor Collections to handle file data using MongoDB gridFS.
http://atmospherejs.com/vsivsi/file-collection
Other
159 stars 37 forks source link

How to post a new file? #168

Closed iMagdy closed 6 years ago

iMagdy commented 6 years ago

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.

vsivsi commented 6 years ago

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.

iMagdy commented 6 years ago

Sound great. Thanks a lot.