@@ -30,6 +30,11 @@ class HttpKernel implements BridgeInterface
3030 */
3131 protected $ bootstrap ;
3232
33+ /**
34+ * @var string[]
35+ */
36+ protected $ tempFiles = [];
37+
3338 /**
3439 * Bootstrap an application implementing the HttpKernelInterface.
3540 *
@@ -82,7 +87,8 @@ public function handle(ServerRequestInterface $request)
8287 $ syResponse = $ this ->application ->handle ($ syRequest );
8388 } catch (\Exception $ exception ) {
8489 // internal server error
85- $ response = new Psr7 \Response (500 , ['Content-type ' => 'text/plain ' ], $ exception ->getMessage ());
90+ error_log ((string )$ exception );
91+ $ response = new Psr7 \Response (500 , ['Content-type ' => 'text/plain ' ], 'Unexpected error ' );
8692
8793 // end buffering if we need to throw
8894 @ob_end_clean ();
@@ -142,8 +148,30 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
142148 session_id (Utils::generateSessionId ());
143149 }
144150
145- // files
146- $ files = $ psrRequest ->getUploadedFiles ();
151+ /** @var \React\Http\Io\UploadedFile $file */
152+ $ uploadedFiles = $ psrRequest ->getUploadedFiles ();
153+
154+ $ mapFiles = function (&$ files ) use (&$ mapFiles ) {
155+ foreach ($ files as &$ value ) {
156+ if (is_array ($ value )) {
157+ $ mapFiles ($ value );
158+ } else if ($ value instanceof \React \Http \Io \UploadedFile) {
159+ $ tmpname = tempnam (sys_get_temp_dir (), 'upload ' );
160+ $ this ->tempFiles [] = $ tmpname ;
161+
162+ file_put_contents ($ tmpname , (string )$ value ->getStream ());
163+ $ value = new \Symfony \Component \HttpFoundation \File \UploadedFile (
164+ $ tmpname ,
165+ $ value ->getClientFilename (),
166+ $ value ->getClientMediaType (),
167+ $ value ->getSize (),
168+ $ value ->getError (),
169+ true
170+ );
171+ }
172+ }
173+ };
174+ $ mapFiles ($ uploadedFiles );
147175
148176 // @todo check howto handle additional headers
149177
@@ -158,7 +186,7 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
158186 }
159187
160188 /** @var SymfonyRequest $syRequest */
161- $ syRequest = new $ class ($ query , $ post , $ attributes = [], $ _COOKIE , $ files , $ _SERVER , $ psrRequest ->getBody ());
189+ $ syRequest = new $ class ($ query , $ post , $ attributes = [], $ _COOKIE , $ uploadedFiles , $ _SERVER , $ psrRequest ->getBody ());
162190
163191 $ syRequest ->setMethod ($ method );
164192
@@ -256,6 +284,12 @@ protected function mapResponse(SymfonyResponse $syResponse)
256284
257285 $ psrResponse = $ psrResponse ->withBody (Psr7 \stream_for ($ content ));
258286
287+ foreach ($ this ->tempFiles as $ tmpname ) {
288+ if (file_exists ($ tmpname )) {
289+ unlink ($ tmpname );
290+ }
291+ }
292+
259293 return $ psrResponse ;
260294 }
261295
0 commit comments