@@ -59,7 +59,7 @@ we also wrap this in a function:
59
59
``` js
60
60
import { MongoInternals } from ' meteor/mongo' ;
61
61
62
- export const createObjectId = ({ gridFsFileId }) => new MongoInternals.NpmModules.mongodb.module.ObjectID (gridFsFileId);
62
+ export const createObjectId = ({ gridFsFileId }) => new MongoInternals.NpmModules.mongodb.module.ObjectId (gridFsFileId);
63
63
```
64
64
65
65
### 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
71
71
import { Meteor } from ' meteor/meteor' ;
72
72
import fs from ' fs' ;
73
73
74
- export const createOnAfterUpload = ( bucket ) => {
75
- return function onAfterUpload (file ) {
74
+ export const createOnAfterUpload = bucket =>
75
+ function onAfterUpload (file ) {
76
76
const self = this ;
77
77
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 => {
84
80
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);
110
99
}
100
+ })
101
+ .on (' error' , async (err ) => {
102
+ console .error (err);
103
+ await self .unlinkAsync (await this .collection .findOneAsync (file ._id ), versionName);
111
104
});
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
+ });
115
112
});
116
113
};
117
- };
118
114
```
119
115
120
116
### 4. Create download handler
0 commit comments