From 398feb90a436c146af7bf61121f2505d42ef93ae Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 19 Apr 2023 05:05:31 +0000 Subject: [PATCH 1/3] fix chunk upload dart and flutter --- templates/dart/lib/src/client_browser.dart.twig | 10 +++++----- templates/dart/lib/src/client_io.dart.twig | 10 +++++----- templates/flutter/lib/src/client_browser.dart.twig | 10 +++++----- templates/flutter/lib/src/client_io.dart.twig | 10 +++++----- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/templates/dart/lib/src/client_browser.dart.twig b/templates/dart/lib/src/client_browser.dart.twig index 221f7b4e1..ed3d2bde7 100644 --- a/templates/dart/lib/src/client_browser.dart.twig +++ b/templates/dart/lib/src/client_browser.dart.twig @@ -114,18 +114,18 @@ class ClientBrowser extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size, chunksUploaded * CHUNK_SIZE); + offset = min(size - 1, chunksUploaded * CHUNK_SIZE); } on {{spec.title | caseUcfirst}}Exception catch (_) {} } while (offset < size) { List chunk; - final end = min(offset + CHUNK_SIZE, size); + final end = min(offset + CHUNK_SIZE, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min(((offset + CHUNK_SIZE) - 1), size)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -134,8 +134,8 @@ class ClientBrowser extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset - 1, size) / size * 100, - sizeUploaded: min(offset - 1, size), + progress: min(offset + 1, size) / size * 100, + sizeUploaded: min(offset + 1, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); diff --git a/templates/dart/lib/src/client_io.dart.twig b/templates/dart/lib/src/client_io.dart.twig index e93251525..f44c5985d 100644 --- a/templates/dart/lib/src/client_io.dart.twig +++ b/templates/dart/lib/src/client_io.dart.twig @@ -142,7 +142,7 @@ class ClientIO extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size, chunksUploaded * CHUNK_SIZE); + offset = min(size - 1, chunksUploaded * CHUNK_SIZE); } on {{spec.title | caseUcfirst}}Exception catch (_) {} } @@ -155,7 +155,7 @@ class ClientIO extends ClientBase with ClientMixin { while (offset < size) { List chunk = []; if (file.bytes != null) { - final end = min(offset + CHUNK_SIZE-1, size-1); + final end = min(offset + CHUNK_SIZE, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); } else { raf!.setPositionSync(offset); @@ -164,7 +164,7 @@ class ClientIO extends ClientBase with ClientMixin { params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min(((offset + CHUNK_SIZE) - 1), size)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -173,8 +173,8 @@ class ClientIO extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset - 1, size) / size * 100, - sizeUploaded: min(offset - 1, size), + progress: min(offset + 1, size) / size * 100, + sizeUploaded: min(offset + 1, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); diff --git a/templates/flutter/lib/src/client_browser.dart.twig b/templates/flutter/lib/src/client_browser.dart.twig index 948915c5f..1a0060e1f 100644 --- a/templates/flutter/lib/src/client_browser.dart.twig +++ b/templates/flutter/lib/src/client_browser.dart.twig @@ -143,18 +143,18 @@ class ClientBrowser extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size, chunksUploaded * CHUNK_SIZE); + offset = min(size - 1, chunksUploaded * CHUNK_SIZE); } on {{spec.title | caseUcfirst}}Exception catch (_) {} } while (offset < size) { var chunk; - final end = min(offset + CHUNK_SIZE, size); + final end = min(offset + CHUNK_SIZE, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min(((offset + CHUNK_SIZE) - 1), size)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -163,8 +163,8 @@ class ClientBrowser extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset - 1, size) / size * 100, - sizeUploaded: min(offset - 1, size), + progress: min(offset + 1, size) / size * 100, + sizeUploaded: min(offset + 1, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); diff --git a/templates/flutter/lib/src/client_io.dart.twig b/templates/flutter/lib/src/client_io.dart.twig index a200fcb49..0b72f51f7 100644 --- a/templates/flutter/lib/src/client_io.dart.twig +++ b/templates/flutter/lib/src/client_io.dart.twig @@ -263,7 +263,7 @@ class ClientIO extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size, chunksUploaded * CHUNK_SIZE); + offset = min(size - 1, chunksUploaded * CHUNK_SIZE); } on {{spec.title | caseUcfirst}}Exception catch (_) {} } @@ -276,7 +276,7 @@ class ClientIO extends ClientBase with ClientMixin { while (offset < size) { List chunk = []; if (file.bytes != null) { - final end = min(offset + CHUNK_SIZE-1, size-1); + final end = min(offset + CHUNK_SIZE, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); } else { raf!.setPositionSync(offset); @@ -285,7 +285,7 @@ class ClientIO extends ClientBase with ClientMixin { params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min(((offset + CHUNK_SIZE) - 1), size)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -294,8 +294,8 @@ class ClientIO extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset - 1, size) / size * 100, - sizeUploaded: min(offset - 1, size), + progress: min(offset + 1, size) / size * 100, + sizeUploaded: min(offset + 1, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); From dee773a7cc566d0e205bb938e490ea088197714b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 19 Apr 2023 05:33:18 +0000 Subject: [PATCH 2/3] fix dart and flutter chunk upload --- templates/dart/lib/src/client_browser.dart.twig | 12 ++++++------ templates/dart/lib/src/client_io.dart.twig | 10 +++++----- templates/flutter/lib/src/client_browser.dart.twig | 10 +++++----- templates/flutter/lib/src/client_io.dart.twig | 10 +++++----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/templates/dart/lib/src/client_browser.dart.twig b/templates/dart/lib/src/client_browser.dart.twig index ed3d2bde7..652bd5059 100644 --- a/templates/dart/lib/src/client_browser.dart.twig +++ b/templates/dart/lib/src/client_browser.dart.twig @@ -114,18 +114,18 @@ class ClientBrowser extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size - 1, chunksUploaded * CHUNK_SIZE); + offset = chunksUploaded * CHUNK_SIZE; } on {{spec.title | caseUcfirst}}Exception catch (_) {} } while (offset < size) { - List chunk; - final end = min(offset + CHUNK_SIZE, size - 1); + var chunk; + final end = min(offset + CHUNK_SIZE - 1, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE - 1), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -134,8 +134,8 @@ class ClientBrowser extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset + 1, size) / size * 100, - sizeUploaded: min(offset + 1, size), + progress: min(offset, size) / size * 100, + sizeUploaded: min(offset, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); diff --git a/templates/dart/lib/src/client_io.dart.twig b/templates/dart/lib/src/client_io.dart.twig index f44c5985d..fc5a441f5 100644 --- a/templates/dart/lib/src/client_io.dart.twig +++ b/templates/dart/lib/src/client_io.dart.twig @@ -142,7 +142,7 @@ class ClientIO extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size - 1, chunksUploaded * CHUNK_SIZE); + offset = chunksUploaded * CHUNK_SIZE; } on {{spec.title | caseUcfirst}}Exception catch (_) {} } @@ -155,7 +155,7 @@ class ClientIO extends ClientBase with ClientMixin { while (offset < size) { List chunk = []; if (file.bytes != null) { - final end = min(offset + CHUNK_SIZE, size - 1); + final end = min(offset + CHUNK_SIZE - 1, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); } else { raf!.setPositionSync(offset); @@ -164,7 +164,7 @@ class ClientIO extends ClientBase with ClientMixin { params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE - 1), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -173,8 +173,8 @@ class ClientIO extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset + 1, size) / size * 100, - sizeUploaded: min(offset + 1, size), + progress: min(offset, size) / size * 100, + sizeUploaded: min(offset, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); diff --git a/templates/flutter/lib/src/client_browser.dart.twig b/templates/flutter/lib/src/client_browser.dart.twig index 1a0060e1f..442a2fb02 100644 --- a/templates/flutter/lib/src/client_browser.dart.twig +++ b/templates/flutter/lib/src/client_browser.dart.twig @@ -143,18 +143,18 @@ class ClientBrowser extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size - 1, chunksUploaded * CHUNK_SIZE); + offset = chunksUploaded * CHUNK_SIZE; } on {{spec.title | caseUcfirst}}Exception catch (_) {} } while (offset < size) { var chunk; - final end = min(offset + CHUNK_SIZE, size - 1); + final end = min(offset + CHUNK_SIZE - 1, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE - 1), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -163,8 +163,8 @@ class ClientBrowser extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset + 1, size) / size * 100, - sizeUploaded: min(offset + 1, size), + progress: min(offset, size) / size * 100, + sizeUploaded: min(offset, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); diff --git a/templates/flutter/lib/src/client_io.dart.twig b/templates/flutter/lib/src/client_io.dart.twig index 0b72f51f7..4d17e99cb 100644 --- a/templates/flutter/lib/src/client_io.dart.twig +++ b/templates/flutter/lib/src/client_io.dart.twig @@ -263,7 +263,7 @@ class ClientIO extends ClientBase with ClientMixin { headers: headers, ); final int chunksUploaded = res.data['chunksUploaded'] as int; - offset = min(size - 1, chunksUploaded * CHUNK_SIZE); + offset = chunksUploaded * CHUNK_SIZE; } on {{spec.title | caseUcfirst}}Exception catch (_) {} } @@ -276,7 +276,7 @@ class ClientIO extends ClientBase with ClientMixin { while (offset < size) { List chunk = []; if (file.bytes != null) { - final end = min(offset + CHUNK_SIZE, size - 1); + final end = min(offset + CHUNK_SIZE - 1, size - 1); chunk = file.bytes!.getRange(offset, end).toList(); } else { raf!.setPositionSync(offset); @@ -285,7 +285,7 @@ class ClientIO extends ClientBase with ClientMixin { params[paramName] = http.MultipartFile.fromBytes(paramName, chunk, filename: file.filename); headers['content-range'] = - 'bytes $offset-${min((offset + CHUNK_SIZE), size - 1)}/$size'; + 'bytes $offset-${min((offset + CHUNK_SIZE - 1), size - 1)}/$size'; res = await call(HttpMethod.post, path: path, headers: headers, params: params); offset += CHUNK_SIZE; @@ -294,8 +294,8 @@ class ClientIO extends ClientBase with ClientMixin { } final progress = UploadProgress( $id: res.data['\$id'] ?? '', - progress: min(offset + 1, size) / size * 100, - sizeUploaded: min(offset + 1, size), + progress: min(offset, size) / size * 100, + sizeUploaded: min(offset, size), chunksTotal: res.data['chunksTotal'] ?? 0, chunksUploaded: res.data['chunksUploaded'] ?? 0, ); From 0add3b68be76747025c32c1e98b79cae5989f7bb Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 19 Apr 2023 06:07:40 +0000 Subject: [PATCH 3/3] fix chunk upload for remaining SDKs --- .../library/src/main/java/io/appwrite/Client.kt.twig | 6 +++--- templates/cli/lib/commands/command.js.twig | 2 +- templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig | 6 +++--- templates/node/base/requests/file.twig | 2 +- templates/php/base/requests/file.twig | 4 ++-- templates/python/package/client.py.twig | 4 ++-- templates/ruby/lib/container/client.rb.twig | 4 ++-- templates/swift/Sources/Client.swift.twig | 4 ++-- templates/web/src/services/template.ts.twig | 4 ++-- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/templates/android/library/src/main/java/io/appwrite/Client.kt.twig b/templates/android/library/src/main/java/io/appwrite/Client.kt.twig index 930b74edd..210ef4fbe 100644 --- a/templates/android/library/src/main/java/io/appwrite/Client.kt.twig +++ b/templates/android/library/src/main/java/io/appwrite/Client.kt.twig @@ -374,7 +374,7 @@ class Client @JvmOverloads constructor( responseType = Map::class.java, ) val chunksUploaded = current["chunksUploaded"] as Long - offset = (chunksUploaded * CHUNK_SIZE).coerceAtMost(size) + offset = chunksUploaded * CHUNK_SIZE } while (offset < size) { @@ -385,7 +385,7 @@ class Client @JvmOverloads constructor( } "bytes" -> { val end = if (offset + CHUNK_SIZE < size) { - offset + CHUNK_SIZE + offset + CHUNK_SIZE - 1 } else { size - 1 } @@ -405,7 +405,7 @@ class Client @JvmOverloads constructor( ) headers["Content-Range"] = - "bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size)}/$size" + "bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size - 1)}/$size" result = call( method = "POST", diff --git a/templates/cli/lib/commands/command.js.twig b/templates/cli/lib/commands/command.js.twig index 74d8a0c60..8cab04d1b 100644 --- a/templates/cli/lib/commands/command.js.twig +++ b/templates/cli/lib/commands/command.js.twig @@ -164,7 +164,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ { for (counter; counter < totalCounters; counter++) { const start = (counter * libClient.CHUNK_SIZE); - const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size); + const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size - 1); headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size; diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index 280c7407e..2f80907e5 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -333,7 +333,7 @@ class Client @JvmOverloads constructor( responseType = Map::class.java, ) val chunksUploaded = current["chunksUploaded"] as Long - offset = (chunksUploaded * CHUNK_SIZE).coerceAtMost(size) + offset = chunksUploaded * CHUNK_SIZE } while (offset < size) { @@ -344,7 +344,7 @@ class Client @JvmOverloads constructor( } "bytes" -> { val end = if (offset + CHUNK_SIZE < size) { - offset + CHUNK_SIZE + offset + CHUNK_SIZE - 1 } else { size - 1 } @@ -364,7 +364,7 @@ class Client @JvmOverloads constructor( ) headers["Content-Range"] = - "bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size)}/$size" + "bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size - 1)}/$size" result = call( method = "POST", diff --git a/templates/node/base/requests/file.twig b/templates/node/base/requests/file.twig index 533dc5d5b..6bae47dd1 100644 --- a/templates/node/base/requests/file.twig +++ b/templates/node/base/requests/file.twig @@ -40,7 +40,7 @@ } const start = currentChunkStart; - const end = Math.min(((start + client.CHUNK_SIZE) - 1), size); + const end = Math.min(((start + client.CHUNK_SIZE) - 1), size - 1); if(!lastUpload || currentChunkStart !== 0) { headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size; diff --git a/templates/php/base/requests/file.twig b/templates/php/base/requests/file.twig index 7f9b2d5dc..9e3031d89 100644 --- a/templates/php/base/requests/file.twig +++ b/templates/php/base/requests/file.twig @@ -68,7 +68,7 @@ $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; + $headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size - 1) . '/' . $size; if(!empty($id)) { $headers['x-{{spec.title | caseLower }}-id'] = $id; } @@ -81,7 +81,7 @@ if($onProgress !== null) { $onProgress([ '$id' => $response['$id'], - 'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100, + 'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE)), $size) / $size * 100, 'sizeUploaded' => min($counter * Client::CHUNK_SIZE), 'chunksTotal' => $response['chunksTotal'], 'chunksUploaded' => $response['chunksUploaded'], diff --git a/templates/python/package/client.py.twig b/templates/python/package/client.py.twig index 0d5b2fbb9..aa2827727 100644 --- a/templates/python/package/client.py.twig +++ b/templates/python/package/client.py.twig @@ -157,7 +157,7 @@ class Client: input_file.data = input[offset:end] params[param_name] = input_file - headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size)}/{size}' + headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size - 1)}/{size}' result = self.call( 'post', @@ -172,7 +172,7 @@ class Client: headers["x-{{ spec.title | caseLower }}-id"] = result["$id"] if on_progress is not None: - end = min((((counter * self._chunk_size) + self._chunk_size) - 1), size) + end = min((((counter * self._chunk_size) + self._chunk_size) - 1), size - 1) on_progress({ "$id": result["$id"], "progress": min(offset, size)/size * 100, diff --git a/templates/ruby/lib/container/client.rb.twig b/templates/ruby/lib/container/client.rb.twig index fc0762dc9..4b4892889 100644 --- a/templates/ruby/lib/container/client.rb.twig +++ b/templates/ruby/lib/container/client.rb.twig @@ -141,7 +141,7 @@ module {{ spec.title | caseUcfirst }} params: {} ) chunks_uploaded = current['chunksUploaded'].to_i - offset = [size, (chunks_uploaded * @chunk_size)].min + offset = chunks_uploaded * @chunk_size end while offset < size @@ -158,7 +158,7 @@ module {{ spec.title | caseUcfirst }} mime_type: input_file.mime_type ) - headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}" + headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size - 1].min}/#{size}" result = call( method: 'POST', diff --git a/templates/swift/Sources/Client.swift.twig b/templates/swift/Sources/Client.swift.twig index db36c208f..0ebecdf70 100644 --- a/templates/swift/Sources/Client.swift.twig +++ b/templates/swift/Sources/Client.swift.twig @@ -379,7 +379,7 @@ open class Client { converter: { return $0 as! [String: Any] } ) let chunksUploaded = map["chunksUploaded"] as! Int - offset = min(size, (chunksUploaded * Client.chunkSize)) + offset = chunksUploaded * Client.chunkSize } catch { // File does not exist yet, swallow exception } @@ -390,7 +390,7 @@ open class Client { ?? (input.data as! ByteBuffer).getSlice(at: offset, length: Int(size - offset)) params[paramName] = InputFile.fromBuffer(slice!, filename: input.filename, mimeType: input.mimeType) - headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size))/\(size)" + headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size - 1))/\(size)" result = try await call( method: "POST", diff --git a/templates/web/src/services/template.ts.twig b/templates/web/src/services/template.ts.twig index f1345d1e1..98b79986c 100644 --- a/templates/web/src/services/template.ts.twig +++ b/templates/web/src/services/template.ts.twig @@ -122,7 +122,7 @@ export class {{ service.name | caseUcfirst }} extends Service { for (counter; counter < totalCounters; counter++) { const start = (counter * Service.CHUNK_SIZE); - const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size); + const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size - 1); headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size @@ -142,7 +142,7 @@ export class {{ service.name | caseUcfirst }} extends Service { if (onProgress) { onProgress({ $id: response.$id, - progress: Math.min((counter + 1) * Service.CHUNK_SIZE - 1, size) / size * 100, + progress: Math.min((counter + 1) * Service.CHUNK_SIZE, size) / size * 100, sizeUploaded: end, chunksTotal: response.chunksTotal, chunksUploaded: response.chunksUploaded