Skip to content

Commit adc9500

Browse files
authored
Merge pull request #401 from appwrite/feat-resumable-upload-cli
Feat: CLI resumable upload
2 parents a031f47 + 7458e0b commit adc9500

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function getSSLPage($url) {
183183
->setTwitter('appwrite_io')
184184
->setDiscord('564160730845151244', 'https://appwrite.io/discord')
185185
->setDefaultHeaders([
186-
'X-Appwrite-Response-Format' => '0.12.0',
186+
'X-Appwrite-Response-Format' => '0.13.0',
187187
])
188188
;
189189

templates/cli/lib/commands/command.js.twig

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,46 +106,61 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
106106
const streamFilePath = payload['{{ parameter.name }}'];
107107
let id = undefined;
108108

109+
let counter = 0;
109110
const totalCounters = Math.ceil(size / libClient.CHUNK_SIZE);
110111

111-
for (let counter = 0; counter < totalCounters; counter++) {
112-
const start = (counter * libClient.CHUNK_SIZE);
113-
const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size);
114-
const headers = {
112+
const headers = {
115113
{% for parameter in method.parameters.header %}
116-
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
114+
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
117115
{% endfor %}
118116
{% for key, header in method.headers %}
119-
'{{ key }}': '{{ header }}',
117+
'{{ key }}': '{{ header }}',
118+
{% endfor %}
119+
};
120+
121+
{% for parameter in method.parameters.all %}
122+
{% if parameter.isUploadID %}
123+
if({{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
124+
try {
125+
response = await client.call('get', path + '/' + {{ parameter.name }}, headers);
126+
counter = response.chunksUploaded;
127+
} catch(e) {
128+
}
129+
}
130+
{% endif %}
120131
{% endfor %}
121-
'content-range': 'bytes ' + start + '-' + end + '/' + size
122-
};
123132

124-
if (id) {
125-
headers['x-appwrite-id'] = id;
126-
}
133+
for (counter; counter < totalCounters; counter++) {
134+
const start = (counter * libClient.CHUNK_SIZE);
135+
const end = Math.min((((counter * libClient.CHUNK_SIZE) + libClient.CHUNK_SIZE) - 1), size);
136+
137+
headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
138+
139+
if (id) {
140+
headers['x-appwrite-id'] = id;
141+
}
142+
143+
const stream = fs.createReadStream(streamFilePath, {
144+
start,
145+
end
146+
});
147+
payload['{{ parameter.name }}'] = stream;
148+
149+
response = await client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});
150+
151+
if (!id) {
152+
id = response['$id'];
153+
}
127154

128-
const stream = fs.createReadStream(streamFilePath, {
129-
start,
130-
end
155+
if (onProgress !== null) {
156+
onProgress({
157+
$id: response['$id'],
158+
progress: Math.min((counter+1) * libClient.CHUNK_SIZE, size) / size * 100,
159+
sizeUploaded: end+1,
160+
chunksTotal: response['chunksTotal'],
161+
chunksUploaded: response['chunksUploaded']
131162
});
132-
payload['{{ parameter.name }}'] = stream;
133-
134-
response = await client.call('{{ method.method | caseLower }}', path, headers, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});
135-
136-
if (!id) {
137-
id = response['$id'];
138-
}
139-
140-
if (onProgress !== null) {
141-
onProgress({
142-
$id: response['$id'],
143-
progress: Math.min((counter+1) * libClient.CHUNK_SIZE, size) / size * 100,
144-
sizeUploaded: end+1,
145-
chunksTotal: response['chunksTotal'],
146-
chunksUploaded: response['chunksUploaded']
147-
});
148-
}
163+
}
149164
}
150165
}
151166
{% endif %}

tests/resources/spec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"url": "https://raw.githubusercontent.com/appwrite/appwrite/master/LICENSE"
1616
}
1717
},
18-
"host": "appwrite.io",
18+
"host": "demo.appwrite.io",
1919
"basePath": "/v1",
2020
"schemes": ["https"],
2121
"consumes": ["application/json", "multipart/form-data"],

0 commit comments

Comments
 (0)