-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload-helper.js
73 lines (63 loc) · 1.66 KB
/
upload-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {deleteFileFromDexie, getFileFromDexieById} from './offline/dexie-operations';
import {upload} from '@unicef-polymer/etools-ajax/upload-helper';
export function uploadAllFilesSequentially(config, files) {
return new Promise(function (resolve, reject) {
const allSuccessResponses = [];
const allErrorResponses = [];
let i;
let counter = 0;
for (i = 0; i < files.length; i++) {
upload(config, files[i].binaryData, files[i].filename)
.then((response) => {
allSuccessResponses.push(response);
if (counter + 1 === files.length) {
resolve({
success: allSuccessResponses,
error: allErrorResponses
});
}
counter++;
})
.catch((err) => {
allErrorResponses.push(err);
if (counter + 1 === files.length) {
resolve({
success: allSuccessResponses,
error: allErrorResponses
});
}
counter++;
});
}
});
}
/**
* config = {
* endpointInfo: {},
* uploadEndpoint: '',
* jwtLocalStorageKey: ''
* }
*/
export async function uploadFileStoredInDexie(config, id) {
const file = await getFileFromDexieById(id);
const response = {
success: null,
error: null
};
if (!file) {
response.error = 'File ' + id + ' not found in Dexie';
return response;
}
try {
response.success = await upload(config, file.binaryData, file.filename);
} catch (error) {
response.error = error;
return response;
}
try {
await deleteFileFromDexie(id);
} catch (error) {
response.error = error;
}
return response;
}