-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformdata.gquery.js
118 lines (110 loc) · 3.95 KB
/
formdata.gquery.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/** -------------------------------------
*
* gQuery FormData v0.1.0 [PREVIEW]
* (c) 2021-present, JU Chengren (Ganxiaozhe)
*
* Released under the GPLv3 License.
* gquery.org/license
*
* ------------------------------------- */
if(!window.gQuery || !window.gQuery.fn.gquery){
throw new Error("请先引入 gQuery v1.5.2 以上版本!");
}
console.log('%c gQueryFormData v0.1.0 %c www.gquery.org \n','color: #fff; background: #030307; padding:5px 0; margin-top: 1em;','background: #efefef; color: #333; padding:5px 0;');
$.formdata = new $.chain({
gquery:true,
LINE_BREAK:'\r\n',
init: function(){
this.getBoundary();
this.data = {};
},
getBoundary: function(){
return this.boundary ? this.boundary : this._generateBoundary();
},
_generateBoundary: function(){
this.boundary = '----gQueryFormBoundary'+this._getRandomString(16);
return this.boundary;
},
_getRandomString: function(min, max){
let returnStr = '',
range = (max ? Math.round(Math.random() * (max-min)) + min : min),
charStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for(let i=0; i<range; i++){
let index = Math.round(Math.random() * (charStr.length-1));
returnStr += charStr.substring(index, index+1);
}
return returnStr;
},
append: function(key, val){
if(typeof val!=='object'){
return this._append(key, val);
}
let valProto = Object.prototype.toString.call(val);
switch(valProto){
case '[object Object]':
case '[object Array]':
val = JSON.stringify(val);break;
case '[object File]':
break;
default:
console.error('暂不支持的类型:'+valProto);
return;
}
return this._append(key, val);
},
_append: function(key, val){
if(this.data[key]){
this.data[key].push(val);
} else {
this.data[key] = [val];
}
},
_toBuffer: async function(file){
const arrayBuffer = await file.arrayBuffer();
return Buffer.from(arrayBuffer);
},
getAll: function(name){
return name ? this.data[name] : this.data;
},
toString: async function(){
return (await this.toBuffer()).toString();
},
toBuffer: async function(){
let result = [], rsize = 0;
for(let k in this.data){
let v = this.data[k];
for(let i = 0; i < v.length; i++){
let buf = await this._getContent(k, v[i]);
result.push(buf);rsize += buf.length;
}
}
let lastbuf = Buffer('--'+this.boundary+'--');
result.push(lastbuf);rsize += lastbuf.length;
return Buffer.concat(result, rsize);
},
_getContent: async function(key, val){
let bufArray = [], bufSize = 0;
let result = `--${this.boundary}${this.LINE_BREAK}Content-Disposition: form-data; name="${key}"`;
if(typeof val!=='object'){
result += this.LINE_BREAK.repeat(2) + val + this.LINE_BREAK;
return Buffer(result);
}
let valProto = Object.prototype.toString.call(val);
switch(valProto){
case '[object File]':
result += `; filename="${val.name}"`;
result += this.LINE_BREAK + `Content-Type: ${val.type}` + this.LINE_BREAK.repeat(2);
let buf = Buffer(result);
bufArray.push(buf);bufSize += buf.length;
buf = await this._toBuffer(val);
bufArray.push(buf);bufSize += buf.length;
buf = Buffer(this.LINE_BREAK);
bufArray.push(buf);bufSize += buf.length;
break;
}
return Buffer.concat(bufArray, bufSize);
},
_getContentType: function(){
return `multipart/form-data; boundary=${this.boundary}`;
}
});