-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #463 from appwrite/feat-refactor-templates
- Loading branch information
Showing
4 changed files
with
103 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [ | ||
{% for parameter in method.parameters.header %} | ||
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }}, | ||
{% endfor %} | ||
{% for key, header in method.headers %} | ||
'{{ key }}' => '{{ header }}', | ||
{% endfor %} | ||
], $params); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{% 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); | ||
} | ||
|
||
$id = ''; | ||
$counter = 0; | ||
|
||
{% 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) { | ||
} | ||
} | ||
{% endif %} | ||
{% endfor %} | ||
|
||
$headers = ['content-type' => 'multipart/form-data']; | ||
$handle = @fopen(${{ parameter.name | caseCamel }}, "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); | ||
$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; | ||
} | ||
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params); | ||
$counter++; | ||
$start += Client::CHUNK_SIZE; | ||
if(empty($id)) { | ||
$id = $response['$id']; | ||
} | ||
if($onProgress !== null) { | ||
$onProgress([ | ||
'$id' => $response['$id'], | ||
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100, | ||
'sizeUploaded' => min($counter * Client::CHUNK_SIZE), | ||
'chunksTotal' => $response['chunksTotal'], | ||
'chunksUploaded' => $response['chunksUploaded'], | ||
]); | ||
} | ||
} | ||
@fclose($handle); | ||
return $response; | ||
{% endif %} | ||
{% endfor %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
$params = []; | ||
{% if method.parameters.all | length %} | ||
{% for parameter in method.parameters.all %} | ||
{% if parameter.required %} | ||
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) { | ||
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"'); | ||
} | ||
{% endif %} | ||
{% endfor %} | ||
{% for parameter in method.parameters.query %} | ||
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) { | ||
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; | ||
} | ||
|
||
{% endfor %} | ||
{% for parameter in method.parameters.body %} | ||
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) { | ||
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; | ||
} | ||
|
||
{% endfor %} | ||
{% for parameter in method.parameters.formData %} | ||
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) { | ||
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; | ||
} | ||
{% endfor %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters