| 308 | } |
| 309 | |
| 310 | async _handlePart(part) { |
| 311 | if (part.originalFilename && typeof part.originalFilename !== 'string') { |
| 312 | this._error( |
| 313 | new FormidableError( |
| 314 | `the part.originalFilename should be string when it exists`, |
| 315 | errors.filenameNotString, |
| 316 | ), |
| 317 | ); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | // This MUST check exactly for undefined. You can not change it to !part.originalFilename. |
| 322 | |
| 323 | // todo: uncomment when switch tests to Jest |
| 324 | // console.log(part); |
| 325 | |
| 326 | // ? NOTE(@tunnckocore): no it can be any falsey value, it most probably depends on what's returned |
| 327 | // from somewhere else. Where recently I changed the return statements |
| 328 | // and such thing because code style |
| 329 | // ? NOTE(@tunnckocore): or even better, if there is no mimetype, then it's for sure a field |
| 330 | // ? NOTE(@tunnckocore): originalFilename is an empty string when a field? |
| 331 | if (!part.mimetype) { |
| 332 | let value = ''; |
| 333 | const decoder = new StringDecoder( |
| 334 | part.transferEncoding || this.options.encoding, |
| 335 | ); |
| 336 | |
| 337 | part.on('data', (buffer) => { |
| 338 | this._fieldsSize += buffer.length; |
| 339 | if (this._fieldsSize > this.options.maxFieldsSize) { |
| 340 | this._error( |
| 341 | new FormidableError( |
| 342 | `options.maxFieldsSize (${this.options.maxFieldsSize} bytes) exceeded, received ${this._fieldsSize} bytes of field data`, |
| 343 | errors.maxFieldsSizeExceeded, |
| 344 | 413, // Payload Too Large |
| 345 | ), |
| 346 | ); |
| 347 | return; |
| 348 | } |
| 349 | value += decoder.write(buffer); |
| 350 | }); |
| 351 | |
| 352 | part.on('end', () => { |
| 353 | this.emit('field', part.name, value); |
| 354 | }); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | if (!this.options.filter(part)) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | this._flushing += 1; |
| 363 | |
| 364 | let fileSize = 0; |
| 365 | const newFilename = this._getNewName(part); |
| 366 | const filepath = this._joinDirectoryName(newFilename); |
| 367 | const file = await this._newFile({ |