From 6646a23e73c193078b2a869156084a39af582b6a Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Wed, 2 May 2018 19:16:53 -0700 Subject: [PATCH 1/2] Clarify comments + add field handler to multipart sample --- functions/http/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/functions/http/index.js b/functions/http/index.js index 13a92e9b13..04eb2ebac9 100644 --- a/functions/http/index.js +++ b/functions/http/index.js @@ -148,20 +148,31 @@ exports.uploadFile = (req, res) => { if (req.method === 'POST') { const busboy = new Busboy({ headers: req.headers }); + // This object will accumulate all the fields, keyed by their name + const fields = {}; + // This object will accumulate all the uploaded files, keyed by their name. const uploads = {}; const tmpdir = os.tmpdir(); - // This callback will be invoked for each file uploaded. + // This event will be triggered for each non-file field in the form. + busboy.on('field', (fieldname, val) => { + // TODO(developer): Process submitted field values here + console.log(`Processed field ${fieldname}: ${val}.`); + fields[fieldname] = val; + }); + + // This event will be triggered for each file uploaded. busboy.on('file', (fieldname, file, filename) => { // Note: os.tmpdir() points to an in-memory file system on GCF // Thus, any files in it must fit in the instance's memory. + console.log(`Processed file ${filename}`); const filepath = path.join(tmpdir, filename); uploads[fieldname] = filepath; file.pipe(fs.createWriteStream(filepath)); }); - // This callback will be invoked after all uploaded files are saved. + // This event will be triggered after all uploaded files are saved. busboy.on('finish', () => { // TODO(developer): Process uploaded files here for (const name in uploads) { From ada23bbafa8cf344e67e4877e5c6ac51233abb5e Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Wed, 16 May 2018 11:34:55 -0700 Subject: [PATCH 2/2] Address comments --- functions/http/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/functions/http/index.js b/functions/http/index.js index 04eb2ebac9..67f5088a6c 100644 --- a/functions/http/index.js +++ b/functions/http/index.js @@ -147,22 +147,22 @@ const Busboy = require('busboy'); exports.uploadFile = (req, res) => { if (req.method === 'POST') { const busboy = new Busboy({ headers: req.headers }); + const tmpdir = os.tmpdir(); // This object will accumulate all the fields, keyed by their name const fields = {}; // This object will accumulate all the uploaded files, keyed by their name. const uploads = {}; - const tmpdir = os.tmpdir(); - // This event will be triggered for each non-file field in the form. + // This code will process each non-file field in the form. busboy.on('field', (fieldname, val) => { // TODO(developer): Process submitted field values here console.log(`Processed field ${fieldname}: ${val}.`); fields[fieldname] = val; }); - // This event will be triggered for each file uploaded. + // This code will process each file uploaded. busboy.on('file', (fieldname, file, filename) => { // Note: os.tmpdir() points to an in-memory file system on GCF // Thus, any files in it must fit in the instance's memory.