Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uploaded files handling #827

Merged
merged 8 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix the behavior of the `excluded_exceptions` option: now it's used to skip capture of exceptions, not to purge the
`exception` data of the event, which resulted in broken or empty chains of exceptions in reported events (#822)
- Fix handling of uploaded files in the `RequestIntegration`, to respect the PSR-7 spec fully (#827)

## 2.1.0 (2019-05-22)

Expand Down
49 changes: 33 additions & 16 deletions src/Integration/RequestIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,10 @@ private function captureRequestBody(ServerRequestInterface $serverRequest)
}

$requestData = $serverRequest->getParsedBody();
$requestData = \is_array($requestData) ? $requestData : [];

foreach ($serverRequest->getUploadedFiles() as $fieldName => $uploadedFiles) {
if (!\is_array($uploadedFiles)) {
$uploadedFiles = [$uploadedFiles];
}

/** @var UploadedFileInterface $uploadedFile */
foreach ($uploadedFiles as $uploadedFile) {
$requestData[$fieldName][] = [
'client_filename' => $uploadedFile->getClientFilename(),
'client_media_type' => $uploadedFile->getClientMediaType(),
'size' => $uploadedFile->getSize(),
];
}
}
$requestData = array_merge(
$this->parseUploadedFiles($serverRequest->getUploadedFiles()),
\is_array($requestData) ? $requestData : []
);

if (!empty($requestData)) {
return $requestData;
Expand All @@ -196,4 +184,33 @@ private function captureRequestBody(ServerRequestInterface $serverRequest)

return $requestBody->getContents();
}

/**
* Create an array with the same structure as $uploadedFiles, but replacing
* each UploadedFileInterface with an array of info.
*
* @param array $uploadedFiles The uploaded files info from a PSR-7 server request
*
* @return array
*/
private function parseUploadedFiles(array $uploadedFiles): array
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
{
$result = [];

foreach ($uploadedFiles as $key => $item) {
if ($item instanceof UploadedFileInterface) {
$result[$key] = [
'client_filename' => $item->getClientFilename(),
'client_media_type' => $item->getClientMediaType(),
'size' => $item->getSize(),
];
} elseif (\is_array($item)) {
$result[$key] = $this->parseUploadedFiles($item);
} else {
throw new \UnexpectedValueException(sprintf('Expected either an object implementing the "%s" interface or an array. Got: "%s".', UploadedFileInterface::class, \is_object($item) ? \get_class($item) : \gettype($item)));
}
}

return $result;
}
}
34 changes: 18 additions & 16 deletions tests/Integration/RequestIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ public function applyToEventDataProvider(): \Generator
],
'data' => [
'foo' => [
[
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
],
],
Expand Down Expand Up @@ -372,8 +370,10 @@ public function applyToEventDataProvider(): \Generator
(new ServerRequest())
->withUploadedFiles([
'foo' => [
new UploadedFile('foo content', 123, UPLOAD_ERR_OK, 'foo.ext', 'application/text'),
new UploadedFile('bar content', 321, UPLOAD_ERR_OK, 'bar.ext', 'application/octet-stream'),
'bar' => [
new UploadedFile('foo content', 123, UPLOAD_ERR_OK, 'foo.ext', 'application/text'),
new UploadedFile('bar content', 321, UPLOAD_ERR_OK, 'bar.ext', 'application/octet-stream'),
],
],
])
->withUri(new Uri('http://www.example.com/foo'))
Expand All @@ -386,15 +386,17 @@ public function applyToEventDataProvider(): \Generator
],
'data' => [
'foo' => [
[
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
[
'client_filename' => 'bar.ext',
'client_media_type' => 'application/octet-stream',
'size' => 321,
'bar' => [
[
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
[
'client_filename' => 'bar.ext',
'client_media_type' => 'application/octet-stream',
'size' => 321,
],
],
],
],
Expand Down