mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-23 18:53:36 +02:00
Server: Improve parsing of uploaded content and error handling
This commit is contained in:
parent
99dbb97aa4
commit
db88dfc17d
@ -64,15 +64,19 @@ export async function formParse(request: IncomingMessage): Promise<FormParseResu
|
|||||||
|
|
||||||
if (req.__parsed) return req.__parsed;
|
if (req.__parsed) return req.__parsed;
|
||||||
|
|
||||||
const isFormContentType = req.headers['content-type'] === 'application/x-www-form-urlencoded';
|
const isFormContentType = req.headers['content-type'] === 'application/x-www-form-urlencoded' || req.headers['content-type'].startsWith('multipart/form-data');
|
||||||
|
|
||||||
// Note that for Formidable to work, the content-type must be set in the
|
// Note that for Formidable to work, the content-type must be set in the
|
||||||
// headers
|
// headers
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
||||||
return new Promise((resolve: Function, reject: Function) => {
|
return new Promise((resolve: Function, reject: Function) => {
|
||||||
const form = formidable({ multiples: true });
|
const form = formidable({
|
||||||
|
allowEmptyFiles: true,
|
||||||
|
minFileSize: 0,
|
||||||
|
});
|
||||||
form.parse(req, (error: any, fields: Fields, files: Files) => {
|
form.parse(req, (error: any, fields: Fields, files: Files) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
error.message = `Could not parse form: ${error.message}`;
|
||||||
reject(error);
|
reject(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -96,6 +100,11 @@ export async function bodyFields<T>(req: any/* , filter:string[] = null*/): Prom
|
|||||||
return form.fields as T;
|
return form.fields as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const bodyFiles = async <T>(req: any/* , filter:string[] = null*/): Promise<T> => {
|
||||||
|
const form = await formParse(req);
|
||||||
|
return form.files as T;
|
||||||
|
};
|
||||||
|
|
||||||
export function ownerRequired(ctx: AppContext) {
|
export function ownerRequired(ctx: AppContext) {
|
||||||
if (!ctx.joplin.owner) throw new ErrorForbidden();
|
if (!ctx.joplin.owner) throw new ErrorForbidden();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user