From aa7e326ce9740d3ede102e3d501544e6b2bb2286 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 13 Dec 2022 15:43:29 +1300 Subject: [PATCH] Allow file uploads from explorer --- app/controllers/api/graphql.php | 6 ++++++ app/controllers/api/storage.php | 7 +++++++ tests/e2e/Services/GraphQL/StorageClientTest.php | 4 ++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/graphql.php b/app/controllers/api/graphql.php index c9c62be08..869d860af 100644 --- a/app/controllers/api/graphql.php +++ b/app/controllers/api/graphql.php @@ -86,6 +86,7 @@ App::post('/v1/graphql') } $type = $request->getHeader('content-type'); + if (\str_starts_with($type, 'application/graphql')) { $query = parseGraphql($request); } @@ -195,6 +196,7 @@ function parseMultipart(array $query, Request $request): array { $operations = \json_decode($query['operations'], true); $map = \json_decode($query['map'], true); + foreach ($map as $fileKey => $locations) { foreach ($locations as $location) { $items = &$operations; @@ -207,9 +209,13 @@ function parseMultipart(array $query, Request $request): array $items = $request->getFiles($fileKey); } } + $query['query'] = $operations['query']; $query['variables'] = $operations['variables']; + unset($query['operations']); + unset($query['map']); + return $query; } diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 3c8ffc564..1ea72f49e 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -417,7 +417,14 @@ App::post('/v1/storage/buckets/:bucketId/files') throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Maximum bucket file size is larger than _APP_STORAGE_LIMIT'); } + $file = $request->getFiles('file'); + + // GraphQL multipart spec adds files with index keys + if (empty($file)) { + $file = $request->getFiles(0); + } + if (empty($file)) { throw new Exception(Exception::STORAGE_FILE_EMPTY); } diff --git a/tests/e2e/Services/GraphQL/StorageClientTest.php b/tests/e2e/Services/GraphQL/StorageClientTest.php index 3c7804e0b..55fc10b06 100644 --- a/tests/e2e/Services/GraphQL/StorageClientTest.php +++ b/tests/e2e/Services/GraphQL/StorageClientTest.php @@ -73,9 +73,9 @@ class StorageClientTest extends Scope ] ]), 'map' => \json_encode([ - 'file' => ["variables.file"] + '0' => ["variables.file"] ]), - 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), + '0' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), ]; $file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([