Skip to content

Commit eb78d61

Browse files
Merge branch 'master' into feat-ruby-input-file
2 parents 63412a2 + 9a36431 commit eb78d61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+702
-179
lines changed

specs/swagger2-latest-console.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/SDK/Language/Android.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public function getFiles()
131131
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/json/PreciseNumberAdapter.kt',
132132
'template' => '/android/library/src/main/java/io/appwrite/json/PreciseNumberAdapter.kt.twig',
133133
],
134+
[
135+
'scope' => 'default',
136+
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/models/InputFile.kt',
137+
'template' => '/android/library/src/main/java/io/appwrite/models/InputFile.kt.twig',
138+
],
134139
[
135140
'scope' => 'default',
136141
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/models/RealtimeModels.kt',

src/SDK/Language/Kotlin.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getTypeName($type)
111111
case self::TYPE_STRING:
112112
return 'String';
113113
case self::TYPE_FILE:
114-
return 'File';
114+
return 'InputFile';
115115
case self::TYPE_BOOLEAN:
116116
return 'Boolean';
117117
case self::TYPE_ARRAY:
@@ -197,7 +197,7 @@ public function getParamExample(array $param)
197197
if(empty($example) && $example !== 0 && $example !== false) {
198198
switch ($type) {
199199
case self::TYPE_FILE:
200-
$output .= 'File("file.png")';
200+
$output .= 'InputFile.fromPath("file.png")';
201201
break;
202202
case self::TYPE_NUMBER:
203203
case self::TYPE_INTEGER:
@@ -390,6 +390,12 @@ public function getFiles()
390390
'template' => '/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig',
391391
'minify' => false,
392392
],
393+
[
394+
'scope' => 'default',
395+
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/InputFile.kt',
396+
'template' => '/kotlin/src/main/kotlin/io/appwrite/models/InputFile.kt.twig',
397+
'minify' => false,
398+
],
393399
[
394400
'scope' => 'default',
395401
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/UploadProgress.kt',

src/SDK/Language/Python.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function getParamExample(array $param)
302302
$output .= '{}';
303303
break;
304304
case self::TYPE_FILE:
305-
$output .= "'file.png'";
305+
$output .= "InputFile.from_path('file.png')";
306306
break;
307307
}
308308
}
@@ -321,7 +321,7 @@ public function getParamExample(array $param)
321321
$output .= "'{$example}'";
322322
break;
323323
case self::TYPE_FILE:
324-
$output .= "'file.png'";
324+
$output .= "InputFile.from_path('file.png')";
325325
break;
326326
}
327327
}

src/SDK/Language/Swift.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function getFiles()
155155
[
156156
'scope' => 'default',
157157
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Models/File.swift',
158-
'template' => 'swift/Sources/Models/File.swift.twig',
158+
'template' => 'swift/Sources/Models/InputFile.swift.twig',
159159
'minify' => false,
160160
],
161161
[
@@ -188,6 +188,12 @@ public function getFiles()
188188
'template' => 'swift/Sources/Extensions/HTTPClientRequest+Cookies.swift.twig',
189189
'minify' => false,
190190
],
191+
[
192+
'scope' => 'default',
193+
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Extensions/String+MimeTypes.swift',
194+
'template' => 'swift/Sources/Extensions/String+MimeTypes.swift.twig',
195+
'minify' => false,
196+
],
191197
[
192198
'scope' => 'default',
193199
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/StreamingDelegate.swift',
@@ -301,7 +307,7 @@ public function getTypeName($type)
301307
case self::TYPE_STRING:
302308
return 'String';
303309
case self::TYPE_FILE:
304-
return 'File';
310+
return 'InputFile';
305311
case self::TYPE_BOOLEAN:
306312
return 'Bool';
307313
case self::TYPE_ARRAY:
@@ -390,7 +396,7 @@ public function getParamExample(array $param)
390396
if(empty($example) && $example !== 0 && $example !== false) {
391397
switch ($type) {
392398
case self::TYPE_FILE:
393-
$output .= 'File(name: "image.jpg", buffer: yourByteBuffer)';
399+
$output .= 'InputFile.fromPath("file.png")';
394400
break;
395401
case self::TYPE_NUMBER:
396402
case self::TYPE_INTEGER:

src/SDK/SDK.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ public function generate($target)
584584
$params['service'] = [
585585
'name' => $key,
586586
'methods' => $methods,
587+
'globalParams' => $service['globalParams'] ?? [],
587588
'features' => [
588589
'upload' => $this->hasUploads($methods),
589590
'location' => $this->hasLocation($methods),

templates/android/docs/java/example.md.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public class MainActivity extends AppCompatActivity {
2222
{% endfor %}
2323
{% endif %}
2424

25-
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
25+
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %});
2626

27-
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}new Continuation<Object>() {
27+
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | filter((param) => not param.isGlobal) | length == 0 %}new Continuation<Object>() {
2828
@NotNull
2929
@Override
3030
public CoroutineContext getContext() {
@@ -53,7 +53,7 @@ public class MainActivity extends AppCompatActivity {
5353
{% if method.type == "webAuth" %}
5454
this,
5555
{% endif %}
56-
{% for parameter in method.parameters.all %}
56+
{% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}
5757
{% if parameter.required %}
5858
{{ parameter | paramExample }}{% if not loop.last %}, {% endif %}
5959

templates/android/docs/kotlin/example.md.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class MainActivity : AppCompatActivity() {
2020
{% endfor %}
2121
{% endif %}
2222

23-
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client)
23+
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %})
2424

2525
GlobalScope.launch {
26-
{% if method.type == 'webAuth' %} {% elseif method.type == 'location' %} val result = {% else %} val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
26+
{% if method.type == 'webAuth' %} {% elseif method.type == 'location' %} val result = {% else %} val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | filter((param) => not param.isGlobal) | length == 0 %}){% endif %}
2727

2828
{% if method.type == "webAuth" %}
2929
activity = this@MainActivity,
3030
{% endif %}
31-
{% for parameter in method.parameters.all %}
31+
{% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}
3232
{% if parameter.required %}
3333
{{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %}
3434

templates/android/library/src/main/java/io/appwrite/Client.kt.twig

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {{ sdk.namespace | caseDot }}.cookies.stores.SharedPreferencesCookieStore
99
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
1010
import {{ sdk.namespace | caseDot }}.extensions.fromJson
1111
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
12+
import {{ sdk.namespace | caseDot }}.models.InputFile
1213
import {{ sdk.namespace | caseDot }}.models.UploadProgress
1314
import kotlinx.coroutines.CoroutineScope
1415
import kotlinx.coroutines.Dispatchers
@@ -322,14 +323,29 @@ class Client @JvmOverloads constructor(
322323
idParamName: String? = null,
323324
onProgress: ((UploadProgress) -> Unit)? = null,
324325
): T {
325-
val file = params[paramName] as File
326-
val size = file.length()
326+
var file: RandomAccessFile? = null
327+
val input = params[paramName] as InputFile
328+
val size: Long = when(input.sourceType) {
329+
"path", "file" -> {
330+
file = RandomAccessFile(input.path, "r")
331+
file.length()
332+
}
333+
"bytes" -> {
334+
(input.data as ByteArray).size.toLong()
335+
}
336+
else -> throw UnsupportedOperationException()
337+
}
327338

328339
if (size < CHUNK_SIZE) {
340+
val data = when(input.sourceType) {
341+
"file", "path" -> File(input.path).asRequestBody()
342+
"bytes" -> (input.data as ByteArray).toRequestBody(input.mimeType.toMediaType())
343+
else -> throw UnsupportedOperationException()
344+
}
329345
params[paramName] = MultipartBody.Part.createFormData(
330346
paramName,
331-
file.name,
332-
file.asRequestBody()
347+
input.filename,
348+
data
333349
)
334350
return call(
335351
method = "POST",
@@ -341,7 +357,6 @@ class Client @JvmOverloads constructor(
341357
)
342358
}
343359

344-
val input = RandomAccessFile(file, "r")
345360
val buffer = ByteArray(CHUNK_SIZE)
346361
var offset = 0L
347362
var result: Map<*, *>? = null
@@ -360,12 +375,29 @@ class Client @JvmOverloads constructor(
360375
}
361376

362377
while (offset < size) {
363-
input.seek(offset)
364-
input.read(buffer)
378+
when(input.sourceType) {
379+
"file", "path" -> {
380+
file!!.seek(offset)
381+
file!!.read(buffer)
382+
}
383+
"bytes" -> {
384+
val end = if (offset + CHUNK_SIZE < size) {
385+
offset + CHUNK_SIZE
386+
} else {
387+
size - 1
388+
}
389+
(input.data as ByteArray).copyInto(
390+
buffer,
391+
startIndex = offset.toInt(),
392+
endIndex = end.toInt()
393+
)
394+
}
395+
else -> throw UnsupportedOperationException()
396+
}
365397

366398
params[paramName] = MultipartBody.Part.createFormData(
367399
paramName,
368-
file.name,
400+
input.filename,
369401
buffer.toRequestBody()
370402
)
371403

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package {{ sdk.namespace | caseDot }}.models
2+
3+
import java.io.File
4+
import java.net.URLConnection
5+
import java.nio.file.Files
6+
import java.nio.file.Paths
7+
8+
class InputFile private constructor() {
9+
10+
lateinit var path: String
11+
lateinit var filename: String
12+
lateinit var mimeType: String
13+
lateinit var sourceType: String
14+
lateinit var data: Any
15+
16+
companion object {
17+
fun fromFile(file: File) = InputFile().apply {
18+
path = file.canonicalPath
19+
filename = file.name
20+
mimeType = Files.probeContentType(Paths.get(file.canonicalPath))
21+
?: URLConnection.guessContentTypeFromName(filename)
22+
?: ""
23+
sourceType = "file"
24+
}
25+
26+
fun fromPath(path: String): InputFile = fromFile(File(path)).apply {
27+
sourceType = "path"
28+
}
29+
30+
fun fromBytes(bytes: ByteArray, filename: String = "", mimeType: String = "") = InputFile().apply {
31+
this.filename = filename
32+
this.mimeType = mimeType
33+
data = bytes
34+
sourceType = "bytes"
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)