@@ -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 %}
0 commit comments