Skip to content

Commit abae690

Browse files
authored
Merge pull request #399 from appwrite/feat-resumable-upload-deno
Feat: Deno resumable upload
2 parents fdb1b34 + e5cf2e0 commit abae690

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

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

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -101,58 +101,73 @@ export class {{ service.name | caseUcfirst }} extends Service {
101101
let id = undefined;
102102
let response = undefined;
103103

104+
let counter = 0;
104105
const totalCounters = Math.ceil(size / Client.CHUNK_SIZE);
105106

106-
for (let counter = 0; counter < totalCounters; counter++) {
107-
const start = (counter * Client.CHUNK_SIZE);
108-
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
109-
const headers: { [header: string]: string } = {
107+
const headers: { [header: string]: string } = {
110108
{% for parameter in method.parameters.header %}
111-
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
109+
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
112110
{% endfor %}
113111
{% for key, header in method.headers %}
114-
'{{ key }}': '{{ header }}',
112+
'{{ key }}': '{{ header }}',
115113
{% endfor %}
116-
'content-range': 'bytes ' + start + '-' + end + '/' + size
117-
};
114+
};
118115

119-
if (id) {
120-
headers['x-{{spec.title | caseLower }}-id'] = id;
121-
}
122-
123-
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
124-
125-
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
126-
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
127-
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
128-
const numberOfBytesRead = await Deno.read(stream.rid, buf);
129-
130-
if (!numberOfBytesRead) {
131-
break;
132-
}
133-
134-
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
135-
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
136-
}
137-
}
138-
139-
payload['{{ parameter.name }}'] = new File([totalBuffer], basename({{ parameter.name | caseCamel | escapeKeyword }}));
140-
141-
response = await this.client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});
116+
{% for parameter in method.parameters.all %}
117+
{% if parameter.isUploadID %}
118+
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
119+
try {
120+
response = await this.client.call('get', path + '/' + {{ parameter.name }}, headers);
121+
counter = response.chunksUploaded;
122+
} catch(e) {
123+
}
124+
}
125+
{% endif %}
126+
{% endfor %}
142127

143-
if (!id) {
144-
id = response['$id'];
128+
for (counter; counter < totalCounters; counter++) {
129+
const start = (counter * Client.CHUNK_SIZE);
130+
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
131+
132+
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size
133+
134+
if (id) {
135+
headers['x-{{spec.title | caseLower }}-id'] = id;
136+
}
137+
138+
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
139+
140+
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
141+
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
142+
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
143+
const numberOfBytesRead = await Deno.read(stream.rid, buf);
144+
145+
if (!numberOfBytesRead) {
146+
break;
145147
}
146148

147-
if (onProgress !== null) {
148-
onProgress({
149-
$id: response['$id'],
150-
progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100,
151-
sizeUploaded: end+1,
152-
chunksTotal: response['chunksTotal'],
153-
chunksUploaded: response['chunksUploaded']
154-
});
149+
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
150+
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
155151
}
152+
}
153+
154+
payload['{{ parameter.name }}'] = new File([totalBuffer], basename({{ parameter.name | caseCamel | escapeKeyword }}));
155+
156+
response = await this.client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});
157+
158+
if (!id) {
159+
id = response['$id'];
160+
}
161+
162+
if (onProgress !== null) {
163+
onProgress({
164+
$id: response['$id'],
165+
progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100,
166+
sizeUploaded: end+1,
167+
chunksTotal: response['chunksTotal'],
168+
chunksUploaded: response['chunksUploaded']
169+
});
170+
}
156171
}
157172

158173
return response;

0 commit comments

Comments
 (0)