Meteor-Files
Meteor-Files copied to clipboard
Feature request: clone() to clone a record
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.
+1 A copy / duplicate function would be very useful so it can either straight up copy or have an option to rename.
Is there any workaround for now? I.e. how to duplicate existing files in FielsCollection
now?
@fracz at the moment you would need to:
- Read MongoDB record of the existing file
- Write the same record to MongoDB omitting
_id
field - After new record inserted into collection grab newly created entry
_id
- Replace old
_id
inpath
,versions.original.path
, and in custommeta
fields (if any) -
.copyFile()
to actually duplicate a file usingpath
from the old and new mongodb entries
Hope that helps
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 lgtm, don't forget to pass a callback to .write
method to make sure you have controls over potential errors and exceptions