@@ -30,6 +30,11 @@ class HttpKernel implements BridgeInterface
30
30
*/
31
31
protected $ bootstrap ;
32
32
33
+ /**
34
+ * @var string[]
35
+ */
36
+ protected $ tempFiles = [];
37
+
33
38
/**
34
39
* Bootstrap an application implementing the HttpKernelInterface.
35
40
*
@@ -82,7 +87,8 @@ public function handle(ServerRequestInterface $request)
82
87
$ syResponse = $ this ->application ->handle ($ syRequest );
83
88
} catch (\Exception $ exception ) {
84
89
// 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 ' );
86
92
87
93
// end buffering if we need to throw
88
94
@ob_end_clean ();
@@ -142,8 +148,30 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
142
148
session_id (Utils::generateSessionId ());
143
149
}
144
150
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 );
147
175
148
176
// @todo check howto handle additional headers
149
177
@@ -158,7 +186,7 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
158
186
}
159
187
160
188
/** @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 ());
162
190
163
191
$ syRequest ->setMethod ($ method );
164
192
@@ -256,6 +284,12 @@ protected function mapResponse(SymfonyResponse $syResponse)
256
284
257
285
$ psrResponse = $ psrResponse ->withBody (Psr7 \stream_for ($ content ));
258
286
287
+ foreach ($ this ->tempFiles as $ tmpname ) {
288
+ if (file_exists ($ tmpname )) {
289
+ unlink ($ tmpname );
290
+ }
291
+ }
292
+
259
293
return $ psrResponse ;
260
294
}
261
295
0 commit comments