@swan-admin/swan-ai-measurements 1.1.36 → 1.1.37
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/dist/fileUpload.js +150 -147
- package/package.json +1 -1
package/dist/fileUpload.js
CHANGED
|
@@ -1,155 +1,158 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
const axios_1 = __importDefault(require("axios"));
|
|
7
9
|
const constants_js_1 = require("./constants.js");
|
|
8
10
|
const utils_js_1 = require("./utils.js");
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
import Uppy from "@uppy/core";
|
|
12
|
+
import AwsS3Multipart from "@uppy/aws-s3-multipart";
|
|
11
13
|
class FileUpload {
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
14
|
+
#uppyIns;
|
|
15
|
+
#accessKey;
|
|
16
|
+
#stagingUrl;
|
|
17
|
+
constructor(accessKey, stagingUrl = false) {
|
|
18
|
+
this.#accessKey = accessKey;
|
|
19
|
+
this.#stagingUrl = stagingUrl;
|
|
20
|
+
}
|
|
21
|
+
async uploadFileFrontend({ file, arrayMetaData, scanId, email }) {
|
|
22
|
+
if (!(0, utils_js_1.checkParameters)(file, arrayMetaData, scanId, email)) {
|
|
23
|
+
throw new Error(constants_js_1.REQUIRED_MESSAGE);
|
|
24
|
+
}
|
|
25
|
+
if (!(0, utils_js_1.isValidEmail)(email.trim())) {
|
|
26
|
+
throw new Error(constants_js_1.REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
|
|
27
|
+
}
|
|
28
|
+
if (!(0, utils_js_1.checkMetaDataValue)(arrayMetaData)) {
|
|
29
|
+
throw new Error(constants_js_1.REQUIRED_MESSAGE_FOR_META_DATA);
|
|
30
|
+
}
|
|
31
|
+
arrayMetaData = (0, utils_js_1.addScanType)(arrayMetaData, scanId, email);
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
if (this.#uppyIns) {
|
|
34
|
+
this.#uppyIns.close();
|
|
35
|
+
}
|
|
36
|
+
this.#uppyIns = new Uppy({ autoProceed: true });
|
|
37
|
+
this.#uppyIns.use(AwsS3Multipart, {
|
|
38
|
+
limit: 10,
|
|
39
|
+
retryDelays: [0, 1000, 3000, 5000],
|
|
40
|
+
companionUrl: (0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl }),
|
|
41
|
+
getChunkSize: () => 5 * 1024 * 1024,
|
|
42
|
+
createMultipartUpload: (file) => {
|
|
43
|
+
const objectKey = `${scanId}.${file.extension}`;
|
|
44
|
+
return (0, utils_js_1.fetchData)({
|
|
45
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
46
|
+
apiKey: this.#accessKey,
|
|
47
|
+
stagingUrl: this.#stagingUrl,
|
|
48
|
+
body: {
|
|
49
|
+
objectKey,
|
|
50
|
+
contentType: file.type,
|
|
51
|
+
objectMetadata: arrayMetaData,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
completeMultipartUpload: (file, { uploadId, key, parts }) =>
|
|
56
|
+
(0, utils_js_1.fetchData)({
|
|
57
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
|
|
58
|
+
apiKey: this.#accessKey,
|
|
59
|
+
stagingUrl: this.#stagingUrl,
|
|
60
|
+
body: {
|
|
61
|
+
uploadId,
|
|
62
|
+
objectKey: key,
|
|
63
|
+
parts,
|
|
64
|
+
originalFileName: file.name,
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
signPart: (file, partData) =>
|
|
68
|
+
(0, utils_js_1.fetchData)({
|
|
69
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
70
|
+
stagingUrl: this.#stagingUrl,
|
|
71
|
+
apiKey: this.#accessKey,
|
|
72
|
+
body: {
|
|
73
|
+
objectKey: partData.key,
|
|
74
|
+
uploadId: partData.uploadId,
|
|
75
|
+
partNumber: partData.partNumber,
|
|
76
|
+
},
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
this.#uppyIns.addFile({
|
|
80
|
+
source: "manual",
|
|
81
|
+
name: file.name,
|
|
82
|
+
type: file.type,
|
|
83
|
+
data: file,
|
|
84
|
+
});
|
|
85
|
+
this.#uppyIns.on("upload-error", (file, error, response) => {
|
|
86
|
+
reject(error);
|
|
87
|
+
});
|
|
88
|
+
this.#uppyIns.on("upload-success", () => {
|
|
89
|
+
resolve({ message: "file uploaded successfully" });
|
|
90
|
+
});
|
|
91
|
+
this.#uppyIns.on("complete", (result) => {
|
|
92
|
+
if (this.#uppyIns) {
|
|
93
|
+
this.#uppyIns.close();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async uploadFile({ file, arrayMetaData, scanId, email }) {
|
|
99
|
+
if (!(0, utils_js_1.checkParameters)(file, arrayMetaData, scanId, email)) {
|
|
100
|
+
throw new Error(constants_js_1.REQUIRED_MESSAGE);
|
|
101
|
+
}
|
|
102
|
+
if (!(0, utils_js_1.isValidEmail)(email.trim())) {
|
|
103
|
+
throw new Error(constants_js_1.REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
|
|
104
|
+
}
|
|
105
|
+
if (!(0, utils_js_1.checkMetaDataValue)(arrayMetaData)) {
|
|
106
|
+
throw new Error(constants_js_1.REQUIRED_MESSAGE_FOR_META_DATA);
|
|
107
|
+
}
|
|
108
|
+
arrayMetaData = (0, utils_js_1.addScanType)(arrayMetaData, scanId, email);
|
|
109
|
+
return new Promise(async (resolve, reject) => {
|
|
110
|
+
try {
|
|
111
|
+
const res = await (0, utils_js_1.fetchData)({
|
|
112
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
113
|
+
apiKey: this.#accessKey,
|
|
114
|
+
stagingUrl: this.#stagingUrl,
|
|
115
|
+
body: {
|
|
116
|
+
objectKey: file.name,
|
|
117
|
+
contentType: file.type,
|
|
118
|
+
objectMetadata: arrayMetaData,
|
|
119
|
+
},
|
|
120
|
+
throwError: true,
|
|
121
|
+
});
|
|
122
|
+
const totalChunks = (0, utils_js_1.getFileChunks)(file);
|
|
123
|
+
const parts = [];
|
|
124
|
+
for (let i = 0; i < totalChunks.length; i++) {
|
|
125
|
+
const data = await (0, utils_js_1.fetchData)({
|
|
126
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
127
|
+
apiKey: this.#accessKey,
|
|
128
|
+
stagingUrl: this.#stagingUrl,
|
|
129
|
+
body: {
|
|
130
|
+
objectKey: res?.key,
|
|
131
|
+
uploadId: res?.uploadId,
|
|
132
|
+
partNumber: i + 1,
|
|
133
|
+
},
|
|
134
|
+
throwError: true,
|
|
135
|
+
});
|
|
136
|
+
const val = await axios_1.default.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
|
|
137
|
+
parts.push({ PartNumber: i + 1, ETag: val?.headers?.etag });
|
|
138
|
+
}
|
|
139
|
+
const completeValue = await (0, utils_js_1.fetchData)({
|
|
140
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
|
|
141
|
+
apiKey: this.#accessKey,
|
|
142
|
+
stagingUrl: this.#stagingUrl,
|
|
143
|
+
body: {
|
|
144
|
+
uploadId: res?.uploadId,
|
|
145
|
+
objectKey: res?.key,
|
|
146
|
+
parts,
|
|
147
|
+
originalFileName: file.name,
|
|
148
|
+
},
|
|
149
|
+
throwError: true,
|
|
150
|
+
});
|
|
151
|
+
resolve({ message: "successfully uploaded", data: completeValue });
|
|
152
|
+
} catch (error) {
|
|
153
|
+
reject(error);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
154
157
|
}
|
|
155
158
|
exports.default = FileUpload;
|