diff --git a/docs/gridfs-bucket-integration.md b/docs/gridfs-bucket-integration.md index 9013d8ac..5897ce8f 100644 --- a/docs/gridfs-bucket-integration.md +++ b/docs/gridfs-bucket-integration.md @@ -59,7 +59,7 @@ we also wrap this in a function: ```js import { MongoInternals } from 'meteor/mongo'; -export const createObjectId = ({ gridFsFileId }) => new MongoInternals.NpmModules.mongodb.module.ObjectID(gridFsFileId); +export const createObjectId = ({ gridFsFileId }) => new MongoInternals.NpmModules.mongodb.module.ObjectId(gridFsFileId); ``` ### 3. Create an upload handler for the bucket @@ -71,50 +71,46 @@ In order to stay flexible enough in the choice of the bucket we use a factory f import { Meteor } from 'meteor/meteor'; import fs from 'fs'; -export const createOnAfterUpload = (bucket) => { - return function onAfterUpload (file) { +export const createOnAfterUpload = bucket => + function onAfterUpload(file) { const self = this; - // here you could manipulate your file - // and create a new version, for example a scaled 'thumbnail' - // ... - - // then we read all versions we have got so far - Object.keys(file.versions).forEach((versionName) => { + // Process all versions of the uploaded file + Object.keys(file.versions).forEach(versionName => { const metadata = { ...file.meta, versionName, fileId: file._id }; - fs.createReadStream(file.versions[ versionName ].path) - - // this is where we upload the binary to the bucket using bucket.openUploadStream - // see http://mongodb.github.io/node-mongodb-native/3.6/api/GridFSBucket.html#openUploadStream - .pipe(bucket.openUploadStream(file.name, { - contentType: file.type || 'binary/octet-stream', - metadata - })) - - // and we unlink the file from the fs on any error - // that occurred during the upload to prevent zombie files - .on('error', err => { - console.error(err); - self.unlink(this.collection.findOne(file._id), versionName); // Unlink files from FS - }) - - // once we are finished, we attach the gridFS Object id on the - // FilesCollection document's meta section and finally unlink the - // upload file from the filesystem - .on('finish', Meteor.bindEnvironment(ver => { - const property = `versions.${versionName}.meta.gridFsFileId`; - - self.collection.update(file._id, { - $set: { - [ property ]: ver._id.toHexString(), + const uploadStream = bucket + .openUploadStream(file.name, { + contentType: file.type || 'binary/octet-stream', + metadata, + }) + .on('finish', async () => { + const property = `versions.${versionName}.meta.gridFsFileId` + + try { + await self.collection.updateAsync(file._id, { + $set: { + [property]: uploadStream.id.toHexString(), + }, + }) + await self.unlinkAsync(await this.collection.findOneAsync(file._id), versionName); + } catch (error) { + console.error(error); + await self.unlinkAsync(await this.collection.findOneAsync(file._id), versionName); } + }) + .on('error', async (err) => { + console.error(err); + await self.unlinkAsync(await this.collection.findOneAsync(file._id), versionName); }); - - self.unlink(this.collection.findOne(file._id), versionName); // Unlink files from FS - })); + const readStream = fs.createReadStream(file.versions[versionName].path).on('open', () => { + + + readStream.pipe( + uploadStream + ); + }); }); }; -}; ``` ### 4. Create download handler