Skip to content

Commit 4f021b9

Browse files
committed
resumable upload support
1 parent d43aff6 commit 4f021b9

File tree

5 files changed

+104
-89
lines changed

5 files changed

+104
-89
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ declare module "node-appwrite" {
22112211
/**
22122212
* List the currently active function runtimes.
22132213
*
2214-
* Get a list of all runtimes that are currently active in your project.
2214+
* Get a list of all runtimes that are currently active on your instance.
22152215
*
22162216
* @throws {AppwriteException}
22172217
* @returns {Promise}

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Client {
1010
this.endpoint = 'https://HOSTNAME/v1';
1111
this.headers = {
1212
'content-type': '',
13-
'x-sdk-version': 'appwrite:nodejs:5.0.0',
13+
'x-sdk-version': 'appwrite:nodejs:5.1.0',
1414
'X-Appwrite-Response-Format' : '0.13.0',
1515
};
1616
this.selfSigned = false;

lib/services/functions.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Functions extends Service {
132132
/**
133133
* List the currently active function runtimes.
134134
*
135-
* Get a list of all runtimes that are currently active in your project.
135+
* Get a list of all runtimes that are currently active on your instance.
136136
*
137137
* @throws {AppwriteException}
138138
* @returns {Promise}
@@ -359,53 +359,57 @@ class Functions extends Service {
359359
const { size: size } = await promisify(fs.stat)(code);
360360

361361
if (size <= client.CHUNK_SIZE) {
362-
payload['code'] = fs.createReadStream(code);
362+
payload['code'] = fs.createReadStream(code);
363363

364-
return await this.client.call('post', path, {
364+
return await this.client.call('post', path, {
365365
'content-type': 'multipart/form-data',
366-
}, payload);
366+
}, payload);
367367
} else {
368-
let id = undefined;
369-
let response = undefined;
370-
371-
const totalCounters = Math.ceil(size / client.CHUNK_SIZE);
372-
373-
for (let counter = 0; counter < totalCounters; counter++) {
374-
const start = (counter * client.CHUNK_SIZE);
375-
const end = Math.min((((counter * client.CHUNK_SIZE) + client.CHUNK_SIZE) - 1), size);
376-
const headers = {
377-
'content-type': 'multipart/form-data',
378-
'content-range': 'bytes ' + start + '-' + end + '/' + size
379-
};
380-
381-
if (id) {
382-
headers['x-appwrite-id'] = id;
383-
}
384-
385-
const stream = fs.createReadStream(code, {
386-
start,
387-
end
388-
});
389-
payload['code'] = stream;
390-
391-
response = await this.client.call('post', path, headers, payload);
392-
393-
if (!id) {
394-
id = response['$id'];
395-
}
396-
397-
if (onProgress !== null) {
398-
onProgress({
399-
$id: response['$id'],
400-
progress: Math.min((counter+1) * client.CHUNK_SIZE, size) / size * 100,
401-
sizeUploaded: end+1,
402-
chunksTotal: response['chunksTotal'],
403-
chunksUploaded: response['chunksUploaded']
404-
});
405-
}
368+
let id = undefined;
369+
let response = undefined;
370+
371+
let counter = 0;
372+
const totalCounters = Math.ceil(size / client.CHUNK_SIZE);
373+
374+
const headers = {
375+
'content-type': 'multipart/form-data',
376+
};
377+
378+
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 stream = fs.createReadStream(code, {
390+
start,
391+
end
392+
});
393+
payload['code'] = stream;
394+
395+
response = await this.client.call('post', path, headers, payload);
396+
397+
if (!id) {
398+
id = response['$id'];
399+
}
400+
401+
if (onProgress !== null) {
402+
onProgress({
403+
$id: response['$id'],
404+
progress: Math.min((counter+1) * client.CHUNK_SIZE, size) / size * 100,
405+
sizeUploaded: end+1,
406+
chunksTotal: response['chunksTotal'],
407+
chunksUploaded: response['chunksUploaded']
408+
});
406409
}
410+
}
407411

408-
return response;
412+
return response;
409413
}
410414
}
411415

lib/services/storage.js

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -372,53 +372,64 @@ class Storage extends Service {
372372
const { size: size } = await promisify(fs.stat)(file);
373373

374374
if (size <= client.CHUNK_SIZE) {
375-
payload['file'] = fs.createReadStream(file);
375+
payload['file'] = fs.createReadStream(file);
376376

377-
return await this.client.call('post', path, {
377+
return await this.client.call('post', path, {
378378
'content-type': 'multipart/form-data',
379-
}, payload);
379+
}, payload);
380380
} else {
381-
let id = undefined;
382-
let response = undefined;
383-
384-
const totalCounters = Math.ceil(size / client.CHUNK_SIZE);
385-
386-
for (let counter = 0; counter < totalCounters; counter++) {
387-
const start = (counter * client.CHUNK_SIZE);
388-
const end = Math.min((((counter * client.CHUNK_SIZE) + client.CHUNK_SIZE) - 1), size);
389-
const headers = {
390-
'content-type': 'multipart/form-data',
391-
'content-range': 'bytes ' + start + '-' + end + '/' + size
392-
};
393-
394-
if (id) {
395-
headers['x-appwrite-id'] = id;
396-
}
397-
398-
const stream = fs.createReadStream(file, {
399-
start,
400-
end
401-
});
402-
payload['file'] = stream;
403-
404-
response = await this.client.call('post', path, headers, payload);
405-
406-
if (!id) {
407-
id = response['$id'];
408-
}
409-
410-
if (onProgress !== null) {
411-
onProgress({
412-
$id: response['$id'],
413-
progress: Math.min((counter+1) * client.CHUNK_SIZE, size) / size * 100,
414-
sizeUploaded: end+1,
415-
chunksTotal: response['chunksTotal'],
416-
chunksUploaded: response['chunksUploaded']
417-
});
418-
}
381+
let id = undefined;
382+
let response = undefined;
383+
384+
let counter = 0;
385+
const totalCounters = Math.ceil(size / client.CHUNK_SIZE);
386+
387+
const headers = {
388+
'content-type': 'multipart/form-data',
389+
};
390+
391+
if(fileId != 'unique()') {
392+
try {
393+
response = await this.client.call('get', path + '/' + fileId, headers);
394+
counter = response.chunksUploaded;
395+
} catch(e) {
396+
}
397+
}
398+
399+
for (counter; counter < totalCounters; counter++) {
400+
const start = (counter * client.CHUNK_SIZE);
401+
const end = Math.min((((counter * client.CHUNK_SIZE) + client.CHUNK_SIZE) - 1), size);
402+
403+
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
404+
405+
if (id) {
406+
headers['x-appwrite-id'] = id;
407+
}
408+
409+
const stream = fs.createReadStream(file, {
410+
start,
411+
end
412+
});
413+
payload['file'] = stream;
414+
415+
response = await this.client.call('post', path, headers, payload);
416+
417+
if (!id) {
418+
id = response['$id'];
419+
}
420+
421+
if (onProgress !== null) {
422+
onProgress({
423+
$id: response['$id'],
424+
progress: Math.min((counter+1) * client.CHUNK_SIZE, size) / size * 100,
425+
sizeUploaded: end+1,
426+
chunksTotal: response['chunksTotal'],
427+
chunksUploaded: response['chunksUploaded']
428+
});
419429
}
430+
}
420431

421-
return response;
432+
return response;
422433
}
423434
}
424435

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "node-appwrite",
33
"homepage": "https://appwrite.io/support",
44
"description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API",
5-
"version": "5.0.0",
5+
"version": "5.1.0",
66
"license": "BSD-3-Clause",
77
"main": "./index.js",
88
"types": "./index.d.ts",
@@ -12,7 +12,7 @@
1212
},
1313
"devDependencies": {},
1414
"dependencies": {
15-
"axios": "^0.25.0",
15+
"axios": "^0.26.1",
1616
"form-data": "^4.0.0"
1717
}
1818
}

0 commit comments

Comments
 (0)