feat: allow querystring + single file body uploads for editing tracks

This commit is contained in:
Paul Bienkowski 2020-11-21 16:18:21 +01:00
parent 3c1d666dc3
commit b63afb1146

View file

@ -141,14 +141,7 @@ router.get(
}), }),
); );
async function getMultipartOrJsonBody(req, mapJsonBody = (x) => x) { async function readFile(file) {
const fileInfo = {};
let body;
if (req.busboy) {
body = {};
req.busboy.on('file', async function (fieldname, file, filename, encoding, mimetype) {
let fileContent = ''; let fileContent = '';
file.on('data', function (data) { file.on('data', function (data) {
@ -160,7 +153,18 @@ async function getMultipartOrJsonBody(req, mapJsonBody = (x) => x) {
file.on('error', reject); file.on('error', reject);
}); });
body[fieldname] = fileContent; return fileContent;
}
async function getMultipartOrJsonBody(req, mapJsonBody = (x) => x) {
const fileInfo = {};
let body;
if (req.busboy) {
body = {};
req.busboy.on('file', async function (fieldname, file, filename, encoding, mimetype) {
body[fieldname] = await readFile(file);
fileInfo[fieldname] = { filename, encoding, mimetype }; fileInfo[fieldname] = { filename, encoding, mimetype };
}); });
@ -174,8 +178,15 @@ async function getMultipartOrJsonBody(req, mapJsonBody = (x) => x) {
req.busboy.on('finish', resolve); req.busboy.on('finish', resolve);
req.busboy.on('error', reject); req.busboy.on('error', reject);
}); });
} else { } else if (req.headers['content-type'] === 'application/json') {
body = mapJsonBody(req.body); body = mapJsonBody(req.body);
} else {
body = { body: await readFile(req), ...req.query };
fileInfo.body = {
mimetype: req.headers['content-type'],
filename: req.headers['content-disposition'],
encoding: req.headers['content-encoding'],
};
} }
return { body, fileInfo }; return { body, fileInfo };