From 52bca450ee4e00cdcd43aedfdb68c146834ee616 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Jun 2022 07:34:36 +0000 Subject: [PATCH 1/7] input file --- templates/php/src/InputFile.php.twig | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 templates/php/src/InputFile.php.twig diff --git a/templates/php/src/InputFile.php.twig b/templates/php/src/InputFile.php.twig new file mode 100644 index 000000000..a3a71fb76 --- /dev/null +++ b/templates/php/src/InputFile.php.twig @@ -0,0 +1,47 @@ +data; + } + + public function getPath(): ?string + { + return $this->path; + } + + public function getMimeType(): ?string + { + return $this->mimeType; + } + + public function getFilename(): ?string + { + return $this->filename; + } + + public static function withPath(string $path, ?string $mimeType, ?string $filename) + { + $instance = new InputFile(); + $instance->path = $path; + $instance->mimeType = $mimeType; + $instance->filename = $filename; + } + + public static function withData(string $data, ?string $mimeType, ?string $filename) + { + $instance = new InputFile(); + $instance->data = $data; + $instance->mimeType = $mimeType; + $instance->filename = $filename; + } +} \ No newline at end of file From 38de8cdcc82d3fa238428d3e46390ed53245442e Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Jun 2022 07:35:57 +0000 Subject: [PATCH 2/7] inputfile template render --- src/SDK/Language/PHP.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/SDK/Language/PHP.php b/src/SDK/Language/PHP.php index a5d789d23..d4d2926d9 100644 --- a/src/SDK/Language/PHP.php +++ b/src/SDK/Language/PHP.php @@ -183,6 +183,12 @@ public function getFiles() 'template' => 'php/src/Query.php.twig', 'minify' => false, ], + [ + 'scope' => 'default', + 'destination' => 'src/{{ spec.title | caseUcfirst}}/InputFile.php', + 'template' => 'php/src/InputFile.php.twig', + 'minify' => false, + ], [ 'scope' => 'default', 'destination' => 'src/{{ spec.title | caseUcfirst}}/{{ spec.title | caseUcfirst}}Exception.php', From d814b3f9bc7154d830dfe797e03a03c3553cece0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Jun 2022 08:17:53 +0000 Subject: [PATCH 3/7] fixes an updates --- composer.lock | 231 +++++++++++++------- src/SDK/Language/PHP.php | 2 +- templates/php/src/InputFile.php.twig | 8 +- templates/php/src/Services/Service.php.twig | 82 +++++-- tests/languages/php/test.php | 6 +- 5 files changed, 219 insertions(+), 110 deletions(-) diff --git a/composer.lock b/composer.lock index ac22884f9..4e905a138 100644 --- a/composer.lock +++ b/composer.lock @@ -131,16 +131,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.25.0", + "version": "v1.26.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "30885182c981ab175d4d034db0f6f469898070ab" + "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/30885182c981ab175d4d034db0f6f469898070ab", - "reference": "30885182c981ab175d4d034db0f6f469898070ab", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", + "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", "shasum": "" }, "require": { @@ -155,7 +155,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.23-dev" + "dev-main": "1.26-dev" }, "thanks": { "name": "symfony/polyfill", @@ -193,7 +193,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.25.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.26.0" }, "funding": [ { @@ -209,20 +209,20 @@ "type": "tidelift" } ], - "time": "2021-10-20T20:35:02+00:00" + "time": "2022-05-24T11:49:31+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.25.0", + "version": "v1.26.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825" + "reference": "9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/0abb51d2f102e00a4eefcf46ba7fec406d245825", - "reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e", + "reference": "9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e", "shasum": "" }, "require": { @@ -237,7 +237,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.23-dev" + "dev-main": "1.26-dev" }, "thanks": { "name": "symfony/polyfill", @@ -276,7 +276,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.25.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.26.0" }, "funding": [ { @@ -292,20 +292,20 @@ "type": "tidelift" } ], - "time": "2021-11-30T18:21:41+00:00" + "time": "2022-05-24T11:49:31+00:00" }, { "name": "twig/twig", - "version": "v3.3.10", + "version": "v3.4.1", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "8442df056c51b706793adf80a9fd363406dd3674" + "reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/8442df056c51b706793adf80a9fd363406dd3674", - "reference": "8442df056c51b706793adf80a9fd363406dd3674", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/e939eae92386b69b49cfa4599dd9bead6bf4a342", + "reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342", "shasum": "" }, "require": { @@ -320,7 +320,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.3-dev" + "dev-master": "3.4-dev" } }, "autoload": { @@ -356,7 +356,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.3.10" + "source": "https://github.com/twigphp/Twig/tree/v3.4.1" }, "funding": [ { @@ -368,7 +368,7 @@ "type": "tidelift" } ], - "time": "2022-04-06T06:47:41+00:00" + "time": "2022-05-17T05:48:52+00:00" } ], "packages-dev": [ @@ -591,16 +591,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.13.2", + "version": "v4.14.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "210577fe3cf7badcc5814d99455df46564f3c077" + "reference": "34bea19b6e03d8153165d8f30bba4c3be86184c1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/210577fe3cf7badcc5814d99455df46564f3c077", - "reference": "210577fe3cf7badcc5814d99455df46564f3c077", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/34bea19b6e03d8153165d8f30bba4c3be86184c1", + "reference": "34bea19b6e03d8153165d8f30bba4c3be86184c1", "shasum": "" }, "require": { @@ -641,9 +641,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.13.2" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.14.0" }, - "time": "2021-11-30T19:35:32+00:00" + "time": "2022-05-31T20:59:12+00:00" }, { "name": "phar-io/manifest", @@ -2423,20 +2423,21 @@ }, { "name": "symfony/console", - "version": "v6.0.8", + "version": "v6.1.1", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "0d00aa289215353aa8746a31d101f8e60826285c" + "reference": "6187424023fbffcd757789aeb517c9161b1eabee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/0d00aa289215353aa8746a31d101f8e60826285c", - "reference": "0d00aa289215353aa8746a31d101f8e60826285c", + "url": "https://api.github.com/repos/symfony/console/zipball/6187424023fbffcd757789aeb517c9161b1eabee", + "reference": "6187424023fbffcd757789aeb517c9161b1eabee", "shasum": "" }, "require": { - "php": ">=8.0.2", + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.1|^3", "symfony/polyfill-mbstring": "~1.0", "symfony/service-contracts": "^1.1|^2|^3", "symfony/string": "^5.4|^6.0" @@ -2498,7 +2499,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v6.0.8" + "source": "https://github.com/symfony/console/tree/v6.1.1" }, "funding": [ { @@ -2514,20 +2515,87 @@ "type": "tidelift" } ], - "time": "2022-04-20T15:01:42+00:00" + "time": "2022-06-08T14:02:09+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "v3.1.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918", + "reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.1-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.1.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-02-25T11:15:52+00:00" }, { "name": "symfony/polyfill-intl-grapheme", - "version": "v1.25.0", + "version": "v1.26.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-grapheme.git", - "reference": "81b86b50cf841a64252b439e738e97f4a34e2783" + "reference": "433d05519ce6990bf3530fba6957499d327395c2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/81b86b50cf841a64252b439e738e97f4a34e2783", - "reference": "81b86b50cf841a64252b439e738e97f4a34e2783", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/433d05519ce6990bf3530fba6957499d327395c2", + "reference": "433d05519ce6990bf3530fba6957499d327395c2", "shasum": "" }, "require": { @@ -2539,7 +2607,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.23-dev" + "dev-main": "1.26-dev" }, "thanks": { "name": "symfony/polyfill", @@ -2579,7 +2647,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.25.0" + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.26.0" }, "funding": [ { @@ -2595,20 +2663,20 @@ "type": "tidelift" } ], - "time": "2021-11-23T21:10:46+00:00" + "time": "2022-05-24T11:49:31+00:00" }, { "name": "symfony/polyfill-intl-normalizer", - "version": "v1.25.0", + "version": "v1.26.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-normalizer.git", - "reference": "8590a5f561694770bdcd3f9b5c69dde6945028e8" + "reference": "219aa369ceff116e673852dce47c3a41794c14bd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/8590a5f561694770bdcd3f9b5c69dde6945028e8", - "reference": "8590a5f561694770bdcd3f9b5c69dde6945028e8", + "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/219aa369ceff116e673852dce47c3a41794c14bd", + "reference": "219aa369ceff116e673852dce47c3a41794c14bd", "shasum": "" }, "require": { @@ -2620,7 +2688,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.23-dev" + "dev-main": "1.26-dev" }, "thanks": { "name": "symfony/polyfill", @@ -2663,7 +2731,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.25.0" + "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.26.0" }, "funding": [ { @@ -2679,24 +2747,24 @@ "type": "tidelift" } ], - "time": "2021-02-19T12:13:01+00:00" + "time": "2022-05-24T11:49:31+00:00" }, { "name": "symfony/process", - "version": "v6.0.8", + "version": "v6.1.0", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "d074154ea8b1443a96391f6e39f9e547b2dd01b9" + "reference": "318718453c2be58266f1a9e74063d13cb8dd4165" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/d074154ea8b1443a96391f6e39f9e547b2dd01b9", - "reference": "d074154ea8b1443a96391f6e39f9e547b2dd01b9", + "url": "https://api.github.com/repos/symfony/process/zipball/318718453c2be58266f1a9e74063d13cb8dd4165", + "reference": "318718453c2be58266f1a9e74063d13cb8dd4165", "shasum": "" }, "require": { - "php": ">=8.0.2" + "php": ">=8.1" }, "type": "library", "autoload": { @@ -2724,7 +2792,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v6.0.8" + "source": "https://github.com/symfony/process/tree/v6.1.0" }, "funding": [ { @@ -2740,24 +2808,24 @@ "type": "tidelift" } ], - "time": "2022-04-12T16:11:42+00:00" + "time": "2022-05-11T12:12:29+00:00" }, { "name": "symfony/service-contracts", - "version": "v3.0.1", + "version": "v3.1.0", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "e517458f278c2131ca9f262f8fbaf01410f2c65c" + "reference": "d66cd8ab656780f62c4215b903a420eb86358957" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/e517458f278c2131ca9f262f8fbaf01410f2c65c", - "reference": "e517458f278c2131ca9f262f8fbaf01410f2c65c", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/d66cd8ab656780f62c4215b903a420eb86358957", + "reference": "d66cd8ab656780f62c4215b903a420eb86358957", "shasum": "" }, "require": { - "php": ">=8.0.2", + "php": ">=8.1", "psr/container": "^2.0" }, "conflict": { @@ -2769,7 +2837,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "3.0-dev" + "dev-main": "3.1-dev" }, "thanks": { "name": "symfony/contracts", @@ -2779,7 +2847,10 @@ "autoload": { "psr-4": { "Symfony\\Contracts\\Service\\": "" - } + }, + "exclude-from-classmap": [ + "/Test/" + ] }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -2806,7 +2877,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/service-contracts/tree/v3.0.1" + "source": "https://github.com/symfony/service-contracts/tree/v3.1.0" }, "funding": [ { @@ -2822,24 +2893,24 @@ "type": "tidelift" } ], - "time": "2022-03-13T20:10:05+00:00" + "time": "2022-05-07T08:07:09+00:00" }, { "name": "symfony/string", - "version": "v6.0.8", + "version": "v6.1.0", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "ac0aa5c2282e0de624c175b68d13f2c8f2e2649d" + "reference": "d3edc75baf9f1d4f94879764dda2e1ac33499529" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/ac0aa5c2282e0de624c175b68d13f2c8f2e2649d", - "reference": "ac0aa5c2282e0de624c175b68d13f2c8f2e2649d", + "url": "https://api.github.com/repos/symfony/string/zipball/d3edc75baf9f1d4f94879764dda2e1ac33499529", + "reference": "d3edc75baf9f1d4f94879764dda2e1ac33499529", "shasum": "" }, "require": { - "php": ">=8.0.2", + "php": ">=8.1", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-intl-grapheme": "~1.0", "symfony/polyfill-intl-normalizer": "~1.0", @@ -2891,7 +2962,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v6.0.8" + "source": "https://github.com/symfony/string/tree/v6.1.0" }, "funding": [ { @@ -2907,7 +2978,7 @@ "type": "tidelift" } ], - "time": "2022-04-22T08:18:02+00:00" + "time": "2022-04-22T08:18:23+00:00" }, { "name": "theseer/tokenizer", @@ -2961,21 +3032,21 @@ }, { "name": "webmozart/assert", - "version": "1.10.0", + "version": "1.11.0", "source": { "type": "git", "url": "https://github.com/webmozarts/assert.git", - "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25" + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozarts/assert/zipball/6964c76c7804814a842473e0c8fd15bab0f18e25", - "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", "shasum": "" }, "require": { - "php": "^7.2 || ^8.0", - "symfony/polyfill-ctype": "^1.8" + "ext-ctype": "*", + "php": "^7.2 || ^8.0" }, "conflict": { "phpstan/phpstan": "<0.12.20", @@ -3013,9 +3084,9 @@ ], "support": { "issues": "https://github.com/webmozarts/assert/issues", - "source": "https://github.com/webmozarts/assert/tree/1.10.0" + "source": "https://github.com/webmozarts/assert/tree/1.11.0" }, - "time": "2021-03-09T10:59:23+00:00" + "time": "2022-06-03T18:03:27+00:00" } ], "aliases": [], @@ -3030,5 +3101,5 @@ "ext-json": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/src/SDK/Language/PHP.php b/src/SDK/Language/PHP.php index d4d2926d9..f9a5be25d 100644 --- a/src/SDK/Language/PHP.php +++ b/src/SDK/Language/PHP.php @@ -228,7 +228,7 @@ public function getTypeName($type) $type = 'array'; break; case self::TYPE_FILE: - $type = 'string'; + $type = 'InputFile'; break; } diff --git a/templates/php/src/InputFile.php.twig b/templates/php/src/InputFile.php.twig index a3a71fb76..29f85b4f9 100644 --- a/templates/php/src/InputFile.php.twig +++ b/templates/php/src/InputFile.php.twig @@ -1,5 +1,5 @@ filename; } - public static function withPath(string $path, ?string $mimeType, ?string $filename) + public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null) { $instance = new InputFile(); $instance->path = $path; $instance->mimeType = $mimeType; $instance->filename = $filename; + return $instance; } - public static function withData(string $data, ?string $mimeType, ?string $filename) + public static function withData(string $data, ?string $mimeType = null, ?string $filename = null) { $instance = new InputFile(); $instance->data = $data; $instance->mimeType = $mimeType; $instance->filename = $filename; + return $instance; } } \ No newline at end of file diff --git a/templates/php/src/Services/Service.php.twig b/templates/php/src/Services/Service.php.twig index 7e4d6ee02..6fbea7e8d 100644 --- a/templates/php/src/Services/Service.php.twig +++ b/templates/php/src/Services/Service.php.twig @@ -5,6 +5,7 @@ namespace {{ spec.title | caseUcfirst }}\Services; use {{ spec.title | caseUcfirst }}\{{spec.title | caseUcfirst}}Exception; use {{ spec.title | caseUcfirst }}\Client; use {{ spec.title | caseUcfirst }}\Service; +use {{ spec.title | caseUcfirst }}\InputFile; class {{ service.name | caseUcfirst }} extends Service { @@ -58,20 +59,40 @@ class {{ service.name | caseUcfirst }} extends Service {% if 'multipart/form-data' in method.consumes %} {% for parameter in method.parameters.all %} {% if parameter.type == 'file' %} - $size = filesize(${{ parameter.name | caseCamel }}); - $mimeType = mime_content_type(${{ parameter.name | caseCamel }}); - $postedName = basename(${{ parameter.name | caseCamel }}); - //send single file if size is less than or equal to 5MB - if ($size <= Client::CHUNK_SIZE) { - $params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}, $mimeType, $postedName); - return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [ -{% for param in method.parameters.header %} - '{{ param.name }}' => ${{ param.name | caseCamel }}, -{% endfor %} -{% for key, header in method.headers %} - '{{ key }}' => '{{ header }}', -{% endfor %} - ], $params); + $size = 0; + $mimeType = null; + $postedName = null; + if(empty(${{ parameter.name | caseCamel }}->getPath())) { + $size = strlen(${{ parameter.name | caseCamel }}->getData()); + $mimeType = ${{ parameter.name | caseCamel }}->getMimeType(); + $postedName = ${{ parameter.name | caseCamel }}->getFilename(); + if ($size <= Client::CHUNK_SIZE) { + $params['{{ parameter.name | caseCamel }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(${{ parameter.name | caseCamel }}->getData()), $mimeType, $postedName); + return $this->client->call(Client::METHOD_POST, $path, [ + {% for param in method.parameters.header %} + '{{ param.name }}' => ${{ param.name | caseCamel }}, + {% endfor %} + {% for key, header in method.headers %} + '{{ key }}' => '{{ header }}', + {% endfor %} + ], $params); + } + } else { + $size = filesize(${{ parameter.name | caseCamel }}->getPath()); + $mimeType = ${{ parameter.name | caseCamel }}->getMimeType() ?? mime_content_type(${{ parameter.name | caseCamel }}->getPath()); + $postedName = ${{ parameter.name | caseCamel }}->getFilename() ?? basename(${{ parameter.name | caseCamel }}->getPath()); + //send single file if size is less than or equal to 5MB + if ($size <= Client::CHUNK_SIZE) { + $params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}->getPath(), $mimeType, $postedName); + return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [ + {% for param in method.parameters.header %} + '{{ param.name }}' => ${{ param.name | caseCamel }}, + {% endfor %} + {% for key, header in method.headers %} + '{{ key }}' => '{{ header }}', + {% endfor %} + ], $params); + } } $id = ''; @@ -79,22 +100,33 @@ class {{ service.name | caseUcfirst }} extends Service {% for parameter in method.parameters.all %} {% if parameter.isUploadID %} - if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { - try { - $response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . {{ parameter.name }}), headers); - $counter = $response['chunksUploaded'] ?? 0; - } catch(\Exception $e) { - } + if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { + try { + $response = $this->client->call(Client::METHOD_GET, $path . '/' . ${{ parameter.name }},); + $counter = $response['chunksUploaded'] ?? 0; + } catch(\Exception $e) { } + } {% endif %} {% endfor %} $headers = ['content-type' => 'multipart/form-data']; - $handle = @fopen(${{ parameter.name | caseCamel }}, "rb"); + $handle = null; + + if(!empty(${{parameter.name}}->getPath())) { + $handle = @fopen(${{parameter.name}}->getPath(), "rb"); + } + $start = $counter * Client::CHUNK_SIZE; while ($start < $size) { - fseek($handle, $start); - $params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName); + $chunk = ''; + if(!empty($handle)) { + fseek($handle, $start); + $chunk = @fread($handle, Client::CHUNK_SIZE); + } else { + $chunk = substr($file->getData(), $start, Client::CHUNK_SIZE); + } + $params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode($chunk), $mimeType, $postedName); $headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size; if(!empty($id)) { $headers['x-{{spec.title | caseLower }}-id'] = $id; @@ -115,7 +147,9 @@ class {{ service.name | caseUcfirst }} extends Service ]); } } - @fclose($handle); + if(!empty($handle)) { + @fclose($handle); + } return $response; {% endif %} {% endfor %} diff --git a/tests/languages/php/test.php b/tests/languages/php/test.php index f0dea23ba..614a3a5e5 100644 --- a/tests/languages/php/test.php +++ b/tests/languages/php/test.php @@ -2,6 +2,7 @@ include __DIR__ . '/../../sdks/php/src/Appwrite/Client.php'; include __DIR__ . '/../../sdks/php/src/Appwrite/Service.php'; +include __DIR__ . '/../../sdks/php/src/Appwrite/InputFile.php'; include __DIR__ . '/../../sdks/php/src/Appwrite/AppwriteException.php'; include __DIR__ . '/../../sdks/php/src/Appwrite/Services/Foo.php'; include __DIR__ . '/../../sdks/php/src/Appwrite/Services/Bar.php'; @@ -9,6 +10,7 @@ use Appwrite\AppwriteException; use Appwrite\Client; +use Appwrite\InputFile; use Appwrite\Services\Bar; use Appwrite\Services\Foo; use Appwrite\Services\General; @@ -59,10 +61,10 @@ $response = $general->redirect(); echo "{$response['result']}\n"; -$response = $general->upload('string', 123, ['string in array'], __DIR__ . '/../../resources/file.png'); +$response = $general->upload('string', 123, ['string in array'], InputFile::withPath(__DIR__ .'/../../resources/file.png')); echo "{$response['result']}\n"; -$response = $general->upload('string', 123, ['string in array'], __DIR__ . '/../../resources/large_file.mp4'); +$response = $general->upload('string', 123, ['string in array'], InputFile::withPath(__DIR__ .'/../../resources/large_file.mp4')); echo "{$response['result']}\n"; try { From 6dfebe2fb96f14ce47bcec8a3e9b46e14b830e07 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 12 Jun 2022 04:34:46 +0000 Subject: [PATCH 4/7] update php version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 451a7056c..99e74d250 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ os: linux language: php php: - - 8.0 + - 8.1 env: - SDK=Android11Java8 From d78d1e50a5136cd320d9df290c75baf9b38605fa Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jun 2022 08:01:33 +0000 Subject: [PATCH 5/7] remove extra comma --- templates/php/src/Services/Service.php.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/php/src/Services/Service.php.twig b/templates/php/src/Services/Service.php.twig index 6fbea7e8d..58fbd9b42 100644 --- a/templates/php/src/Services/Service.php.twig +++ b/templates/php/src/Services/Service.php.twig @@ -102,7 +102,7 @@ class {{ service.name | caseUcfirst }} extends Service {% if parameter.isUploadID %} if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { try { - $response = $this->client->call(Client::METHOD_GET, $path . '/' . ${{ parameter.name }},); + $response = $this->client->call(Client::METHOD_GET, $path . '/' . ${{ parameter.name }}); $counter = $response['chunksUploaded'] ?? 0; } catch(\Exception $e) { } From e7ef9bbf4b3862bbbaec9c3f616b6c5ea430a1ae Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jun 2022 08:33:33 +0000 Subject: [PATCH 6/7] fix --- templates/php/base/requests/file.twig | 85 ++++++++++++++++++--------- 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/templates/php/base/requests/file.twig b/templates/php/base/requests/file.twig index 6083a3de6..a68adae01 100644 --- a/templates/php/base/requests/file.twig +++ b/templates/php/base/requests/file.twig @@ -1,20 +1,39 @@ -{% if 'multipart/form-data' in method.consumes %} {% for parameter in method.parameters.all %} {% if parameter.type == 'file' %} - $size = filesize(${{ parameter.name | caseCamel }}); - $mimeType = mime_content_type(${{ parameter.name | caseCamel }}); - $postedName = basename(${{ parameter.name | caseCamel }}); - //send single file if size is less than or equal to 5MB - if ($size <= Client::CHUNK_SIZE) { - $params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}, $mimeType, $postedName); - return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [ -{% for param in method.parameters.header %} - '{{ param.name }}' => ${{ param.name | caseCamel }}, -{% endfor %} -{% for key, header in method.headers %} - '{{ key }}' => '{{ header }}', -{% endfor %} - ], $params); + $size = 0; + $mimeType = null; + $postedName = null; + if(empty(${{ parameter.name | caseCamel }}->getPath())) { + $size = strlen(${{ parameter.name | caseCamel }}->getData()); + $mimeType = ${{ parameter.name | caseCamel }}->getMimeType(); + $postedName = ${{ parameter.name | caseCamel }}->getFilename(); + if ($size <= Client::CHUNK_SIZE) { + $params['{{ parameter.name | caseCamel }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(${{ parameter.name | caseCamel }}->getData()), $mimeType, $postedName); + return $this->client->call(Client::METHOD_POST, $path, [ + {% for param in method.parameters.header %} + '{{ param.name }}' => ${{ param.name | caseCamel }}, + {% endfor %} + {% for key, header in method.headers %} + '{{ key }}' => '{{ header }}', + {% endfor %} + ], $params); + } + } else { + $size = filesize(${{ parameter.name | caseCamel }}->getPath()); + $mimeType = ${{ parameter.name | caseCamel }}->getMimeType() ?? mime_content_type(${{ parameter.name | caseCamel }}->getPath()); + $postedName = ${{ parameter.name | caseCamel }}->getFilename() ?? basename(${{ parameter.name | caseCamel }}->getPath()); + //send single file if size is less than or equal to 5MB + if ($size <= Client::CHUNK_SIZE) { + $params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}->getPath(), $mimeType, $postedName); + return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [ + {% for param in method.parameters.header %} + '{{ param.name }}' => ${{ param.name | caseCamel }}, + {% endfor %} + {% for key, header in method.headers %} + '{{ key }}' => '{{ header }}', + {% endfor %} + ], $params); + } } $id = ''; @@ -22,22 +41,33 @@ {% for parameter in method.parameters.all %} {% if parameter.isUploadID %} - if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { - try { - $response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . {{ parameter.name }}), headers); - $counter = $response['chunksUploaded'] ?? 0; - } catch(\Exception $e) { - } + if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') { + try { + $response = $this->client->call(Client::METHOD_GET, $path . '/' . ${{ parameter.name }}); + $counter = $response['chunksUploaded'] ?? 0; + } catch(\Exception $e) { } + } {% endif %} {% endfor %} $headers = ['content-type' => 'multipart/form-data']; - $handle = @fopen(${{ parameter.name | caseCamel }}, "rb"); + $handle = null; + + if(!empty(${{parameter.name}}->getPath())) { + $handle = @fopen(${{parameter.name}}->getPath(), "rb"); + } + $start = $counter * Client::CHUNK_SIZE; while ($start < $size) { - fseek($handle, $start); - $params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName); + $chunk = ''; + if(!empty($handle)) { + fseek($handle, $start); + $chunk = @fread($handle, Client::CHUNK_SIZE); + } else { + $chunk = substr($file->getData(), $start, Client::CHUNK_SIZE); + } + $params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode($chunk), $mimeType, $postedName); $headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size; if(!empty($id)) { $headers['x-{{spec.title | caseLower }}-id'] = $id; @@ -58,8 +88,9 @@ ]); } } - @fclose($handle); + if(!empty($handle)) { + @fclose($handle); + } return $response; {% endif %} -{% endfor %} -{% endif %} \ No newline at end of file +{% endfor %} \ No newline at end of file From ba1781fe3edc7e77cfbae6bec799ea0a90900a49 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 21 Jun 2022 08:48:15 +0000 Subject: [PATCH 7/7] extra tests with data --- templates/php/base/requests/file.twig | 2 +- templates/php/src/InputFile.php.twig | 2 ++ tests/PHP74Test.php | 2 ++ tests/PHP80Test.php | 2 ++ tests/languages/php/test.php | 8 ++++++++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/templates/php/base/requests/file.twig b/templates/php/base/requests/file.twig index a68adae01..7f9b2d5dc 100644 --- a/templates/php/base/requests/file.twig +++ b/templates/php/base/requests/file.twig @@ -3,7 +3,7 @@ $size = 0; $mimeType = null; $postedName = null; - if(empty(${{ parameter.name | caseCamel }}->getPath())) { + if(empty(${{ parameter.name | caseCamel }}->getPath() ?? null)) { $size = strlen(${{ parameter.name | caseCamel }}->getData()); $mimeType = ${{ parameter.name | caseCamel }}->getMimeType(); $postedName = ${{ parameter.name | caseCamel }}->getFilename(); diff --git a/templates/php/src/InputFile.php.twig b/templates/php/src/InputFile.php.twig index 29f85b4f9..a7822196b 100644 --- a/templates/php/src/InputFile.php.twig +++ b/templates/php/src/InputFile.php.twig @@ -33,6 +33,7 @@ class InputFile { { $instance = new InputFile(); $instance->path = $path; + $instance->data = null; $instance->mimeType = $mimeType; $instance->filename = $filename; return $instance; @@ -41,6 +42,7 @@ class InputFile { public static function withData(string $data, ?string $mimeType = null, ?string $filename = null) { $instance = new InputFile(); + $instance->path = null; $instance->data = $data; $instance->mimeType = $mimeType; $instance->filename = $filename; diff --git a/tests/PHP74Test.php b/tests/PHP74Test.php index 9b1946f65..22f3ef664 100644 --- a/tests/PHP74Test.php +++ b/tests/PHP74Test.php @@ -15,6 +15,8 @@ class PHP74Test extends Base ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, ...Base::LARGE_FILE_RESPONSES, + ...Base::LARGE_FILE_RESPONSES, + ...Base::LARGE_FILE_RESPONSES, ...Base::EXCEPTION_RESPONSES, ]; } diff --git a/tests/PHP80Test.php b/tests/PHP80Test.php index bbdf09e4e..d44a29af7 100644 --- a/tests/PHP80Test.php +++ b/tests/PHP80Test.php @@ -15,6 +15,8 @@ class PHP80Test extends Base ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, ...Base::LARGE_FILE_RESPONSES, + ...Base::LARGE_FILE_RESPONSES, + ...Base::LARGE_FILE_RESPONSES, ...Base::EXCEPTION_RESPONSES, ]; } diff --git a/tests/languages/php/test.php b/tests/languages/php/test.php index 614a3a5e5..5667b27a2 100644 --- a/tests/languages/php/test.php +++ b/tests/languages/php/test.php @@ -61,6 +61,14 @@ $response = $general->redirect(); echo "{$response['result']}\n"; +$data = file_get_contents(__DIR__ . '/../../resources/file.png'); +$response = $general->upload('string', 123, ['string in array'], InputFile::withData($data, 'image/png', 'file.png')); +echo "{$response['result']}\n"; + +$data = file_get_contents(__DIR__ . '/../../resources/large_file.mp4'); +$response = $general->upload('string', 123, ['string in array'], InputFile::withData($data, 'video/mp4', 'large_file.mp4')); +echo "{$response['result']}\n"; + $response = $general->upload('string', 123, ['string in array'], InputFile::withPath(__DIR__ .'/../../resources/file.png')); echo "{$response['result']}\n";