| 190 | } |
| 191 | |
| 192 | function FormDataToObject(form, contentType) { |
| 193 | const boundary = contentType.split("; ")[1].split("=")[1]; |
| 194 | const splitBoundary = `--${boundary}`; |
| 195 | const index = form.indexOf(splitBoundary); |
| 196 | form = form.substr(index); |
| 197 | const lastIndex = form.lastIndexOf(splitBoundary); |
| 198 | form = form.substring(0, lastIndex); |
| 199 | const array = compact(form.split(splitBoundary)).map((a) => { |
| 200 | const entity = compact(a.split("\r\n")); |
| 201 | const regex = /Content-Disposition: form-data; name="(.*)"/; |
| 202 | var matchs = regex.exec(entity[0]); |
| 203 | return { |
| 204 | name: matchs[1], |
| 205 | value: entity[1], |
| 206 | }; |
| 207 | }); |
| 208 | |
| 209 | function compact(array) { |
| 210 | let resIndex = 0; |
| 211 | const result = []; |
| 212 | if (array == null) { |
| 213 | return result; |
| 214 | } |
| 215 | for (const value of array) { |
| 216 | if (value) { |
| 217 | result[resIndex++] = value; |
| 218 | } |
| 219 | } |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | const result = {}; |
| 224 | array.forEach((a) => { |
| 225 | result[a.name] = a.value; |
| 226 | }); |
| 227 | return result; |
| 228 | } |
| 229 | |
| 230 | function ObjectToFormData(object, contentType) { |
| 231 | const boundary = contentType.split("; ")[1].split("=")[1]; |