Meteor-Files icon indicating copy to clipboard operation
Meteor-Files copied to clipboard

Feature request: clone() to clone a record

Open urakagi opened this issue 4 years ago • 5 comments

A method for FileCursor instance or Collection.clone(<_id>) will be good for applications that have duplication needs.

For example, wekan can copy cards, so when I copy a card the corresponded attachments also need to be copied. In this case, a clone method will be convenient.

It will also be good if we can pass an option that defines something that will not be cloned but use the passed parameters instead.

urakagi avatar May 13 '20 01:05 urakagi

+1 A copy / duplicate function would be very useful so it can either straight up copy or have an option to rename.

tomtom87 avatar Jun 03 '20 04:06 tomtom87

Is there any workaround for now? I.e. how to duplicate existing files in FielsCollection now?

fracz avatar Mar 27 '22 19:03 fracz

@fracz at the moment you would need to:

  1. Read MongoDB record of the existing file
  2. Write the same record to MongoDB omitting _id field
  3. After new record inserted into collection grab newly created entry _id
  4. Replace old _id in path, versions.original.path, and in custom meta fields (if any)
  5. .copyFile() to actually duplicate a file using path from the old and new mongodb entries

Hope that helps

dr-dimitru avatar Mar 27 '22 19:03 dr-dimitru

Ended up with the following code for duplicating certain files. Could you take a look, please?

// collection definition
const Attachments = new FilesCollection({...}).

// inside some meteor method:
if (Meteor.isServer) {
  Attachments.find(someFilters).forEach((attachment) => {
    const buffer = fs.readFileSync(attachment.path);
    Attachments.write(buffer, {
        fileName: attachment.name,
        type: attachment.type,
        size: attachment.size,
        userId: this.userId,
        meta: attachment.meta,
    });
  });
}

fracz avatar Mar 27 '22 20:03 fracz

@fracz lgtm, don't forget to pass a callback to .write method to make sure you have controls over potential errors and exceptions

dr-dimitru avatar Mar 27 '22 20:03 dr-dimitru