diff --git a/functions/http/index.js b/functions/http/index.js index 13a92e9b13..67f5088a6c 100644 --- a/functions/http/index.js +++ b/functions/http/index.js @@ -147,21 +147,32 @@ 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 callback will be invoked for each file uploaded. + // 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 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. + 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) {