From c2b7b7508f6ed1ce82fcc5ad03a3ef17f33db138 Mon Sep 17 00:00:00 2001 From: harryadel Date: Fri, 28 Mar 2025 20:49:16 +0200 Subject: [PATCH 1/2] Update GridFS exmaple code --- docs/gridfs-bucket-integration.md | 65 ++++++++++++++++--------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/docs/gridfs-bucket-integration.md b/docs/gridfs-bucket-integration.md index 9013d8ac..e96d3aac 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 @@ -82,38 +82,39 @@ export const createOnAfterUpload = (bucket) => { // then we read all versions we have got so far 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 + const readStream = fs.createReadStream(file.versions[versionName].path).on('open', () => { + const uploadStream = bucket + .openUploadStream(file.name, { + contentType: file.type || 'binary/octet-stream', + metadata, + }) + + // this is where we upload the binary to the bucket + + readStream.pipe( + uploadStream + .on('error', (err) => { + console.error(err); + self.unlink(await this.collection.findOneAsync(file._id), versionName); + }) + .on('finish', async () => { + const property = `versions.${versionName}.meta.gridFsFileId` + try { + await self.collection.updateAsync(file._id, { + $set: { + [property]: uploadStream.id.toHexString(), + }, + }) + self.unlink(await this.collection.findOneAsync(file._id), versionName); + } catch (error) { + console.error(error); + } + }) + }) }) - - // 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(), - } - }); - - self.unlink(this.collection.findOne(file._id), versionName); // Unlink files from FS - })); - }); - }; + }) + }) + }, }; ``` From 3384ff4432da62ee2f592759222cfa7b926373ba Mon Sep 17 00:00:00 2001 From: harryadel Date: Wed, 2 Apr 2025 18:22:49 +0200 Subject: [PATCH 2/2] Apply the doctor prescription --- docs/gridfs-bucket-integration.md | 69 ++++++++++++++----------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/docs/gridfs-bucket-integration.md b/docs/gridfs-bucket-integration.md index e96d3aac..5897ce8f 100644 --- a/docs/gridfs-bucket-integration.md +++ b/docs/gridfs-bucket-integration.md @@ -71,51 +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 }; - const readStream = fs.createReadStream(file.versions[versionName].path).on('open', () => { - const uploadStream = bucket + const uploadStream = bucket .openUploadStream(file.name, { contentType: file.type || 'binary/octet-stream', metadata, }) - - // this is where we upload the binary to the bucket - - readStream.pipe( - uploadStream - .on('error', (err) => { - console.error(err); - self.unlink(await this.collection.findOneAsync(file._id), versionName); - }) - .on('finish', async () => { - const property = `versions.${versionName}.meta.gridFsFileId` - try { - await self.collection.updateAsync(file._id, { - $set: { - [property]: uploadStream.id.toHexString(), - }, - }) - self.unlink(await this.collection.findOneAsync(file._id), versionName); - } catch (error) { - console.error(error); - } - }) + .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); + }); + const readStream = fs.createReadStream(file.versions[versionName].path).on('open', () => { + + + readStream.pipe( + uploadStream + ); + }); + }); + }; ``` ### 4. Create download handler