vsivsi / meteor-file-collection

Extends Meteor Collections to handle file data using MongoDB gridFS.
http://atmospherejs.com/vsivsi/file-collection
Other
159 stars 37 forks source link

Copy/Duplicate a GridFS Document #134

Closed CoolestNerdIII closed 8 years ago

CoolestNerdIII commented 8 years ago

Thank you so much for the awesome package. Quick question, which I hope isn't stupid and I am not overlooking something. Is there a simply way of making a copy or duplicate of a document?

vsivsi commented 8 years ago

On the server you can simply pipe the readable stream returned by fc.findOneStream() into the writable stream returned by fc.upsertStream().

See the node docs if you haven't piped streams together before: https://nodejs.org/dist/latest-v4.x/docs/api/stream.html#stream_readable_pipe_destination_options

CoolestNerdIII commented 8 years ago

Thanks a ton! Also thank you for the quick response!

CoolestNerdIII commented 8 years ago

Don't mean to be too much of a pain, but I think I am missing something (a bit new to streams). I implemented something along these lines, and in the db it appears to have the correct params, but I am unable to download the file. Any guidance would be greatly appreciated!

let script = fc.findOne(id);
let readable = fc.findOneStream({'_id': id});
let writable = fc.upsertStream({
    'filename': script.filename,
    'metadata': {'_auth': {'owner': script.metadata._auth.owner} }
});
readable.pipe(writable);

** Nevermind, I have a stupid error somewhere but that works. Thank you so much for your help!