From a4f183cc6223ab02e8a06d0c0282afc10451ff35 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 3 Mar 2022 17:45:20 +0000 Subject: [PATCH 1/8] fix examples --- docs/sdks/dart/EXAMPLES.md | 2 +- docs/sdks/flutter/EXAMPLES.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/sdks/dart/EXAMPLES.md b/docs/sdks/dart/EXAMPLES.md index 9b766b2229..a16ba88385 100644 --- a/docs/sdks/dart/EXAMPLES.md +++ b/docs/sdks/dart/EXAMPLES.md @@ -41,7 +41,7 @@ Upload File: ```dart Storage storage = Storage(client); -InputFile file = InputFile(path: './path-to-file/image.jpg', fileName: 'image.jpg'); +InputFile file = InputFile(path: './path-to-file/image.jpg', filename: 'image.jpg'); storage.createFile( bucketId: '[BUCKET_ID]', diff --git a/docs/sdks/flutter/EXAMPLES.md b/docs/sdks/flutter/EXAMPLES.md index 7b4015d308..c20544439a 100644 --- a/docs/sdks/flutter/EXAMPLES.md +++ b/docs/sdks/flutter/EXAMPLES.md @@ -40,9 +40,9 @@ Storage storage = Storage(client); late InputFile file; if(kIsWeb) { - file = InputFile(file: MultipartFile.fromFile('./path-to-file/image.jpg', filename: 'image.jpg')); + file = InputFile(file: await MultipartFile.fromFile('file', './path-to-file/image.jpg', filename: 'image.jpg')); } else { - file = InputFile(path: './path-to-file/image.jpg', fileName: 'image.jpg'); + file = InputFile(path: './path-to-file/image.jpg', filename: 'image.jpg'); } storage.createFile( From 07e104ea019af09aaf9bd803cb78e9705d475297 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 4 Mar 2022 01:14:44 +0400 Subject: [PATCH 2/8] feat: fix timeout issues --- app/executor.php | 16 ++++++++------- src/Executor/Executor.php | 41 +++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/executor.php b/app/executor.php index 40c636acb2..51215b637e 100644 --- a/app/executor.php +++ b/app/executor.php @@ -25,14 +25,14 @@ use Utopia\Swoole\Response; use Utopia\Validator\ArrayList; use Utopia\Validator\Assoc; use Utopia\Validator\Boolean; -use Utopia\Validator\Range as ValidatorRange; +use Utopia\Validator\Range; use Utopia\Validator\Text; Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); /** Constants */ -const MAINTENANCE_INTERVAL = 1200; // 20 minutes +const MAINTENANCE_INTERVAL = 3600; // 3600 seconds = 1 hour /** * Create a Swoole table to store runtime information @@ -395,9 +395,9 @@ App::delete('/v1/runtimes/:runtimeId') App::post('/v1/execution') ->desc('Create an execution') ->param('runtimeId', '', new Text(64), 'The runtimeID to execute') - ->param('vars', [], new Assoc(), 'Environment variables required for the build', false) - ->param('data', '', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) - ->param('timeout', 15, new ValidatorRange(1, 900), 'Function maximum execution time in seconds.', true) + ->param('vars', [], new Assoc(), 'Environment variables required for the build') + ->param('data', '{}', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) + ->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Function maximum execution time in seconds.') ->inject('activeRuntimes') ->inject('response') ->action( @@ -423,17 +423,19 @@ App::post('/v1/execution') $errNo = -1; $executorResponse = ''; + $timeout = $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900); + $ch = \curl_init(); $body = \json_encode([ 'env' => $vars, 'payload' => $data, - 'timeout' => $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) + 'timeout' => $timeout ]); \curl_setopt($ch, CURLOPT_URL, "http://" . $runtimeId . ":3000/"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - \curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)); + \curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); \curl_setopt($ch, CURLOPT_HTTPHEADER, [ diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 2bdad22e8f..33cd17cce0 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -82,7 +82,9 @@ class Executor 'commands' => $commands ]; - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)); + $timeout = (int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900); + + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $timeout); $status = $response['headers']['status-code']; if ($status >= 400) { @@ -159,13 +161,18 @@ class Executor 'timeout' => $timeout, ]; - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + /* Add 2 seconds as a buffer to the actual timeout value since there can be a slight variance*/ + $requestTimeout = $timeout + 2; + + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); $status = $response['headers']['status-code']; - if ($status >= 400) { - for ($attempts = 0; $attempts < 10; $attempts++) { - switch ($status) { - case 404: + for ($attempts = 0; $attempts < 10; $attempts++) { + try { + switch (true) { + case $status < 400: + return $response['body']; + case $status == 404: $response = $this->createRuntime( deploymentId: $deploymentId, projectId: $projectId, @@ -176,31 +183,23 @@ class Executor entrypoint: $entrypoint, commands: [] ); - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); $status = $response['headers']['status-code']; break; - case 406: - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); + case $status == 406: + $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); $status = $response['headers']['status-code']; break; default: throw new \Exception($response['body']['message'], $status); } - - if ($status < 400) { - return $response['body']; - } - - if ($status != 406) { - throw new \Exception($response['body']['message'], $status); - } - - sleep(2); + } catch (\Exception $e) { + throw new \Exception($e->getMessage(), $e->getCode()); } - throw new Exception($response['body']['message'], 503); + sleep(2); } - return $response['body']; + throw new Exception($response['body']['message'], 503); } /** From b3fbb7c40dd3389c4fa0d43028edaedec3b8067f Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 4 Mar 2022 11:57:44 +0545 Subject: [PATCH 3/8] fix mock upload --- app/controllers/mock.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 8d85b94578..0d02bab183 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -252,7 +252,7 @@ App::post('/v1/mock/tests/general/upload') $end = $request->getContentRangeEnd(); $size = $request->getContentRangeSize(); $id = $request->getHeader('x-appwrite-id', ''); - $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; + $file['size'] = (\is_array($file['size'])) ? $file['size'][0] : $file['size']; if(is_null($start) || is_null($end) || is_null($size)) { throw new Exception('Invalid content-range header', 400, Exception::GENERAL_MOCK); @@ -274,15 +274,14 @@ App::post('/v1/mock/tests/general/upload') throw new Exception('Chunk size must be 5MB (except last chunk)', 400, Exception::GENERAL_MOCK); } - foreach ($file['size'] as $i => $sz) { - if ($end !== $size && $sz !== $chunkSize) { - throw new Exception('Wrong chunk size', 400, Exception::GENERAL_MOCK); - } - - if($sz > $chunkSize) { - throw new Exception('Chunk size must be 5MB or less', 400, Exception::GENERAL_MOCK); - } + if ($end !== $size && $file['size'] !== $chunkSize) { + throw new Exception('Wrong chunk size', 400, Exception::GENERAL_MOCK); } + + if($file['size'] > $chunkSize) { + throw new Exception('Chunk size must be 5MB or less', 400, Exception::GENERAL_MOCK); + } + if($end !== $size) { $response->json([ '$id'=> 'newfileid', From 57f410cb5fc2976b9f85c62a960b44f1fc5108d6 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 4 Mar 2022 11:59:20 +0545 Subject: [PATCH 4/8] fix mock upload --- app/controllers/mock.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 0d02bab183..d04ad016d0 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -290,9 +290,9 @@ App::post('/v1/mock/tests/general/upload') ]); } } 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']]; + $file['tmp_name'] = (\is_array($file['tmp_name'])) ? $file['tmp_name'][0] : $file['tmp_name']; + $file['name'] = (\is_array($file['name'])) ? $file['name'][0] : $file['name']; + $file['size'] = (\is_array($file['size'])) ? $file['size'][0] : $file['size']; foreach ($file['name'] as $i => $name) { if ($name !== 'file.png') { @@ -305,11 +305,9 @@ App::post('/v1/mock/tests/general/upload') throw new Exception('Wrong file size', 400, Exception::GENERAL_MOCK); } } - - foreach ($file['tmp_name'] as $i => $tmpName) { - if (\md5(\file_get_contents($tmpName)) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') { - throw new Exception('Wrong file uploaded', 400, Exception::GENERAL_MOCK); - } + + if (\md5(\file_get_contents($file['tmp_name'])) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') { + throw new Exception('Wrong file uploaded', 400, Exception::GENERAL_MOCK); } } }); From 2096c5c8b85d22eaf1e91a8ab5549f68464469a1 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 4 Mar 2022 12:03:10 +0545 Subject: [PATCH 5/8] fix --- app/controllers/mock.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index d04ad016d0..69bbe8d68a 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -294,16 +294,12 @@ App::post('/v1/mock/tests/general/upload') $file['name'] = (\is_array($file['name'])) ? $file['name'][0] : $file['name']; $file['size'] = (\is_array($file['size'])) ? $file['size'][0] : $file['size']; - foreach ($file['name'] as $i => $name) { - if ($name !== 'file.png') { - throw new Exception('Wrong file name', 400, Exception::GENERAL_MOCK); - } + if ($file['name'] !== 'file.png') { + throw new Exception('Wrong file name', 400, Exception::GENERAL_MOCK); } - foreach ($file['size'] as $i => $size) { - if ($size !== 38756) { + if ($file['size'] !== 38756) { throw new Exception('Wrong file size', 400, Exception::GENERAL_MOCK); - } } if (\md5(\file_get_contents($file['tmp_name'])) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') { From a742bdbe11ec43d8338c3168499c588649704af7 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 4 Mar 2022 12:29:12 +0545 Subject: [PATCH 6/8] dart flutter fix --- app/config/platforms.php | 4 ++-- .../0.13.x/client-flutter/examples/storage/create-file.md | 2 +- .../server-dart/examples/functions/create-deployment.md | 2 +- .../0.13.x/server-dart/examples/storage/create-file.md | 2 +- docs/sdks/dart/CHANGELOG.md | 4 ++++ docs/sdks/flutter/CHANGELOG.md | 4 ++++ 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/config/platforms.php b/app/config/platforms.php index 7efa8367b5..6649723a0c 100644 --- a/app/config/platforms.php +++ b/app/config/platforms.php @@ -63,7 +63,7 @@ return [ [ 'key' => 'flutter', 'name' => 'Flutter', - 'version' => '4.0.0', + 'version' => '4.0.1', 'url' => 'https://github.com/appwrite/sdk-for-flutter', 'package' => 'https://pub.dev/packages/appwrite', 'enabled' => true, @@ -352,7 +352,7 @@ return [ [ 'key' => 'dart', 'name' => 'Dart', - 'version' => '4.0.0', + 'version' => '4.0.1', 'url' => 'https://github.com/appwrite/sdk-for-dart', 'package' => 'https://pub.dev/packages/dart_appwrite', 'enabled' => true, diff --git a/docs/examples/0.13.x/client-flutter/examples/storage/create-file.md b/docs/examples/0.13.x/client-flutter/examples/storage/create-file.md index 16732b8d2e..5c591ff223 100644 --- a/docs/examples/0.13.x/client-flutter/examples/storage/create-file.md +++ b/docs/examples/0.13.x/client-flutter/examples/storage/create-file.md @@ -12,7 +12,7 @@ void main() { // Init SDK Future result = storage.createFile( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', - file: await MultipartFile.fromPath('file', './path-to-files/image.jpg', 'image.jpg'), + file: InputFile(path: './path-to-files/image.jpg', filename: 'image.jpg'), ); result diff --git a/docs/examples/0.13.x/server-dart/examples/functions/create-deployment.md b/docs/examples/0.13.x/server-dart/examples/functions/create-deployment.md index 2df3b45e7f..a407622ffc 100644 --- a/docs/examples/0.13.x/server-dart/examples/functions/create-deployment.md +++ b/docs/examples/0.13.x/server-dart/examples/functions/create-deployment.md @@ -13,7 +13,7 @@ void main() { // Init SDK Future result = functions.createDeployment( functionId: '[FUNCTION_ID]', entrypoint: '[ENTRYPOINT]', - code: await MultipartFile.fromPath('code', './path-to-files/image.jpg', 'image.jpg'), + code: InputFile(path: './path-to-files/image.jpg', filename: 'image.jpg'), activate: false, ); diff --git a/docs/examples/0.13.x/server-dart/examples/storage/create-file.md b/docs/examples/0.13.x/server-dart/examples/storage/create-file.md index 04b7f63ac0..ef596fcc49 100644 --- a/docs/examples/0.13.x/server-dart/examples/storage/create-file.md +++ b/docs/examples/0.13.x/server-dart/examples/storage/create-file.md @@ -14,7 +14,7 @@ void main() { // Init SDK Future result = storage.createFile( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', - file: await MultipartFile.fromPath('file', './path-to-files/image.jpg', 'image.jpg'), + file: InputFile(path: './path-to-files/image.jpg', filename: 'image.jpg'), ); result diff --git a/docs/sdks/dart/CHANGELOG.md b/docs/sdks/dart/CHANGELOG.md index 577116a5bc..65793a8f44 100644 --- a/docs/sdks/dart/CHANGELOG.md +++ b/docs/sdks/dart/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.0.1 +* Fix InputFile filename param +* Fix examples + ## 4.0.0 * Support for Appwrite 0.13 * **BREAKING** **Tags** have been renamed to **Deployments** diff --git a/docs/sdks/flutter/CHANGELOG.md b/docs/sdks/flutter/CHANGELOG.md index e06f6421ee..1fba948022 100644 --- a/docs/sdks/flutter/CHANGELOG.md +++ b/docs/sdks/flutter/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.0.1 +* Fix InputFile filename param +* Fix examples + ## 4.0.0 * Support for Appwrite 0.13 * **BREAKING** **Tags** have been renamed to **Deployments** From ea1d55e82b44e942582d95443a4e9a8c36af5891 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 4 Mar 2022 14:34:42 +0400 Subject: [PATCH 7/8] feat: use strict comparison --- src/Executor/Executor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 33cd17cce0..1a96b3561b 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -172,7 +172,7 @@ class Executor switch (true) { case $status < 400: return $response['body']; - case $status == 404: + case $status === 404: $response = $this->createRuntime( deploymentId: $deploymentId, projectId: $projectId, @@ -186,7 +186,7 @@ class Executor $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); $status = $response['headers']['status-code']; break; - case $status == 406: + case $status === 406: $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); $status = $response['headers']['status-code']; break; From 4a087ef33d9165bcb18236abd6069cf77c9cc3de Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 4 Mar 2022 14:37:56 +0400 Subject: [PATCH 8/8] feat: use shorter syntax --- app/executor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/executor.php b/app/executor.php index 51215b637e..b82d17a117 100644 --- a/app/executor.php +++ b/app/executor.php @@ -423,7 +423,7 @@ App::post('/v1/execution') $errNo = -1; $executorResponse = ''; - $timeout = $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900); + $timeout ??= (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900); $ch = \curl_init(); $body = \json_encode([