lupine.components 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/upload-file.ts +30 -12
package/package.json
CHANGED
package/src/lib/upload-file.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { getRenderPageProps } from "lupine.web";
|
|
2
|
+
import { NotificationColor, NotificationMessage } from "../components";
|
|
2
3
|
|
|
3
4
|
const _saveChunkSize = {
|
|
4
|
-
size: 1024 *
|
|
5
|
+
size: 1024 * 200,
|
|
5
6
|
};
|
|
6
7
|
export const setChunkSize = (chunkSize: number) => {
|
|
7
8
|
_saveChunkSize.size = chunkSize;
|
|
@@ -24,22 +25,35 @@ export const uploadFileChunk = async (
|
|
|
24
25
|
chunkNumber: number,
|
|
25
26
|
totalChunks: number,
|
|
26
27
|
uploadUrl: string,
|
|
27
|
-
key: string
|
|
28
|
+
key: string,
|
|
29
|
+
retryCount = 3,
|
|
30
|
+
retryMessage = '', // can have ${tryCount}
|
|
28
31
|
) => {
|
|
29
|
-
// this must be the FE so we can use fetch
|
|
30
32
|
let url = uploadUrl + (uploadUrl.indexOf('?') === -1 ? '?' : '');
|
|
31
33
|
url += `&chunkNumber=${chunkNumber.toString()}`;
|
|
32
34
|
url += `&totalChunks=${totalChunks.toString()}`;
|
|
33
35
|
if (key) {
|
|
34
36
|
url += `&key=${key}`;
|
|
35
37
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
let tryCount = 0;
|
|
39
|
+
let json;
|
|
40
|
+
while (tryCount < retryCount) {
|
|
41
|
+
try {
|
|
42
|
+
json = await getRenderPageProps().renderPageFunctions.fetchData(url, chunk);
|
|
43
|
+
if (json && json.json) {
|
|
44
|
+
json = json.json;
|
|
45
|
+
}
|
|
46
|
+
if (json && json.status) { // ok or error
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.log(`uploadFileChunk error, try: ${tryCount}`, error);
|
|
51
|
+
}
|
|
52
|
+
tryCount++;
|
|
53
|
+
if (retryMessage) {
|
|
54
|
+
NotificationMessage.sendMessage(retryMessage.replace('${tryCount}', tryCount.toString()), NotificationColor.Warning);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
43
57
|
return json;
|
|
44
58
|
};
|
|
45
59
|
|
|
@@ -53,7 +67,11 @@ export const uploadFile = async (
|
|
|
53
67
|
let key = '';
|
|
54
68
|
const len = file instanceof File ? file.size : file.length;
|
|
55
69
|
if (len <= chunkSize) {
|
|
56
|
-
|
|
70
|
+
const uploaded = await uploadFileChunk(file, 0, 1, uploadUrl, key);
|
|
71
|
+
if (!uploaded || uploaded.status !== 'ok') {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
57
75
|
}
|
|
58
76
|
|
|
59
77
|
const totalChunks = Math.ceil(len / chunkSize);
|
|
@@ -62,7 +80,7 @@ export const uploadFile = async (
|
|
|
62
80
|
const end = Math.min((i + 1) * chunkSize, len);
|
|
63
81
|
const chunk = file.slice(start, end);
|
|
64
82
|
const uploaded = await uploadFileChunk(chunk, i, totalChunks, uploadUrl, key);
|
|
65
|
-
if (!uploaded || uploaded.
|
|
83
|
+
if (!uploaded || uploaded.status !== 'ok') {
|
|
66
84
|
return false;
|
|
67
85
|
}
|
|
68
86
|
key = uploaded.key;
|