Meteor-files: onAfterRemove didn't get trigger

Created on 26 Jan 2017  路  11Comments  路  Source: veliovgroup/Meteor-Files

i follow the grid fs tutorial but i notice that onAfterRemove didn't get call after i use Images.remove({}) all the grid fs file is still there, i have to manually called it in a method

question

All 11 comments

Hello @lastday4you ,

Show some of your code.
And how do you remove files. Is there a chance cursor (selection) is empty?

here is the collection code

export const Images = new FilesCollection({
  collectionName: 'images',
  allowClientCode: true,
  debug: Meteor.isServer && process.env.NODE_ENV === 'development',
  onBeforeUpload(file) {
    if (file.size <= 10 * 1024 * 1024 && /png|jpg|jpeg/i.test(file.extension)) return true;
    return 'Please upload image, with size equal or less than 3MB';
  },
  onAfterUpload(image) {
    // Move file to GridFS
    Object.keys(image.versions).forEach(versionName => {
      const metadata = {versionName, imageId: image._id, storedAt: new Date()}; // Optional
      const writeStream = gfs.createWriteStream({filename: image.name, metadata});

      fs.createReadStream(image.versions[versionName].path).pipe(writeStream);

      writeStream.on('close', Meteor.bindEnvironment(file => {
        const property = `versions.${versionName}.meta.gridFsFileId`;

        // If we store the ObjectID itself, Meteor (EJSON?) seems to convert it to a
        // LocalCollection.ObjectID, which GFS doesn't understand.
        this.collection.update(image._id, {$set: {[property]: file._id.toString()}});
        this.unlink(this.collection.findOne(image._id), versionName); // Unlink files from FS
      }));
    });
  },
  interceptDownload(http, image, versionName) {
    // Serve file from GridFS
    const _id = (image.versions[versionName].meta || {}).gridFsFileId;
    if (_id) {
      const readStream = gfs.createReadStream({_id});
      readStream.on('error', err => {
        throw err;
      });
      readStream.pipe(http.response);
    }
    return Boolean(_id); // Serve file from either GridFS or FS if it wasn't uploaded yet
  },
  onAfterRemove(images) {
    // Remove corresponding file from GridFS
    images.forEach(image => {
      console.log(image + "remove image");
      Object.keys(image.versions).forEach(versionName => {
        const _id = (image.versions[versionName].meta || {}).gridFsFileId;
        if (_id) gfs.remove({_id}, err => {
          if (err) throw err;
        });
      });
    });
  }
});

and here is my remove from the hook that called both server and client

Status.before.remove(function (userId, doc) {
  if (doc.image_id != null){
    Images.remove({_id: doc.image_id[0]});
  }
});

the file in the images collection is gone but the gridfs and chuck file is still there
and thank for the fast reply :)

the work around at the moment for me is to copy the onAfterRemove code in to another method and call that method in the hook instead.

Is this server code:?

Status.before.remove(function (userId, doc) {
  if (doc.image_id != null){
    Images.remove({_id: doc.image_id[0]});
  }
});

Is onBeforeRemove called?

it is in the lib folder called both client and server, tried onBeforeRemove too it didn't call too

Make sure you call Images.remove() on server only.

If onBeforeRemove and onAfterRemove both are not called - there is something definitely wrong on your end.

What versions you're on (Meteor, Meteor-Files, OS)?

im using macOS, ostrio:[email protected], and [email protected] wait let me try to call Images.remove() on server only

ostrio:[email protected] is very outdated
[email protected] are you sure? 1.4.2 is latest stable

sorry was copying out of versions file got one wrong

omg thank you it was the version problem, i guess when i add through meteor add it pull the 1.5.6 instead

I'm glad it's solved.

Please, support this project by:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JanSchuermannPH picture JanSchuermannPH  路  3Comments

rlhk picture rlhk  路  4Comments

sylido picture sylido  路  3Comments

sidkdbl07 picture sidkdbl07  路  4Comments

schlaegerz picture schlaegerz  路  3Comments