-
Notifications
You must be signed in to change notification settings - Fork 57
/
utils.js
70 lines (55 loc) · 1.61 KB
/
utils.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
window.escapeShellArg = function (arg, doubleQuotes) {
let ret = "";
if (doubleQuotes) {
ret = arg.replace(/["\\]/g, (m) => `\\${m.slice(0, 1)}`);
return `"${ret}"`;
}
ret = arg.replace(/'/g, (m) => `'\\${m.slice(0, 1)}'`);
return `'${ret}'`;
};
window.toQueryString = function (obj) {
let parts = [];
for (let [key, values] of Object.entries(obj))
if (Array.isArray(values))
for (let value of values)
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
else parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(values)}`);
return parts.join("&");
};
window.getFilenameFromContentDisposition = function (header) {
let headerL = header.toLowerCase();
let i;
i = headerL.indexOf("filename*=utf-8''");
if (i !== -1) {
i += 17;
let j = i;
while (j < headerL.length && !/[\s;]/.test(headerL[j])) ++j;
return decodeURIComponent(header.slice(i, j));
}
i = headerL.indexOf('filename="');
if (i !== -1) {
i += 10;
let j = i;
while (
j < headerL.length &&
!(header[j] === '"' && headerL.slice(j - 1, j + 1) !== '\\"')
)
++j;
return JSON.parse(header.slice(i - 1, j + 1));
}
i = headerL.indexOf("filename=");
if (i !== -1) {
i += 9;
let j = i;
while (j < headerL.length && !/[\s;]/.test(headerL[j])) ++j;
return header.slice(i, j);
}
return null;
};
window.getFilenameFromUrl = function (url) {
let j = url.indexOf("?");
if (j === -1) j = url.indexOf("#");
if (j === -1) j = url.length;
let i = url.lastIndexOf("/", j);
return decodeURIComponent(url.slice(i + 1, j));
};