lupine.components 1.0.6 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lupine.components",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "license": "MIT",
5
5
  "author": "uuware.com",
6
6
  "homepage": "https://uuware.com/",
@@ -1,5 +1,8 @@
1
+ import { getRenderPageProps } from "lupine.web";
2
+ import { NotificationColor, NotificationMessage } from "../components";
3
+
1
4
  const _saveChunkSize = {
2
- size: 1024 * 500,
5
+ size: 1024 * 200,
3
6
  };
4
7
  export const setChunkSize = (chunkSize: number) => {
5
8
  _saveChunkSize.size = chunkSize;
@@ -8,10 +11,11 @@ export const getChunkSize = () => {
8
11
  return _saveChunkSize.size;
9
12
  };
10
13
  export const checkUploadedFileSize = async (uploadUrl: string) => {
11
- const response = await fetch(uploadUrl + '?check-size=1', {
12
- method: 'POST',
13
- });
14
- const json = await response.json();
14
+ // const response = await fetch(uploadUrl + '?check-size=1', {
15
+ // method: 'POST',
16
+ // });
17
+ // const json = await response.json();
18
+ const json = await getRenderPageProps().renderPageFunctions.fetchData(uploadUrl + '?check-size=1');
15
19
  return json && json.size ? json.size : 0;
16
20
  };
17
21
 
@@ -21,20 +25,35 @@ export const uploadFileChunk = async (
21
25
  chunkNumber: number,
22
26
  totalChunks: number,
23
27
  uploadUrl: string,
24
- key: string
28
+ key: string,
29
+ retryCount = 3,
30
+ retryMessage = '', // can have ${tryCount}
25
31
  ) => {
26
- // this must be the FE so we can use fetch
27
32
  let url = uploadUrl + (uploadUrl.indexOf('?') === -1 ? '?' : '');
28
33
  url += `&chunkNumber=${chunkNumber.toString()}`;
29
34
  url += `&totalChunks=${totalChunks.toString()}`;
30
35
  if (key) {
31
36
  url += `&key=${key}`;
32
37
  }
33
- const response = await fetch(url, {
34
- method: 'POST',
35
- body: chunk,
36
- });
37
- const json = await response.json();
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
+ }
38
57
  return json;
39
58
  };
40
59
 
@@ -48,7 +67,11 @@ export const uploadFile = async (
48
67
  let key = '';
49
68
  const len = file instanceof File ? file.size : file.length;
50
69
  if (len <= chunkSize) {
51
- return await uploadFileChunk(file, 0, 1, uploadUrl, key);
70
+ const uploaded = await uploadFileChunk(file, 0, 1, uploadUrl, key);
71
+ if (!uploaded || uploaded.status !== 'ok') {
72
+ return false;
73
+ }
74
+ return true;
52
75
  }
53
76
 
54
77
  const totalChunks = Math.ceil(len / chunkSize);
@@ -57,7 +80,7 @@ export const uploadFile = async (
57
80
  const end = Math.min((i + 1) * chunkSize, len);
58
81
  const chunk = file.slice(start, end);
59
82
  const uploaded = await uploadFileChunk(chunk, i, totalChunks, uploadUrl, key);
60
- if (!uploaded || uploaded.chunkNumber === i.toString() || !uploaded.key) {
83
+ if (!uploaded || uploaded.status !== 'ok') {
61
84
  return false;
62
85
  }
63
86
  key = uploaded.key;