From 5dfdf3b3acca6ef4dc76ccfb48867b9051588772 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 22 Oct 2021 16:06:23 +0545 Subject: [PATCH 1/4] wip --- app/controllers/mock.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index d4076547e0..306ee61b5f 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -237,6 +237,10 @@ App::post('/v1/mock/tests/general/upload') ->action(function ($x, $y, $z, $file, $request) { /** @var Utopia\Swoole\Request $request */ + // support multipart upload + // validate md5 for each chunk + // validate individual chunk and chunk size + $file = $request->getFiles('file'); $file['tmp_name'] = (\is_array($file['tmp_name'])) ? $file['tmp_name'] : [$file['tmp_name']]; $file['name'] = (\is_array($file['name'])) ? $file['name'] : [$file['name']]; From 410081bf116e45f5212690c5d2434c7dd9f3bec8 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 2 Nov 2021 11:17:02 +0545 Subject: [PATCH 2/4] some tests to validate mock tests --- app/controllers/mock.php | 77 ++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 306ee61b5f..86f25b6e51 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -234,33 +234,72 @@ App::post('/v1/mock/tests/general/upload') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->param('file', [], new File(), 'Sample file param', false) ->inject('request') - ->action(function ($x, $y, $z, $file, $request) { + ->inject('response') + ->action(function ($x, $y, $z, $file, $request, $response) { /** @var Utopia\Swoole\Request $request */ + /** @var Utopia\Swoole\Response $response */ - // support multipart upload - // validate md5 for each chunk - // validate individual chunk and chunk size - $file = $request->getFiles('file'); - $file['tmp_name'] = (\is_array($file['tmp_name'])) ? $file['tmp_name'] : [$file['tmp_name']]; - $file['name'] = (\is_array($file['name'])) ? $file['name'] : [$file['name']]; - $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; + + $contentRange = $request->getHeader('content-range'); + if(!empty($contentRange)) { + $start = $request->getContentRangeStart(); + $end = $request->getContentRangeEnd(); + $size = $request->getContentRangeSize(); + $id = $request->getHeader('x-appwrite-id', ''); + $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; - foreach ($file['name'] as $i => $name) { - if ($name !== 'file.png') { - throw new Exception('Wrong file name', 400); + if(is_null($start) || is_null($end) || is_null($size)) { + throw new Exception('Invalid content-range header', 400); } - } - foreach ($file['size'] as $i => $size) { - if ($size !== 38756) { - throw new Exception('Wrong file size', 400); + if($start > $end || $end > $size) { + throw new Exception('Invalid content-range header', 400); } - } - foreach ($file['tmp_name'] as $i => $tmpName) { - if (\md5(\file_get_contents($tmpName)) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') { - throw new Exception('Wrong file uploaded', 400); + if($start === 0 && !empty($id)) { + throw new Exception('First chunked request cannot have id header', 400); + } + + if($start !== 0 && $id !== 'newfileid') { + throw new Exception('All chunked request must have id header (except first)', 400); + } + + if($end !== $size && $end-$start !== 5*1024*1024) { + throw new Exception('Chunk size must be 5MB (except last chunk)', 400); + } + + foreach ($file['size'] as $i => $sz) { + if ($end !== $size && $sz !== 5*1024*1024) { + throw new Exception('Wrong chunk size', 400); + } + + if($sz > 5*1024*1024) { + throw new Exception('Chunk size must be 5MB or less', 400); + } + } + $response->json(['$id'=> 'newfileid']); + } else { + $file['tmp_name'] = (\is_array($file['tmp_name'])) ? $file['tmp_name'] : [$file['tmp_name']]; + $file['name'] = (\is_array($file['name'])) ? $file['name'] : [$file['name']]; + $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; + + foreach ($file['name'] as $i => $name) { + if ($name !== 'file.png') { + throw new Exception('Wrong file name', 400); + } + } + + foreach ($file['size'] as $i => $size) { + if ($size !== 38756) { + throw new Exception('Wrong file size', 400); + } + } + + foreach ($file['tmp_name'] as $i => $tmpName) { + if (\md5(\file_get_contents($tmpName)) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') { + throw new Exception('Wrong file uploaded', 400); + } } } }); From cff399530075b9215f6521effb003589eed36dcf Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 2 Nov 2021 14:44:26 +0545 Subject: [PATCH 3/4] fix response --- app/controllers/mock.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 86f25b6e51..6f24f055e8 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -249,6 +249,8 @@ App::post('/v1/mock/tests/general/upload') $id = $request->getHeader('x-appwrite-id', ''); $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; + var_dump($id); + if(is_null($start) || is_null($end) || is_null($size)) { throw new Exception('Invalid content-range header', 400); } @@ -265,7 +267,7 @@ App::post('/v1/mock/tests/general/upload') throw new Exception('All chunked request must have id header (except first)', 400); } - if($end !== $size && $end-$start !== 5*1024*1024) { + if($end !== $size && $end-$start+1 !== 5*1024*1024) { throw new Exception('Chunk size must be 5MB (except last chunk)', 400); } @@ -278,7 +280,9 @@ App::post('/v1/mock/tests/general/upload') throw new Exception('Chunk size must be 5MB or less', 400); } } - $response->json(['$id'=> 'newfileid']); + if($end !== $size) { + $response->json(['$id'=> 'newfileid']); + } } else { $file['tmp_name'] = (\is_array($file['tmp_name'])) ? $file['tmp_name'] : [$file['tmp_name']]; $file['name'] = (\is_array($file['name'])) ? $file['name'] : [$file['name']]; From 27cc105325c0819ac4acadf9ecb06537f1b4aacf Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 2 Nov 2021 15:04:18 +0545 Subject: [PATCH 4/4] remove leftover dump --- app/controllers/mock.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 6f24f055e8..ffffff1ad6 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -249,8 +249,6 @@ App::post('/v1/mock/tests/general/upload') $id = $request->getHeader('x-appwrite-id', ''); $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; - var_dump($id); - if(is_null($start) || is_null($end) || is_null($size)) { throw new Exception('Invalid content-range header', 400); }