2021-06-24 10:09:10 +02:00
|
|
|
import { createWriteStream } from "fs";
|
|
|
|
import { join, dirname } from "path";
|
|
|
|
import Busboy from "busboy";
|
|
|
|
import mkdirp from "mkdirp";
|
2020-10-01 14:11:34 +02:00
|
|
|
|
|
|
|
function formData(
|
2021-06-24 10:09:10 +02:00
|
|
|
contType: string,
|
|
|
|
options: busboy.BusboyConfig & {
|
|
|
|
abortOnLimit?: boolean;
|
|
|
|
tmpDir?: string;
|
|
|
|
onFile?: (
|
|
|
|
fieldname: string,
|
|
|
|
file: NodeJS.ReadableStream,
|
|
|
|
filename: string,
|
|
|
|
encoding: string,
|
|
|
|
mimetype: string
|
|
|
|
) => string;
|
|
|
|
onField?: (fieldname: string, value: any) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
filename?: (oldName: string) => string;
|
|
|
|
} = {}
|
2020-10-01 14:11:34 +02:00
|
|
|
) {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.log("Enter form data");
|
|
|
|
options.headers = {
|
|
|
|
"content-type": contType,
|
|
|
|
};
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const busb = new Busboy(options);
|
|
|
|
const ret = {};
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
this.bodyStream().pipe(busb);
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
busb.on("limit", () => {
|
|
|
|
if (options.abortOnLimit) {
|
|
|
|
reject(Error("limit"));
|
|
|
|
}
|
|
|
|
});
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
busb.on("file", function (fieldname, file, filename, encoding, mimetype) {
|
|
|
|
const value: { filePath: string | undefined; filename: string; encoding: string; mimetype: string } = {
|
|
|
|
filename,
|
|
|
|
encoding,
|
|
|
|
mimetype,
|
|
|
|
filePath: undefined,
|
|
|
|
};
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof options.tmpDir === "string") {
|
|
|
|
if (typeof options.filename === "function") filename = options.filename(filename);
|
|
|
|
const fileToSave = join(options.tmpDir, filename);
|
|
|
|
mkdirp(dirname(fileToSave));
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
file.pipe(createWriteStream(fileToSave));
|
|
|
|
value.filePath = fileToSave;
|
|
|
|
}
|
|
|
|
if (typeof options.onFile === "function") {
|
|
|
|
value.filePath = options.onFile(fieldname, file, filename, encoding, mimetype) || value.filePath;
|
|
|
|
}
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
setRetValue(ret, fieldname, value);
|
|
|
|
});
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
busb.on("field", function (fieldname, value) {
|
|
|
|
if (typeof options.onField === "function") options.onField(fieldname, value);
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
setRetValue(ret, fieldname, value);
|
|
|
|
});
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
busb.on("finish", function () {
|
|
|
|
resolve(ret);
|
|
|
|
});
|
2020-10-01 14:11:34 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
busb.on("error", reject);
|
|
|
|
});
|
2020-10-01 14:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setRetValue(
|
2021-06-24 10:09:10 +02:00
|
|
|
ret: { [x: string]: any }, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
fieldname: string,
|
|
|
|
value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any // eslint-disable-line @typescript-eslint/no-explicit-any
|
2020-10-01 14:11:34 +02:00
|
|
|
) {
|
2021-06-24 10:09:10 +02:00
|
|
|
if (fieldname.endsWith("[]")) {
|
|
|
|
fieldname = fieldname.slice(0, fieldname.length - 2);
|
|
|
|
if (Array.isArray(ret[fieldname])) {
|
|
|
|
ret[fieldname].push(value);
|
|
|
|
} else {
|
|
|
|
ret[fieldname] = [value];
|
|
|
|
}
|
2020-10-01 14:11:34 +02:00
|
|
|
} else {
|
2021-06-24 10:09:10 +02:00
|
|
|
if (Array.isArray(ret[fieldname])) {
|
|
|
|
ret[fieldname].push(value);
|
|
|
|
} else if (ret[fieldname]) {
|
|
|
|
ret[fieldname] = [ret[fieldname], value];
|
|
|
|
} else {
|
|
|
|
ret[fieldname] = value;
|
|
|
|
}
|
2020-10-01 14:11:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default formData;
|