Skip to content

Commit 1ed12d1

Browse files
authored
Merge pull request #900 from harryadel/update-document
Update GridFS exmaple code
2 parents e502dc0 + 3384ff4 commit 1ed12d1

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

docs/gridfs-bucket-integration.md

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ we also wrap this in a function:
5959
```js
6060
import { MongoInternals } from 'meteor/mongo';
6161

62-
export const createObjectId = ({ gridFsFileId }) => new MongoInternals.NpmModules.mongodb.module.ObjectID(gridFsFileId);
62+
export const createObjectId = ({ gridFsFileId }) => new MongoInternals.NpmModules.mongodb.module.ObjectId(gridFsFileId);
6363
```
6464

6565
### 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
7171
import { Meteor } from 'meteor/meteor';
7272
import fs from 'fs';
7373

74-
export const createOnAfterUpload = (bucket) => {
75-
return function onAfterUpload (file) {
74+
export const createOnAfterUpload = bucket =>
75+
function onAfterUpload(file) {
7676
const self = this;
7777

78-
// here you could manipulate your file
79-
// and create a new version, for example a scaled 'thumbnail'
80-
// ...
81-
82-
// then we read all versions we have got so far
83-
Object.keys(file.versions).forEach((versionName) => {
78+
// Process all versions of the uploaded file
79+
Object.keys(file.versions).forEach(versionName => {
8480
const metadata = { ...file.meta, versionName, fileId: file._id };
85-
fs.createReadStream(file.versions[ versionName ].path)
86-
87-
// this is where we upload the binary to the bucket using bucket.openUploadStream
88-
// see http://mongodb.github.io/node-mongodb-native/3.6/api/GridFSBucket.html#openUploadStream
89-
.pipe(bucket.openUploadStream(file.name, {
90-
contentType: file.type || 'binary/octet-stream',
91-
metadata
92-
}))
93-
94-
// and we unlink the file from the fs on any error
95-
// that occurred during the upload to prevent zombie files
96-
.on('error', err => {
97-
console.error(err);
98-
self.unlink(this.collection.findOne(file._id), versionName); // Unlink files from FS
99-
})
100-
101-
// once we are finished, we attach the gridFS Object id on the
102-
// FilesCollection document's meta section and finally unlink the
103-
// upload file from the filesystem
104-
.on('finish', Meteor.bindEnvironment(ver => {
105-
const property = `versions.${versionName}.meta.gridFsFileId`;
106-
107-
self.collection.update(file._id, {
108-
$set: {
109-
[ property ]: ver._id.toHexString(),
81+
const uploadStream = bucket
82+
.openUploadStream(file.name, {
83+
contentType: file.type || 'binary/octet-stream',
84+
metadata,
85+
})
86+
.on('finish', async () => {
87+
const property = `versions.${versionName}.meta.gridFsFileId`
88+
89+
try {
90+
await self.collection.updateAsync(file._id, {
91+
$set: {
92+
[property]: uploadStream.id.toHexString(),
93+
},
94+
})
95+
await self.unlinkAsync(await this.collection.findOneAsync(file._id), versionName);
96+
} catch (error) {
97+
console.error(error);
98+
await self.unlinkAsync(await this.collection.findOneAsync(file._id), versionName);
11099
}
100+
})
101+
.on('error', async (err) => {
102+
console.error(err);
103+
await self.unlinkAsync(await this.collection.findOneAsync(file._id), versionName);
111104
});
112-
113-
self.unlink(this.collection.findOne(file._id), versionName); // Unlink files from FS
114-
}));
105+
const readStream = fs.createReadStream(file.versions[versionName].path).on('open', () => {
106+
107+
108+
readStream.pipe(
109+
uploadStream
110+
);
111+
});
115112
});
116113
};
117-
};
118114
```
119115

120116
### 4. Create download handler

0 commit comments

Comments
 (0)