diff --git a/app/controllers/mock.php b/app/controllers/mock.php index d4076547e0..ffffff1ad6 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -234,29 +234,74 @@ 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 */ $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+1 !== 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); + } + } + 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']]; + $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); + } } } });