@@ -166,24 +166,42 @@ exports.uploadFile = (req, res) => {
166
166
fields [ fieldname ] = val ;
167
167
} ) ;
168
168
169
+ let fileWrites = [ ] ;
170
+
169
171
// This code will process each file uploaded.
170
172
busboy . on ( 'file' , ( fieldname , file , filename ) => {
171
173
// Note: os.tmpdir() points to an in-memory file system on GCF
172
174
// Thus, any files in it must fit in the instance's memory.
173
175
console . log ( `Processed file ${ filename } ` ) ;
174
176
const filepath = path . join ( tmpdir , filename ) ;
175
177
uploads [ fieldname ] = filepath ;
176
- file . pipe ( fs . createWriteStream ( filepath ) ) ;
178
+
179
+ const writeStream = fs . createWriteStream ( filepath ) ;
180
+ file . pipe ( writeStream ) ;
181
+
182
+ // File was processed by Busboy; wait for it to be written to disk.
183
+ const promise = new Promise ( ( resolve , reject ) => {
184
+ file . on ( 'end' , ( ) => {
185
+ writeStream . end ( ) ;
186
+ } ) ;
187
+ writeStream . on ( 'finish' , resolve ) ;
188
+ writeStream . on ( 'error' , reject ) ;
189
+ } ) ;
190
+ fileWrites . push ( promise ) ;
177
191
} ) ;
178
192
179
- // This event will be triggered after all uploaded files are saved.
193
+ // Triggered once all uploaded files are processed by Busboy.
194
+ // We still need to wait for the disk writes (saves) to complete.
180
195
busboy . on ( 'finish' , ( ) => {
181
- // TODO(developer): Process uploaded files here
182
- for ( const name in uploads ) {
183
- const file = uploads [ name ] ;
184
- fs . unlinkSync ( file ) ;
185
- }
186
- res . send ( ) ;
196
+ Promise . all ( fileWrites )
197
+ . then ( ( ) => {
198
+ // TODO(developer): Process saved files here
199
+ for ( const name in uploads ) {
200
+ const file = uploads [ name ] ;
201
+ fs . unlinkSync ( file ) ;
202
+ }
203
+ res . send ( ) ;
204
+ } ) ;
187
205
} ) ;
188
206
189
207
busboy . end ( req . rawBody ) ;
0 commit comments