Skip to content

Commit

Permalink
Merge pull request #1029 from appwrite/fix-file-ids
Browse files Browse the repository at this point in the history
fix: file ids
  • Loading branch information
christyjacob4 authored Jan 24, 2025
2 parents f241c8a + 4cc0897 commit 52fa4ef
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 89 deletions.
38 changes: 19 additions & 19 deletions templates/android/library/src/main/java/io/package/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Client @JvmOverloads constructor(
internal lateinit var http: OkHttpClient

internal val headers: MutableMap<String, String>

val config: MutableMap<String, String>

internal val cookieJar = ListenableCookieJar(CookieManager(
Expand All @@ -87,14 +87,14 @@ class Client @JvmOverloads constructor(
"x-sdk-platform" to "{{ sdk.platform }}",
"x-sdk-language" to "{{ language.name | caseLower }}",
"x-sdk-version" to "{{ sdk.version }}"{% if spec.global.defaultHeaders | length > 0 %},{% endif %}

{% for key,header in spec.global.defaultHeaders %}
"{{ key | caseLower }}" to "{{ header }}"{% if not loop.last %},{% endif %}
{% endfor %}

)
config = mutableMapOf()

setSelfSigned(selfSigned)
}

Expand All @@ -119,10 +119,10 @@ class Client @JvmOverloads constructor(
{% endfor %}
/**
* Set self Signed
*
*
* @param status
*
* @return this
* @return this
*/
fun setSelfSigned(status: Boolean): Client {
selfSigned = status
Expand Down Expand Up @@ -171,10 +171,10 @@ class Client @JvmOverloads constructor(

/**
* Set endpoint and realtime endpoint.
*
*
* @param endpoint
*
* @return this
* @return this
*/
fun setEndpoint(endpoint: String): Client {
this.endpoint = endpoint
Expand All @@ -200,11 +200,11 @@ class Client @JvmOverloads constructor(

/**
* Add Header
*
*
* @param key
* @param value
*
* @return this
* @return this
*/
fun addHeader(key: String, value: String): Client {
headers[key] = value
Expand Down Expand Up @@ -232,19 +232,19 @@ class Client @JvmOverloads constructor(

/**
* Send the HTTP request
*
*
* @param method
* @param path
* @param headers
* @param params
*
* @return [T]
* @return [T]
*/
@Throws({{ spec.title | caseUcfirst }}Exception::class)
suspend fun <T> call(
method: String,
path: String,
headers: Map<String, String> = mapOf(),
method: String,
path: String,
headers: Map<String, String> = mapOf(),
params: Map<String, Any?> = mapOf(),
responseType: Class<T>,
converter: ((Any) -> T)? = null
Expand Down Expand Up @@ -382,7 +382,7 @@ class Client @JvmOverloads constructor(
var offset = 0L
var result: Map<*, *>? = null

if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
if (idParamName?.isNotEmpty() == true) {
// Make a request to check if a file already exists
val current = call(
method = "GET",
Expand Down Expand Up @@ -479,14 +479,14 @@ class Client @JvmOverloads constructor(
.charStream()
.buffered()
.use(BufferedReader::readText)

val error = if (response.headers["content-type"]?.contains("application/json") == true) {
val map = body.fromJson<Map<String, Any>>()

{{ spec.title | caseUcfirst }}Exception(
map["message"] as? String ?: "",
map["message"] as? String ?: "",
(map["code"] as Number).toInt(),
map["type"] as? String ?: "",
map["type"] as? String ?: "",
body
)
} else {
Expand Down Expand Up @@ -546,4 +546,4 @@ class Client @JvmOverloads constructor(
}
})
}
}
}
2 changes: 1 addition & 1 deletion templates/apple/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ open class Client {
var offset = 0
var result = [String:Any]()

if idParamName != nil && params[idParamName!] as! String != "unique()" {
if idParamName != nil {
// Make a request to check if a file already exists
do {
let map = try await call(
Expand Down
2 changes: 1 addition & 1 deletion templates/dart/lib/src/client_browser.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ClientBrowser extends ClientBase with ClientMixin {
}

var offset = 0;
if (idParamName.isNotEmpty && params[idParamName] != 'unique()') {
if (idParamName.isNotEmpty) {
//make a request to check if a file already exists
try {
res = await call(
Expand Down
2 changes: 1 addition & 1 deletion templates/dart/lib/src/client_io.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ClientIO extends ClientBase with ClientMixin {
}

var offset = 0;
if (idParamName.isNotEmpty && params[idParamName] != 'unique()') {
if (idParamName.isNotEmpty) {
//make a request to check if a file already exists
try {
res = await call(
Expand Down
24 changes: 11 additions & 13 deletions templates/deno/src/services/service.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
{% if parameter.type == 'file' %}

const size = {{ parameter.name | caseCamel | escapeKeyword }}.size;

const apiHeaders: { [header: string]: string } = {
{% for parameter in method.parameters.header %}
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
Expand All @@ -125,16 +125,14 @@ export class {{ service.name | caseUcfirst }} extends Service {

{% for parameter in method.parameters.all %}
{% if parameter.isUploadID %}
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
try {
response = await this.client.call(
'get',
apiPath + '/' + {{ parameter.name }},
apiHeaders
);
chunksUploaded = response.chunksUploaded;
} catch(e) {
}
try {
response = await this.client.call(
'get',
apiPath + '/' + {{ parameter.name }},
apiHeaders
);
chunksUploaded = response.chunksUploaded;
} catch(e) {
}
{% endif %}
{% endfor %}
Expand All @@ -160,7 +158,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
}

let uploadableChunkTrimmed: Uint8Array;

if(currentPosition + 1 >= Client.CHUNK_SIZE) {
uploadableChunkTrimmed = uploadableChunk;
} else {
Expand Down Expand Up @@ -241,4 +239,4 @@ export class {{ service.name | caseUcfirst }} extends Service {
{% endif %}
}
{% endfor %}
}
}
2 changes: 1 addition & 1 deletion templates/dotnet/Package/Client.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ namespace {{ spec.title | caseUcfirst }}
);
}

if (!string.IsNullOrEmpty(idParamName) && (string)parameters[idParamName] != "unique()")
if (!string.IsNullOrEmpty(idParamName))
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/lib/src/client_browser.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ClientBrowser extends ClientBase with ClientMixin {
}

var offset = 0;
if (idParamName.isNotEmpty && params[idParamName] != 'unique()') {
if (idParamName.isNotEmpty) {
//make a request to check if a file already exists
try {
res = await call(
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/lib/src/client_io.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class ClientIO extends ClientBase with ClientMixin {
}

var offset = 0;
if (idParamName.isNotEmpty && params[idParamName] != 'unique()') {
if (idParamName.isNotEmpty) {
//make a request to check if a file already exists
try {
res = await call(
Expand Down
6 changes: 3 additions & 3 deletions templates/go/client.go.twig
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ func (client *Client) FileUpload(url string, headers map[string]interface{}, par
numChunks++
}
var currentChunk int64 = 0
if uploadId != "" && uploadId != "unique()" {
if uploadId != "" {
resp, err := client.Call("GET", url+"/"+uploadId, nil, nil)
if err == nil {
currentChunk = int64(resp.Result.(map[string]interface{})["chunksUploaded"].(float64))
}
}

if fileInfo.Size() <= client.ChunkSize {
if uploadId != "" && uploadId != "unique()" {
if uploadId != "" {
headers["x-appwrite-id"] = uploadId
}
inputFile.Data = make([]byte, fileInfo.Size())
Expand Down Expand Up @@ -203,7 +203,7 @@ func (client *Client) FileUpload(url string, headers map[string]interface{}, par
return nil, err
}
params[paramName] = inputFile
if uploadId != "" && uploadId != "unique()" {
if uploadId != "" {
headers["x-appwrite-id"] = uploadId
}
totalSize := fileInfo.Size()
Expand Down
24 changes: 12 additions & 12 deletions templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Client @JvmOverloads constructor(

/**
* Prepare the HTTP request
*
*
* @param method
* @param path
* @param headers
Expand Down Expand Up @@ -286,7 +286,7 @@ class Client @JvmOverloads constructor(
* @param headers
* @param params
*
* @return [T]
* @return [T]
*/
@Throws({{ spec.title | caseUcfirst }}Exception::class)
suspend fun <T> call(
Expand All @@ -309,7 +309,7 @@ class Client @JvmOverloads constructor(
* @param headers
* @param params
*
* @return [T]
* @return [T]
*/
@Throws({{ spec.title | caseUcfirst }}Exception::class)
suspend fun redirect(
Expand Down Expand Up @@ -381,7 +381,7 @@ class Client @JvmOverloads constructor(
var offset = 0L
var result: Map<*, *>? = null

if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
if (idParamName?.isNotEmpty() == true) {
// Make a request to check if a file already exists
val current = call(
method = "GET",
Expand Down Expand Up @@ -448,7 +448,7 @@ class Client @JvmOverloads constructor(
return converter(result as Map<String, Any>)
}

/**
/**
* Await Redirect
*
* @param request
Expand All @@ -475,14 +475,14 @@ class Client @JvmOverloads constructor(
.charStream()
.buffered()
.use(BufferedReader::readText)

val error = if (response.headers["content-type"]?.contains("application/json") == true) {
val map = body.fromJson<Map<String, Any>>()

{{ spec.title | caseUcfirst }}Exception(
map["message"] as? String ?: "",
map["message"] as? String ?: "",
(map["code"] as Number).toInt(),
map["type"] as? String ?: "",
map["type"] as? String ?: "",
body
)
} else {
Expand Down Expand Up @@ -526,14 +526,14 @@ class Client @JvmOverloads constructor(
.charStream()
.buffered()
.use(BufferedReader::readText)

val error = if (response.headers["content-type"]?.contains("application/json") == true) {
val map = body.fromJson<Map<String, Any>>()

{{ spec.title | caseUcfirst }}Exception(
map["message"] as? String ?: "",
map["message"] as? String ?: "",
(map["code"] as Number).toInt(),
map["type"] as? String ?: "",
map["type"] as? String ?: "",
body
)
} else {
Expand Down Expand Up @@ -591,4 +591,4 @@ class Client @JvmOverloads constructor(
}
})
}
}
}
14 changes: 6 additions & 8 deletions templates/php/base/requests/file.twig
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@

{% for parameter in method.parameters.all %}
{% if parameter.isUploadID %}
if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
try {
$response = $this->client->call(Client::METHOD_GET, $apiPath . '/' . ${{ parameter.name }});
$counter = $response['chunksUploaded'] ?? 0;
} catch(\Exception $e) {
}
try {
$response = $this->client->call(Client::METHOD_GET, $apiPath . '/' . ${{ parameter.name }});
$counter = $response['chunksUploaded'] ?? 0;
} catch(\Exception $e) {
}
{% endif %}
{% endfor %}
Expand Down Expand Up @@ -84,7 +82,7 @@
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE)), $size) / $size * 100,
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
'chunksTotal' => $response['chunksTotal'],
'chunksUploaded' => $response['chunksUploaded'],
'chunksUploaded' => $response['chunksUploaded'],
]);
}
}
Expand All @@ -93,4 +91,4 @@
}
return $response;
{% endif %}
{% endfor %}
{% endfor %}
Loading

0 comments on commit 52fa4ef

Please sign in to comment.