Skip to content

Commit 0add3b6

Browse files
committed
fix chunk upload for remaining SDKs
1 parent dee773a commit 0add3b6

File tree

9 files changed

+18
-18
lines changed

9 files changed

+18
-18
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Client @JvmOverloads constructor(
374374
responseType = Map::class.java,
375375
)
376376
val chunksUploaded = current["chunksUploaded"] as Long
377-
offset = (chunksUploaded * CHUNK_SIZE).coerceAtMost(size)
377+
offset = chunksUploaded * CHUNK_SIZE
378378
}
379379

380380
while (offset < size) {
@@ -385,7 +385,7 @@ class Client @JvmOverloads constructor(
385385
}
386386
"bytes" -> {
387387
val end = if (offset + CHUNK_SIZE < size) {
388-
offset + CHUNK_SIZE
388+
offset + CHUNK_SIZE - 1
389389
} else {
390390
size - 1
391391
}
@@ -405,7 +405,7 @@ class Client @JvmOverloads constructor(
405405
)
406406

407407
headers["Content-Range"] =
408-
"bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size)}/$size"
408+
"bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size - 1)}/$size"
409409

410410
result = call(
411411
method = "POST",

templates/cli/lib/commands/command.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
164164

165165
for (counter; counter < totalCounters; counter++) {
166166
const start = (counter * libClient.CHUNK_SIZE);
167-
const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size);
167+
const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size - 1);
168168

169169
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
170170

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class Client @JvmOverloads constructor(
333333
responseType = Map::class.java,
334334
)
335335
val chunksUploaded = current["chunksUploaded"] as Long
336-
offset = (chunksUploaded * CHUNK_SIZE).coerceAtMost(size)
336+
offset = chunksUploaded * CHUNK_SIZE
337337
}
338338

339339
while (offset < size) {
@@ -344,7 +344,7 @@ class Client @JvmOverloads constructor(
344344
}
345345
"bytes" -> {
346346
val end = if (offset + CHUNK_SIZE < size) {
347-
offset + CHUNK_SIZE
347+
offset + CHUNK_SIZE - 1
348348
} else {
349349
size - 1
350350
}
@@ -364,7 +364,7 @@ class Client @JvmOverloads constructor(
364364
)
365365

366366
headers["Content-Range"] =
367-
"bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size)}/$size"
367+
"bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size - 1)}/$size"
368368

369369
result = call(
370370
method = "POST",

templates/node/base/requests/file.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}
4141

4242
const start = currentChunkStart;
43-
const end = Math.min(((start + client.CHUNK_SIZE) - 1), size);
43+
const end = Math.min(((start + client.CHUNK_SIZE) - 1), size - 1);
4444

4545
if(!lastUpload || currentChunkStart !== 0) {
4646
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size;

templates/php/base/requests/file.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
$chunk = substr($file->getData(), $start, Client::CHUNK_SIZE);
6969
}
7070
$params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode($chunk), $mimeType, $postedName);
71-
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
71+
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size - 1) . '/' . $size;
7272
if(!empty($id)) {
7373
$headers['x-{{spec.title | caseLower }}-id'] = $id;
7474
}
@@ -81,7 +81,7 @@
8181
if($onProgress !== null) {
8282
$onProgress([
8383
'$id' => $response['$id'],
84-
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
84+
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE)), $size) / $size * 100,
8585
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
8686
'chunksTotal' => $response['chunksTotal'],
8787
'chunksUploaded' => $response['chunksUploaded'],

templates/python/package/client.py.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class Client:
157157
input_file.data = input[offset:end]
158158

159159
params[param_name] = input_file
160-
headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size)}/{size}'
160+
headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size - 1)}/{size}'
161161

162162
result = self.call(
163163
'post',
@@ -172,7 +172,7 @@ class Client:
172172
headers["x-{{ spec.title | caseLower }}-id"] = result["$id"]
173173

174174
if on_progress is not None:
175-
end = min((((counter * self._chunk_size) + self._chunk_size) - 1), size)
175+
end = min((((counter * self._chunk_size) + self._chunk_size) - 1), size - 1)
176176
on_progress({
177177
"$id": result["$id"],
178178
"progress": min(offset, size)/size * 100,

templates/ruby/lib/container/client.rb.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ module {{ spec.title | caseUcfirst }}
141141
params: {}
142142
)
143143
chunks_uploaded = current['chunksUploaded'].to_i
144-
offset = [size, (chunks_uploaded * @chunk_size)].min
144+
offset = chunks_uploaded * @chunk_size
145145
end
146146

147147
while offset < size
@@ -158,7 +158,7 @@ module {{ spec.title | caseUcfirst }}
158158
mime_type: input_file.mime_type
159159
)
160160

161-
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
161+
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size - 1].min}/#{size}"
162162

163163
result = call(
164164
method: 'POST',

templates/swift/Sources/Client.swift.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ open class Client {
379379
converter: { return $0 as! [String: Any] }
380380
)
381381
let chunksUploaded = map["chunksUploaded"] as! Int
382-
offset = min(size, (chunksUploaded * Client.chunkSize))
382+
offset = chunksUploaded * Client.chunkSize
383383
} catch {
384384
// File does not exist yet, swallow exception
385385
}
@@ -390,7 +390,7 @@ open class Client {
390390
?? (input.data as! ByteBuffer).getSlice(at: offset, length: Int(size - offset))
391391

392392
params[paramName] = InputFile.fromBuffer(slice!, filename: input.filename, mimeType: input.mimeType)
393-
headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size))/\(size)"
393+
headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size - 1))/\(size)"
394394

395395
result = try await call(
396396
method: "POST",

templates/web/src/services/template.ts.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
122122

123123
for (counter; counter < totalCounters; counter++) {
124124
const start = (counter * Service.CHUNK_SIZE);
125-
const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size);
125+
const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size - 1);
126126

127127
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size
128128

@@ -142,7 +142,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
142142
if (onProgress) {
143143
onProgress({
144144
$id: response.$id,
145-
progress: Math.min((counter + 1) * Service.CHUNK_SIZE - 1, size) / size * 100,
145+
progress: Math.min((counter + 1) * Service.CHUNK_SIZE, size) / size * 100,
146146
sizeUploaded: end,
147147
chunksTotal: response.chunksTotal,
148148
chunksUploaded: response.chunksUploaded

0 commit comments

Comments
 (0)