Skip to content

Instantly share code, notes, and snippets.

@msukmanowsky
Last active January 10, 2023 03:50
Show Gist options
  • Save msukmanowsky/c8daf3720c2839d3c535afc69234ab9e to your computer and use it in GitHub Desktop.
Save msukmanowsky/c8daf3720c2839d3c535afc69234ab9e to your computer and use it in GitHub Desktop.
// middleware.js
exports.filesUpload = function(req, res, next) {
// See https://cloud.google.com/functions/docs/writing/http#multipart_data
const busboy = new Busboy({
headers: req.headers,
limits: {
// Cloud functions impose this restriction anyway
fileSize: 10 * 1024 * 1024,
}
});
const fields = {};
const files = [];
const fileWrites = [];
// Note: os.tmpdir() points to an in-memory file system on GCF
// Thus, any files in it must fit in the instance's memory.
const tmpdir = os.tmpdir();
busboy.on('field', (key, value) => {
// You could do additional deserialization logic here, values will just be
// strings
fields[key] = value;
});
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
const filepath = path.join(tmpdir, filename);
console.log(`Handling file upload field ${fieldname}: ${filename} (${filepath})`);
const writeStream = fs.createWriteStream(filepath);
file.pipe(writeStream);
fileWrites.push(new Promise((resolve, reject) => {
file.on('end', () => writeStream.end());
writeStream.on('finish', () => {
fs.readFile(filepath, (err, buffer) => {
const size = Buffer.byteLength(buffer);
console.log(`${filename} is ${size} bytes`);
if (err) {
return reject(err);
}
files.push({
fieldname,
originalname: filename,
encoding,
mimetype,
buffer,
size,
});
try {
fs.unlinkSync(filepath);
} catch (error) {
return reject(error);
}
resolve();
});
});
writeStream.on('error', reject);
}));
});
busboy.on('finish', () => {
Promise.all(fileWrites)
.then(() => {
req.body = fields;
req.files = files;
next();
})
.catch(next);
});
busboy.end(req.rawBody);
}
@VanSon999
Copy link

I have set 'fileSize: 500 * 1024' (500KB). But when I upload a file larger than 1MB in size it still happens normally! No error occurred! Please help!

@jayjaydluffy
Copy link

@msukmanowsky the next step (in your usage.js) to upload it to Cloud Storage will be via admin.storage() or firebase.storage()? Is the files in req.files of type File? i don't seem to find a way to upload File types via admin.storage() whereas I can do it with firebase.storage(), can't I? Please help. If you have gists on how you upload it after the middleware then pls share also. thanks

@jeanpierregomez
Copy link

@jayjaydluffy do you can solve this?

@AliKORKMAZ53
Copy link

i modified the code and here is working version for me:

`var busboy = new Busboy({ headers: req.headers });
const bucket = storage.bucket('buketdeneme');
let mimtype;
var saveTo;

busboy.on('file', function(name, file, filename, encoding, mimetype) {
  console.log('File [' + name + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
  const imageExtension = filename.split('.')[filename.split('.').length - 1];
  var fname=filename+'.'+imageExtension;
  saveTo = path.join(os.tmpdir(), filename);
  file.pipe(fs.createWriteStream(saveTo));
  mimtype=mimetype;
  
});

busboy.on('finish', async function() {
	await bucket.upload(saveTo, {
    resumable: false,
	gzip: true,
    metadata:{
		metadata:{
            contentType:mimtype
		}
		}
})
.then( () => {
    return res.json({message: "Image Uploaded Successfully"});
})
.catch(err => {
    console.error(err);
    return res.status(400).send(JSON.stringify(err, ["message", "arguments", "type", "name"]));
});
  
  res.end();
});
req.pipe(busboy);`

@babacarMbengue12
Copy link

thanks !!

@dungphanxuan
Copy link

Hi, can you show me solution for embed in code,
https://github.com/ThalKod/DropIt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment