veliovgroup / Meteor-Files

🚀 Upload files via DDP or HTTP to ☄️ Meteor server FS, AWS, GridFS, DropBox or Google Drive. Fast, secure and robust.
https://packosphere.com/ostrio/files
BSD 3-Clause "New" or "Revised" License
1.11k stars 166 forks source link

How to change item metadata before it is inserted into the collection? #867

Closed make-github-pseudonymous-again closed 1 year ago

make-github-pseudonymous-again commented 1 year ago

The API allows a user to send any arbitrary metadata together with the uploaded file. Is it possible to check/validate/modify this metadata on the server before it is inserted in the Mongo collection?

It seems when onBeforeUpload is called the file already has an _id so is it already inserted at that point?

An alternative is to use onAfterUpload, but that is too late, and will result in one insertion, and one update, instead of just one insertion.

dr-dimitru commented 1 year ago

Hello @make-github-pseudonymous-again,

onBeforeUpload is meant for this purpose. As stated in the docs after the first chunk is received. Intermediate collection used to store during upload

make-github-pseudonymous-again commented 1 year ago

Oh, great! How does one modify the metadata at this stage? Just by modifying the file object? Or do we need to make a query against this intermediate collection? Could onInitiateUpload be used instead? If using onBeforeUpload, should I check for this.chunkId == -1 && !this.eof to only act once?

dr-dimitru commented 1 year ago

@make-github-pseudonymous-again on this stage it's possible abort or continue upload, Altering meta-data should happen in onAfterUpload

make-github-pseudonymous-again commented 1 year ago

I understand. Is there a plan to make this possible in the future? To avoid having yet-another extra layer of "the upload is there but it's not ready for use yet".

make-github-pseudonymous-again commented 1 year ago

My point is that the upload action finishes before it's metadata is updated in onAfterUpload, so one would have to add extra logic to be synchronized with that event.

dr-dimitru commented 1 year ago

@make-github-pseudonymous-again

the upload is there but it's not ready for use yet

Not sure if I've got this one correctly. But it's how it's usually done:

  1. Wait for upload
  2. Wait for verification and processing
make-github-pseudonymous-again commented 1 year ago

OK. And you don't think this library should allow to abstract those two steps into a single one?

My solution without changing this library is to add an extra flag to each document that says whether certain things in onAfterUpload have run. Then I have to abstract away every Uploads.insert call to wait for this flag to be set, and every publication or method that involves those documents needs to filter documents based on this flag.

dr-dimitru commented 1 year ago

OK. And you don't think this library should allow to abstract those two steps into a single one?

No, that would be unnecessary overhead. This lib allows you to control these things.

My solution without changing this library is to add an extra flag to each document that says whether certain things in onAfterUpload have run. Then I have to abstract away every Uploads.insert call to wait for this flag to be set, and every publication or method that involves those documents needs to filter documents based on this flag.

@make-github-pseudonymous-again sounds correct, we were using one of two options:

  1. Add a flag after all post-processing finished; Read/filter from DB based using the flag;
  2. Insert into other collection or update an object in other collection
make-github-pseudonymous-again commented 1 year ago

OK. Feel free to close!