1+ {% for parameter in method .parameters .all %}
2+ {% if parameter .type == ' file' %}
3+
4+ const size = {{ parameter .name | caseCamel | escapeKeyword }}.size;
5+
6+ const apiHeaders = {
7+ {% for parameter in method .parameters .header %}
8+ '{{ parameter .name }}': ${{ parameter .name | caseCamel | escapeKeyword }},
9+ {% endfor %}
10+ {% for key , header in method .headers %}
11+ '{{ key }}': '{{ header }}',
12+ {% endfor %}
13+ };
14+
15+ let id = undefined;
16+ let response = undefined;
17+
18+ let chunksUploaded = 0;
19+ {% for parameter in method .parameters .all %}
20+ {% if parameter .isUploadID %}
21+
22+ if({{ parameter .name | caseCamel | escapeKeyword }} != 'unique()') {
23+ try {
24+ response = await client.call('get', apiPath + '/' + {{ parameter .name }}, apiHeaders);
25+ chunksUploaded = response.chunksUploaded;
26+ } catch(e) {
27+ }
28+ }
29+ {% endif %}
30+ {% endfor %}
31+
32+ let currentChunk = 1;
33+ let currentPosition = 0;
34+ let uploadableChunk = new Uint8Array(client.CHUNK_SIZE);
35+
36+ const uploadChunk = async (lastUpload = false) => {
37+ if(currentChunk < = chunksUploaded) {
38+ return;
39+ }
40+
41+ const start = ((currentChunk - 1) * client.CHUNK_SIZE);
42+ let end = start + currentPosition - 1;
43+
44+ if(!lastUpload || currentChunk !== 1) {
45+ apiHeaders['content-range'] = 'bytes ' + start + '-' + end + '/' + size;
46+ }
47+
48+ let uploadableChunkTrimmed;
49+
50+ if(currentPosition + 1 >= client.CHUNK_SIZE) {
51+ uploadableChunkTrimmed = uploadableChunk;
52+ } else {
53+ uploadableChunkTrimmed = new Uint8Array(currentPosition);
54+ for(let i = 0; i < = currentPosition; i++) {
55+ uploadableChunkTrimmed[i] = uploadableChunk[i];
56+ }
57+ }
58+
59+ if (id) {
60+ apiHeaders['x-{{spec .title | caseLower }}-id'] = id;
61+ }
62+
63+ payload['{{ parameter .name }}'] = { type: 'file', file: new File([uploadableChunkTrimmed], {{ parameter .name | caseCamel | escapeKeyword }}.filename), filename: {{ parameter .name | caseCamel | escapeKeyword }}.filename };
64+
65+ response = await client.call('{{ method .method | caseLower }}', apiPath, apiHeaders, payload{% if method .type == ' location' %}, 'arraybuffer'{% endif %});
66+
67+ if (!id) {
68+ id = response['$id'];
69+ }
70+
71+ if (onProgress !== null) {
72+ onProgress({
73+ $id: response['$id'],
74+ progress: Math.min((currentChunk) * client.CHUNK_SIZE, size) / size * 100,
75+ sizeUploaded: end+1,
76+ chunksTotal: response['chunksTotal'],
77+ chunksUploaded: response['chunksUploaded']
78+ });
79+ }
80+
81+ uploadableChunk = new Uint8Array(client.CHUNK_SIZE);
82+ currentChunk++;
83+ currentPosition = 0;
84+ }
85+
86+ for await (const chunk of {{ parameter .name | caseCamel | escapeKeyword }}.stream) {
87+ for(const b of chunk) {
88+ uploadableChunk[currentPosition] = b;
89+
90+ currentPosition++;
91+ if(currentPosition >= client.CHUNK_SIZE) {
92+ await uploadChunk();
93+ currentPosition = 0;
94+ }
95+ }
96+ }
97+
98+ if (currentPosition > 0) { // Check if there's any remaining data for the last chunk
99+ await uploadChunk(true);
100+ }
101+
102+ {% if method .packaging %}
103+ fs.unlinkSync(filePath);
104+ {% endif %}
105+ {% if method .type == ' location' %}
106+ fs.writeFileSync(destination, response);
107+ {% endif %}
108+
109+ if (parseOutput) {
110+ parse(response)
111+ success()
112+ }
113+
114+ return response;
115+ {% endif %}
116+ {% endfor %}
0 commit comments