@@ -136,10 +136,6 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
136
136
137
137
} else {
138
138
const streamFilePath = payload['{{ parameter .name }}'];
139
- let id = undefined;
140
-
141
- let counter = 0;
142
- const totalCounters = Math.ceil(size / libClient.CHUNK_SIZE);
143
139
144
140
const apiHeaders = {
145
141
{% for parameter in method .parameters .header %}
@@ -150,49 +146,44 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
150
146
{% endfor %}
151
147
};
152
148
149
+ let offset = 0;
153
150
{% for parameter in method .parameters .all %}
154
151
{% if parameter .isUploadID %}
155
152
if({{ parameter .name | caseCamel | escapeKeyword }} != 'unique()') {
156
153
try {
157
154
response = await client.call('get', apiPath + '/' + {{ parameter .name }}, apiHeaders);
158
- counter = response.chunksUploaded;
155
+ offset = response.chunksUploaded * libClient.CHUNK_SIZE ;
159
156
} catch(e) {
160
157
}
161
158
}
162
159
{% endif %}
163
160
{% endfor %}
164
161
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);
170
164
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;
173
168
}
174
169
175
170
const stream = fs.createReadStream(streamFilePath, {
176
- start,
171
+ start: offset ,
177
172
end
178
173
});
179
174
payload['{{ parameter .name }}'] = stream;
180
-
181
175
response = await client.call('{{ method .method | caseLower }}', apiPath, apiHeaders, payload{% if method .type == ' location' %}, 'arraybuffer'{% endif %});
182
176
183
- if (!id) {
184
- id = response['$id'];
185
- }
186
-
187
- if (onProgress !== null) {
177
+ if (onProgress) {
188
178
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
194
184
});
195
185
}
186
+ offset += libClient.CHUNK_SIZE;
196
187
}
197
188
}
198
189
{% endif %}
0 commit comments