Skip to content

Commit aff50d2

Browse files
committed
Fix algo for chunk upload
1 parent 81d5863 commit aff50d2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

templates/deno/src/services/service.ts.twig

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ export class {{ service.name | caseUcfirst }} extends Service {
135135
headers['x-{{spec.title | caseLower }}-id'] = id;
136136
}
137137

138-
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
138+
let totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
139+
let lastBufferIndex = -1;
139140

140141
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
141142
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
@@ -148,11 +149,21 @@ export class {{ service.name | caseUcfirst }} extends Service {
148149

149150
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
150151
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
152+
153+
if(buf[byteIndex] !== 0) {
154+
lastBufferIndex = (blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex;
155+
}
151156
}
152157
}
153158

154159
// Shrink empty bytes
155-
totalBuffer = new Uint8Array(totalBuffer.buffer, 0);
160+
if(lastBufferIndex !== -1) {
161+
const newTotalBuffer = new Uint8Array(lastBufferIndex + 1);
162+
for(let index = 0; index <= lastBufferIndex; index++) {
163+
newTotalBuffer[index] = totalBuffer[index];
164+
}
165+
totalBuffer = newTotalBuffer;
166+
}
156167

157168
payload['{{ parameter.name }}'] = new File([totalBuffer], basename({{ parameter.name | caseCamel | escapeKeyword }}));
158169

0 commit comments

Comments
 (0)