-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathFormData.ts
More file actions
39 lines (33 loc) · 1.39 KB
/
FormData.ts
File metadata and controls
39 lines (33 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import path from 'node:path';
import _FormData from 'form-data';
// eslint-disable-next-line no-control-regex
const NON_ASCII_RE = /[^\u0000-\u007F]/i;
export class FormData extends _FormData {
_getContentDisposition(value: any, options: any): string | undefined {
// support non-ascii filename
// https://github.com/form-data/form-data/pull/571
let filename;
let contentDisposition;
if (typeof options.filepath === 'string') {
// custom filepath for relative paths
filename = path.normalize(options.filepath).replaceAll('\\', '/');
} else if (options.filename || value.name || value.path) {
// custom filename take precedence
// formidable and the browser add a name property
// fs- and request- streams have path property
filename = path.basename(options.filename || value.name || value.path);
} else if (value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
// or try http response
filename = path.basename(value.client._httpMessage.path || '');
}
if (filename) {
// https://datatracker.ietf.org/doc/html/rfc6266#section-4.1
// support non-ascii filename
contentDisposition = 'filename="' + filename + '"';
if (NON_ASCII_RE.test(filename)) {
contentDisposition += "; filename*=UTF-8''" + encodeURIComponent(filename);
}
}
return contentDisposition;
}
}