Skip to content

Commit 836af1d

Browse files
committed
chore: fix cli failure
1 parent df01de0 commit 836af1d

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
136136

137137
} else {
138138
const streamFilePath = payload['{{ parameter.name }}'];
139-
let id = undefined;
140-
141-
let counter = 0;
142-
const totalCounters = Math.ceil(size / libClient.CHUNK_SIZE);
143139

144140
const apiHeaders = {
145141
{% for parameter in method.parameters.header %}
@@ -150,49 +146,44 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
150146
{% endfor %}
151147
};
152148

149+
let offset = 0;
153150
{% for parameter in method.parameters.all %}
154151
{% if parameter.isUploadID %}
155152
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
156153
try {
157154
response = await client.call('get', apiPath + '/' + {{ parameter.name }}, apiHeaders);
158-
counter = response.chunksUploaded;
155+
offset = response.chunksUploaded * libClient.CHUNK_SIZE;
159156
} catch(e) {
160157
}
161158
}
162159
{% endif %}
163160
{% endfor %}
164161

165-
for (counter; counter < totalCounters; counter++) {
166-
const start = (counter * libClient.CHUNK_SIZE);
167-
const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size - 1);
168-
169-
apiHeaders['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
162+
while (offset < size) {
163+
let end = Math.min(offset + libClient.CHUNK_SIZE - 1, size - 1);
170164

171-
if (id) {
172-
apiHeaders['x-appwrite-id'] = id;
165+
apiHeaders['content-range'] = 'bytes ' + offset + '-' + end + '/' + size;
166+
if (response && response.$id) {
167+
apiHeaders['x-{{spec.title | caseLower }}-id'] = response.$id;
173168
}
174169

175170
const stream = fs.createReadStream(streamFilePath, {
176-
start,
171+
start: offset,
177172
end
178173
});
179174
payload['{{ parameter.name }}'] = stream;
180-
181175
response = await client.call('{{ method.method | caseLower }}', apiPath, apiHeaders, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});
182176

183-
if (!id) {
184-
id = response['$id'];
185-
}
186-
187-
if (onProgress !== null) {
177+
if (onProgress) {
188178
onProgress({
189-
$id: response['$id'],
190-
progress: Math.min((counter+1) * libClient.CHUNK_SIZE, size) / size * 100,
191-
sizeUploaded: end+1,
192-
chunksTotal: response['chunksTotal'],
193-
chunksUploaded: response['chunksUploaded']
179+
$id: response.$id,
180+
progress: ( offset / size ) * 100,
181+
sizeUploaded: offset,
182+
chunksTotal: response.chunksTotal,
183+
chunksUploaded: response.chunksUploaded
194184
});
195185
}
186+
offset += libClient.CHUNK_SIZE;
196187
}
197188
}
198189
{% endif %}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,25 @@ export class {{ service.name | caseUcfirst }} extends Service {
120120
while (offset < size) {
121121
let end = Math.min(offset + Service.CHUNK_SIZE - 1, size - 1);
122122

123-
const chunk = {{ parameter.name | caseCamel | escapeKeyword }}.slice(offset, end + 1);
124-
payload['{{ parameter.name }}'] = new File([chunk], {{ parameter.name | caseCamel | escapeKeyword }}.name);
125123
apiHeaders['content-range'] = 'bytes ' + offset + '-' + end + '/' + size;
126-
response = await this.client.call('{{ method.method | caseLower }}', uri, apiHeaders, payload);
127-
128-
offset += Service.CHUNK_SIZE;
129-
if (offset < size) {
124+
if (response && response.$id) {
130125
apiHeaders['x-{{spec.title | caseLower }}-id'] = response.$id;
131126
}
132127

128+
const chunk = {{ parameter.name | caseCamel | escapeKeyword }}.slice(offset, end + 1);
129+
payload['{{ parameter.name }}'] = new File([chunk], {{ parameter.name | caseCamel | escapeKeyword }}.name);
130+
response = await this.client.call('{{ method.method | caseLower }}', uri, apiHeaders, payload);
131+
133132
if (onProgress) {
134133
onProgress({
135134
$id: response.$id,
136-
progress: Math.min(offset, size) / size * 100,
137-
sizeUploaded: Math.min(offset, size),
135+
progress: (offset / size) * 100,
136+
sizeUploaded: offset,
138137
chunksTotal: response.chunksTotal,
139138
chunksUploaded: response.chunksUploaded
140139
});
141140
}
141+
offset += Service.CHUNK_SIZE;
142142
}
143143
return response;
144144
{% endif %}

0 commit comments

Comments
 (0)