Skip to content

Commit 2040f64

Browse files
committed
resumable upload support
1 parent d1a4d84 commit 2040f64

File tree

3 files changed

+94
-79
lines changed

3 files changed

+94
-79
lines changed

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class Client {
1111
endpoint: string = 'https://appwrite.io/v1';
1212
headers: Payload = {
1313
'content-type': '',
14-
'x-sdk-version': 'appwrite:deno:3.0.0',
14+
'x-sdk-version': 'appwrite:deno:3.1.0',
1515
'X-Appwrite-Response-Format':'0.13.0',
1616
};
1717

src/services/functions.ts

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class Functions extends Service {
129129
/**
130130
* List the currently active function runtimes.
131131
*
132-
* Get a list of all runtimes that are currently active in your project.
132+
* Get a list of all runtimes that are currently active on your instance.
133133
*
134134
* @throws {AppwriteException}
135135
* @returns {Promise}
@@ -353,53 +353,57 @@ export class Functions extends Service {
353353
let id = undefined;
354354
let response = undefined;
355355

356+
let counter = 0;
356357
const totalCounters = Math.ceil(size / Client.CHUNK_SIZE);
357358

358-
for (let counter = 0; counter < totalCounters; counter++) {
359-
const start = (counter * Client.CHUNK_SIZE);
360-
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
361-
const headers: { [header: string]: string } = {
362-
'content-type': 'multipart/form-data',
363-
'content-range': 'bytes ' + start + '-' + end + '/' + size
364-
};
359+
const headers: { [header: string]: string } = {
360+
'content-type': 'multipart/form-data',
361+
};
365362

366-
if (id) {
367-
headers['x-appwrite-id'] = id;
368-
}
369-
370-
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
371-
372-
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
373-
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
374-
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
375-
const numberOfBytesRead = await Deno.read(stream.rid, buf);
376363

377-
if (!numberOfBytesRead) {
378-
break;
379-
}
364+
for (counter; counter < totalCounters; counter++) {
365+
const start = (counter * Client.CHUNK_SIZE);
366+
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
367+
368+
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size
380369

381-
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
382-
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
383-
}
384-
}
385-
386-
payload['code'] = new File([totalBuffer], basename(code));
370+
if (id) {
371+
headers['x-appwrite-id'] = id;
372+
}
373+
374+
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
387375

388-
response = await this.client.call('post', path, headers, payload);
376+
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
377+
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
378+
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
379+
const numberOfBytesRead = await Deno.read(stream.rid, buf);
389380

390-
if (!id) {
391-
id = response['$id'];
381+
if (!numberOfBytesRead) {
382+
break;
392383
}
393384

394-
if (onProgress !== null) {
395-
onProgress({
396-
$id: response['$id'],
397-
progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100,
398-
sizeUploaded: end+1,
399-
chunksTotal: response['chunksTotal'],
400-
chunksUploaded: response['chunksUploaded']
401-
});
385+
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
386+
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
402387
}
388+
}
389+
390+
payload['code'] = new File([totalBuffer], basename(code));
391+
392+
response = await this.client.call('post', path, headers, payload);
393+
394+
if (!id) {
395+
id = response['$id'];
396+
}
397+
398+
if (onProgress !== null) {
399+
onProgress({
400+
$id: response['$id'],
401+
progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100,
402+
sizeUploaded: end+1,
403+
chunksTotal: response['chunksTotal'],
404+
chunksUploaded: response['chunksUploaded']
405+
});
406+
}
403407
}
404408

405409
return response;

src/services/storage.ts

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -361,53 +361,64 @@ export class Storage extends Service {
361361
let id = undefined;
362362
let response = undefined;
363363

364+
let counter = 0;
364365
const totalCounters = Math.ceil(size / Client.CHUNK_SIZE);
365366

366-
for (let counter = 0; counter < totalCounters; counter++) {
367-
const start = (counter * Client.CHUNK_SIZE);
368-
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
369-
const headers: { [header: string]: string } = {
370-
'content-type': 'multipart/form-data',
371-
'content-range': 'bytes ' + start + '-' + end + '/' + size
372-
};
367+
const headers: { [header: string]: string } = {
368+
'content-type': 'multipart/form-data',
369+
};
373370

374-
if (id) {
375-
headers['x-appwrite-id'] = id;
376-
}
377-
378-
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
379-
380-
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
381-
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
382-
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
383-
const numberOfBytesRead = await Deno.read(stream.rid, buf);
384-
385-
if (!numberOfBytesRead) {
386-
break;
387-
}
388-
389-
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
390-
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
391-
}
392-
}
393-
394-
payload['file'] = new File([totalBuffer], basename(file));
395-
396-
response = await this.client.call('post', path, headers, payload);
371+
if(fileId != 'unique()') {
372+
try {
373+
response = await this.client.call('get', path + '/' + fileId, headers);
374+
counter = response.chunksUploaded;
375+
} catch(e) {
376+
}
377+
}
397378

398-
if (!id) {
399-
id = response['$id'];
379+
for (counter; counter < totalCounters; counter++) {
380+
const start = (counter * Client.CHUNK_SIZE);
381+
const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size);
382+
383+
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size
384+
385+
if (id) {
386+
headers['x-appwrite-id'] = id;
387+
}
388+
389+
const totalBuffer = new Uint8Array(Client.CHUNK_SIZE);
390+
391+
for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) {
392+
const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE);
393+
const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start);
394+
const numberOfBytesRead = await Deno.read(stream.rid, buf);
395+
396+
if (!numberOfBytesRead) {
397+
break;
400398
}
401399

402-
if (onProgress !== null) {
403-
onProgress({
404-
$id: response['$id'],
405-
progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100,
406-
sizeUploaded: end+1,
407-
chunksTotal: response['chunksTotal'],
408-
chunksUploaded: response['chunksUploaded']
409-
});
400+
for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) {
401+
totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex];
410402
}
403+
}
404+
405+
payload['file'] = new File([totalBuffer], basename(file));
406+
407+
response = await this.client.call('post', path, headers, payload);
408+
409+
if (!id) {
410+
id = response['$id'];
411+
}
412+
413+
if (onProgress !== null) {
414+
onProgress({
415+
$id: response['$id'],
416+
progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100,
417+
sizeUploaded: end+1,
418+
chunksTotal: response['chunksTotal'],
419+
chunksUploaded: response['chunksUploaded']
420+
});
421+
}
411422
}
412423

413424
return response;

0 commit comments

Comments
 (0)