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

[3.0.0-beta.6] fileObj._id on upload end ? #889

Open ddaydd opened 4 months ago

ddaydd commented 4 months ago

Hello,

with the 3.0.0-beta.6 version, how to retrieve the id?

      upload.on('end', function (error, fileObj) {
          console.log(fileObj._id); // undefined
    }

Thanks.

dr-dimitru commented 3 months ago

@bratelefant @jankapunkt @harryadel do we have bug in beta/rc release?

dr-dimitru commented 2 months ago

@ddaydd have you found a solution?

ddaydd commented 2 months ago

no, and I saw that findOne became findOneAsync but no insertAsync?

dr-dimitru commented 2 months ago

@ddaydd .insert is client only and only mimics .insert method of mongodb driver

ddaydd commented 2 months ago

thank you for motivating me... I found the _id

...
let fileId;
upload.on('start', function () {
  fileId = this.config.fileId
});
...
dr-dimitru commented 2 months ago

@ddaydd I'm sure it's available via this inside end event as well

ddaydd commented 2 months ago

Yes, i confirm, but I leave it open, this must at least be changed or documented. thanks.

donstephan commented 1 month ago

I'm seeing this on the client in the upload.on('end') callback. The resulting fileObj doesn't include a _id like it used to. Anyone else experiencing this?

Using ostrio:files@3.0.0-beta.6

donstephan commented 1 month ago

Seems you need to access the fileId through the uploadInstance config element in ostrio:files@3.0.0-beta.6

For example:

var uploadInstance = Images.insert({
  file: file,
  chunkSize: 'dynamic'
}, false);

uploadInstance.on('end', function () {
  console.log('File _id: ', uploadInstance.config.fileId);
});

uploadInstance.start();
dr-dimitru commented 1 month ago

@donstephan

  1. Note: Do not rely on .fileId as to _id of the file as they may differ for obfuscation purposes (built-into this package)
  2. Have you checked file's details on this context?
donstephan commented 1 month ago

~In the example I shared above there's no this context relevant to the file upload. This is strictly client side. The resulting fileObj doesn't have any _id on it like it used to.~

The resulting this context doesn't contain a fileId. this.config.fileId is the only place a reference the fileId exists.

To clarify, even though I'm listening to the 'end' event, the fileId in the uploadInstance potentially isn't the resulting _id of the file per obfuscation (which is fair).

Is this related to this comment in the 3.0 PR? https://github.com/veliovgroup/Meteor-Files/pull/884#discussion_r1612953210

I can share a test if needed.

bratelefant commented 1 month ago

Ok, when I eventually started moving my full app to 3.0.1 I also can confirm this is a breaking change. I don't get a proper fileRef in the "uploaded" event listener callback; worked fine in 2.16. Trying to figure this out...

timsun28 commented 1 month ago

I'm having the same issue after updating to meteor 3.0 and using v3.0.0-beta.6. I also found that the onUploaded hook doesn't work properly anymore. It doesn't return an error/fileObj and this is undefined and it also runs instantly after submitting. I'm not sure if this is intended, as it isn't very clear what is new in v3.

Running the following code:

const upload = ElementPhotos.insert(photoObject, false);
upload.on("end", function () {
    console.log({ thisInstance: this });
    console.log("File _id: ", upload.config.fileId);
});

The "this" instance contains some details about the file upload and the upload.config.fileId is a random string, but as dr-dimitru pointed out, this shouldn't be used for the required usage.

I also needed to update my s3 code to fix some issues, now that the collections are also using async functions like findOneAsync, updateAsync and removeAsync. This will also need to be updated in the types, or preferably have the types be added to the package so it wouldn't be necessary to update types manually.

bratelefant commented 1 month ago

Ok just pushed https://github.com/veliovgroup/Meteor-Files/pull/884/commits/d57374198218b178f4a88800fdf8a0dd8c4cc7f9 This should return the fileObj after it was inserted in the db on "end" and "uploaded" events on the client. Feel free to test and comment

timsun28 commented 4 weeks ago

Just installed the package locally with your latest changes and can confirm that this fixed the issue. It now returns the expected object in both the end callback and the onUploaded that includes the _id of the newly inserted file/image and more.

Thank you for your support and hard work on upgrading this package!