I have a node api that I'm building that's capable of handling uploads of multipart file types (chunks). This api is based on the Fastify library, and I've already installed the separate Fastify-Multipart library. I've got everything working, including mulitpart file uploads, but part of the requirements of this api is to be able to send requests to another api. In particular, I need to send the file uploads. I don't know what their api is written in, but their multipart file upload api is basically like this:
sdk.files.uploader(location_id, file_size, "filename.jpg", file)
.then(uploader => uploader.start())
.then(file => { console.log(file) })
My code is basically this:
post: async (request, reply) => {
// Check for file upload
if((request.headers['content-type'] && request.headers['content-type'].indexOf('multipart/form-data') !== -1) && request.params.id) {
const multipart = request.multipart(async (field, stream, filename, encoding, mimetype) => {
console.log(`folderId: ${request.params.id} filename: ${filename},
field name: ${field}, encoding: ${encoding},
mime type: ${mimetype}, file length: ${request.headers['content-length']}`)
try {
let uploader = await sdk.files.uploader(request.params.id, Number(request.headers['content-length']), filename, stream)
let file = await uploader.start()
console.log(file) //Never reaches this point
}
catch(e) {
console.log(`An error occurred during upload: ${e.message}`)
reply.code(500).send()
}
//pump(file, fs.createWriteStream(filename))
}, (error) => {
if(error) {
console.log(`Error uploading file: ${error.message}`)
reply.code(500).send()
} else {
console.log('File upload succeeded') //Upload succeeds but it's just in memory
reply.code(201).send()
}
})
multipart.on('field', (key, value) => {
console.log('form-data', key, value)
})
}
}
So basically what I want to do is pass a multipart file stream to this 3rd party api, but doing it this way doesn't seem to be working (when I go on their site, I don't see the file in the folder where it should be). When I look at Activity Monitor on my machine (macOS), I see the node process is consuming 1.2 Gig of memory (roughly the size of the file). Does anyone know a way to do this using Fastify-Multipart (which I believe is based on BusBoy).