Skip to content

Commit 2e8ec9e

Browse files
benjaminjonardandig
authored andcommitted
Gracefully empty file input (#84)
1 parent 3cbe1bb commit 2e8ec9e

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

Bridges/HttpKernel.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
use PHPPM\Utils;
1010
use Psr\Http\Message\ServerRequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
12+
use Psr\Http\Message\UploadedFileInterface;
1213
use RingCentral\Psr7;
1314
use Symfony\Component\HttpFoundation\Cookie;
15+
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile;
1416
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1517
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1618
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
1719
use Symfony\Component\HttpKernel\TerminableInterface;
1820

21+
1922
class HttpKernel implements BridgeInterface
2023
{
2124
/**
@@ -152,22 +155,28 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
152155
$uploadedFiles = $psrRequest->getUploadedFiles();
153156

154157
$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) {
158+
foreach ($files as &$file) {
159+
if (is_array($file)) {
160+
$mapFiles($file);
161+
} else if ($file instanceof UploadedFileInterface) {
159162
$tmpname = tempnam(sys_get_temp_dir(), 'upload');
160163
$this->tempFiles[] = $tmpname;
161164

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-
);
165+
if (UPLOAD_ERR_NO_FILE == $file->getError()) {
166+
$file = null;
167+
} else {
168+
if (UPLOAD_ERR_OK == $file->getError()) {
169+
file_put_contents($tmpname, (string)$file->getStream());
170+
}
171+
$file = new SymfonyFile(
172+
$tmpname,
173+
$file->getClientFilename(),
174+
$file->getClientMediaType(),
175+
$file->getSize(),
176+
$file->getError(),
177+
true
178+
);
179+
}
171180
}
172181
}
173182
};

0 commit comments

Comments
 (0)