66
77use Amp \Http \Server \Request as AmpRequest ;
88use InvalidArgumentException ;
9- use Symfony \Component \HttpFoundation \File \UploadedFile ;
109
1110/**
1211 * Derived work from the MultipartParser in ReactPHP.
1918final class MultipartParser
2019{
2120 /**
22- * @var array{"$_POST": array<array-key, mixed[]|string>, "$_FILES": array<array-key, UploadedFile []|UploadedFile >}
21+ * @var array{"$_POST": array<array-key, mixed[]|string>, "$_FILES": array<array-key, array{tmp_file: string, error: int, name: string, type: string, size: int} []|array{tmp_file: string, error: int, name: string, type: string, size: int} >}
2322 */
2423 private array $ superglobals = ['$_POST ' => [], '$_FILES ' => []];
2524
@@ -88,7 +87,7 @@ public function __construct(
8887 }
8988
9089 /**
91- * @return array{array<mixed[]|string>, array<UploadedFile []|UploadedFile >}
90+ * @return array{array<mixed[]|string>, array<array{tmp_file: string, error: int, name: string, type: string, size: int} []|array{tmp_file: string, error: int, name: string, type: string, size: int} >}
9291 */
9392 public function parse (AmpRequest $ request , string $ body ): array
9493 {
@@ -219,8 +218,12 @@ private function parseFile(string $name, string $filename, ?string $contentType,
219218 );
220219 }
221220
222- private function parseUploadedFile (string $ filename , ?string $ contentType , string $ contents ): ?UploadedFile
221+ /**
222+ * @return array{tmp_file: string, error: int, name: string, type: string, size: int}|null
223+ */
224+ private function parseUploadedFile (string $ filename , ?string $ contentType , string $ contents ): ?array
223225 {
226+ $ contentType ??= 'application/octet-stream ' ;
224227 $ size = strlen ($ contents );
225228
226229 // no file selected (zero size and empty filename)
@@ -230,13 +233,13 @@ private function parseUploadedFile(string $filename, ?string $contentType, strin
230233 return null ;
231234 }
232235
233- return new UploadedFile (
234- '' ,
235- $ filename ,
236- $ contentType ,
237- UPLOAD_ERR_NO_FILE ,
238- true ,
239- ) ;
236+ return [
237+ 'tmp_name ' => ' ' ,
238+ ' error ' => UPLOAD_ERR_NO_FILE ,
239+ ' name ' => $ filename ,
240+ ' type ' => $ contentType ,
241+ ' size ' => $ size ,
242+ ] ;
240243 }
241244
242245 // ignore excessive number of file uploads
@@ -246,36 +249,36 @@ private function parseUploadedFile(string $filename, ?string $contentType, strin
246249
247250 // file exceeds "upload_max_filesize" ini setting
248251 if ($ size > $ this ->uploadMaxFilesize ) {
249- return new UploadedFile (
250- '' ,
251- $ filename ,
252- $ contentType ,
253- UPLOAD_ERR_INI_SIZE ,
254- true ,
255- ) ;
252+ return [
253+ 'tmp_name ' => ' ' ,
254+ ' error ' => UPLOAD_ERR_INI_SIZE ,
255+ ' name ' => $ filename ,
256+ ' type ' => $ contentType ,
257+ ' size ' => $ size ,
258+ ] ;
256259 }
257260
258261 // file exceeds MAX_FILE_SIZE value
259262 if ($ this ->maxFileSize !== null && $ size > $ this ->maxFileSize ) {
260- return new UploadedFile (
261- '' ,
262- $ filename ,
263- $ contentType ,
264- UPLOAD_ERR_FORM_SIZE ,
265- true ,
266- ) ;
263+ return [
264+ 'tmp_name ' => ' ' ,
265+ ' error ' => UPLOAD_ERR_FORM_SIZE ,
266+ ' name ' => $ filename ,
267+ ' type ' => $ contentType ,
268+ ' size ' => $ size ,
269+ ] ;
267270 }
268271
269272 $ tempFileName = tempnam (sys_get_temp_dir (), 'php ' );
270273 file_put_contents ($ tempFileName , $ contents );
271274
272- return new UploadedFile (
273- $ tempFileName ,
274- $ filename ,
275- $ contentType ,
276- UPLOAD_ERR_OK ,
277- true ,
278- ) ;
275+ return [
276+ ' tmp_name ' => $ tempFileName ,
277+ ' error ' => UPLOAD_ERR_OK ,
278+ ' name ' => $ filename ,
279+ ' type ' => $ contentType ,
280+ ' size ' => $ size ,
281+ ] ;
279282 }
280283
281284 private function parsePost (string $ name , string $ value ): void
0 commit comments