@swan-admin/swan-ai-measurements 1.0.85 → 1.0.87
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/constants.d.ts +1 -1
- package/dist/constants.js +2 -2
- package/dist/esm/constants.d.ts +1 -1
- package/dist/esm/constants.js +1 -1
- package/dist/esm/fileUpload.d.ts +4 -1
- package/dist/esm/fileUpload.js +46 -16
- package/dist/esm/utils.d.ts +2 -0
- package/dist/esm/utils.js +17 -2
- package/dist/fileUpload.d.ts +4 -1
- package/dist/fileUpload.js +44 -13
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +19 -3
- package/package.json +1 -1
- package/src/constants.ts +1 -1
- package/src/fileUpload.ts +44 -17
- package/src/utils.ts +21 -4
package/dist/constants.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare const APP_AUTH_WEBSOCKET_URL: string;
|
|
|
4
4
|
export declare const APP_TRY_ON_WEBSOCKET_URL: string;
|
|
5
5
|
export declare const APP_RECOMMENDATION_WEBSOCKET_URL: string;
|
|
6
6
|
export declare const APP_POSE_DETECTION_WEBSOCKET_URL: string;
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const FILE_UPLOAD_ENDPOINT: {
|
|
8
8
|
UPLOAD_START: string;
|
|
9
9
|
UPLOAD_COMPLETE: string;
|
|
10
10
|
UPLOAD_SIGN_PART: string;
|
package/dist/constants.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.REQUIRED_MESSAGE_FOR_META_DATA = exports.REQUIRED_MESSAGE = exports.requiredMetaData = exports.API_ENDPOINTS = exports.
|
|
3
|
+
exports.REQUIRED_MESSAGE_FOR_META_DATA = exports.REQUIRED_MESSAGE = exports.requiredMetaData = exports.API_ENDPOINTS = exports.FILE_UPLOAD_ENDPOINT = exports.APP_POSE_DETECTION_WEBSOCKET_URL = exports.APP_RECOMMENDATION_WEBSOCKET_URL = exports.APP_TRY_ON_WEBSOCKET_URL = exports.APP_AUTH_WEBSOCKET_URL = exports.APP_AUTH_BASE_URL = exports.APP_BASE_URL = void 0;
|
|
4
4
|
exports.APP_BASE_URL = "https://fitview-server-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
|
|
5
5
|
exports.APP_AUTH_BASE_URL = "https://staging.api.getswan.co";
|
|
6
6
|
exports.APP_AUTH_WEBSOCKET_URL = "wss://staging.wsnotify.api.getswan.co";
|
|
7
7
|
exports.APP_TRY_ON_WEBSOCKET_URL = "wss://bucbzczxjk.execute-api.ap-south-1.amazonaws.com";
|
|
8
8
|
exports.APP_RECOMMENDATION_WEBSOCKET_URL = "wss://staging.wsnotify.api.getswan.co/scanning";
|
|
9
9
|
exports.APP_POSE_DETECTION_WEBSOCKET_URL = "https://posedetect-service-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
|
|
10
|
-
exports.
|
|
10
|
+
exports.FILE_UPLOAD_ENDPOINT = {
|
|
11
11
|
UPLOAD_START: "/upload/start",
|
|
12
12
|
UPLOAD_COMPLETE: "/upload/complete",
|
|
13
13
|
UPLOAD_SIGN_PART: "/upload/signpart",
|
package/dist/esm/constants.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare const APP_AUTH_WEBSOCKET_URL: string;
|
|
|
4
4
|
export declare const APP_TRY_ON_WEBSOCKET_URL: string;
|
|
5
5
|
export declare const APP_RECOMMENDATION_WEBSOCKET_URL: string;
|
|
6
6
|
export declare const APP_POSE_DETECTION_WEBSOCKET_URL: string;
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const FILE_UPLOAD_ENDPOINT: {
|
|
8
8
|
UPLOAD_START: string;
|
|
9
9
|
UPLOAD_COMPLETE: string;
|
|
10
10
|
UPLOAD_SIGN_PART: string;
|
package/dist/esm/constants.js
CHANGED
|
@@ -4,7 +4,7 @@ export const APP_AUTH_WEBSOCKET_URL = "wss://staging.wsnotify.api.getswan.co";
|
|
|
4
4
|
export const APP_TRY_ON_WEBSOCKET_URL = "wss://bucbzczxjk.execute-api.ap-south-1.amazonaws.com";
|
|
5
5
|
export const APP_RECOMMENDATION_WEBSOCKET_URL = "wss://staging.wsnotify.api.getswan.co/scanning";
|
|
6
6
|
export const APP_POSE_DETECTION_WEBSOCKET_URL = "https://posedetect-service-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
|
|
7
|
-
export const
|
|
7
|
+
export const FILE_UPLOAD_ENDPOINT = {
|
|
8
8
|
UPLOAD_START: "/upload/start",
|
|
9
9
|
UPLOAD_COMPLETE: "/upload/complete",
|
|
10
10
|
UPLOAD_SIGN_PART: "/upload/signpart",
|
package/dist/esm/fileUpload.d.ts
CHANGED
|
@@ -17,6 +17,9 @@ interface UploadOptions {
|
|
|
17
17
|
export default class FileUpload {
|
|
18
18
|
#private;
|
|
19
19
|
constructor(accessKey: string);
|
|
20
|
-
|
|
20
|
+
uploadFileFrontend({ file, arrayMetaData, scanId }: UploadOptions): Promise<unknown>;
|
|
21
|
+
uploadFileBackend({ file, arrayMetaData, scanId }: UploadOptions): Promise<{
|
|
22
|
+
message: string;
|
|
23
|
+
}>;
|
|
21
24
|
}
|
|
22
25
|
export {};
|
package/dist/esm/fileUpload.js
CHANGED
|
@@ -47,8 +47,9 @@ var __classPrivateFieldGet =
|
|
|
47
47
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
48
48
|
};
|
|
49
49
|
var _FileUpload_uppyIns, _FileUpload_accessKey;
|
|
50
|
-
import
|
|
51
|
-
import {
|
|
50
|
+
import axios from "axios";
|
|
51
|
+
import { REQUIRED_MESSAGE, REQUIRED_MESSAGE_FOR_META_DATA, FILE_UPLOAD_ENDPOINT } from "./constants.js";
|
|
52
|
+
import { checkMetaDataValue, checkParameters, fetchData, getFileChunks } from "./utils.js";
|
|
52
53
|
import Uppy from "@uppy/core";
|
|
53
54
|
import AwsS3Multipart from "@uppy/aws-s3-multipart";
|
|
54
55
|
class FileUpload {
|
|
@@ -57,7 +58,7 @@ class FileUpload {
|
|
|
57
58
|
_FileUpload_accessKey.set(this, void 0);
|
|
58
59
|
__classPrivateFieldSet(this, _FileUpload_accessKey, accessKey, "f");
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
+
uploadFileFrontend(_a) {
|
|
61
62
|
return __awaiter(this, arguments, void 0, function* ({ file, arrayMetaData, scanId }) {
|
|
62
63
|
if (!checkParameters(file, arrayMetaData, scanId)) {
|
|
63
64
|
throw new Error(REQUIRED_MESSAGE);
|
|
@@ -77,7 +78,7 @@ class FileUpload {
|
|
|
77
78
|
createMultipartUpload: (file) => {
|
|
78
79
|
const objectKey = `${scanId}.${file.extension}`;
|
|
79
80
|
return fetchData({
|
|
80
|
-
path:
|
|
81
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
81
82
|
apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
|
|
82
83
|
body: {
|
|
83
84
|
objectKey,
|
|
@@ -88,7 +89,7 @@ class FileUpload {
|
|
|
88
89
|
},
|
|
89
90
|
completeMultipartUpload: (file, { uploadId, key, parts }) =>
|
|
90
91
|
fetchData({
|
|
91
|
-
path:
|
|
92
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
|
|
92
93
|
apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
|
|
93
94
|
body: {
|
|
94
95
|
uploadId,
|
|
@@ -99,7 +100,7 @@ class FileUpload {
|
|
|
99
100
|
}),
|
|
100
101
|
signPart: (file, partData) =>
|
|
101
102
|
fetchData({
|
|
102
|
-
path:
|
|
103
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
103
104
|
apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
|
|
104
105
|
body: {
|
|
105
106
|
objectKey: partData.key,
|
|
@@ -107,16 +108,6 @@ class FileUpload {
|
|
|
107
108
|
partNumber: partData.partNumber,
|
|
108
109
|
},
|
|
109
110
|
}),
|
|
110
|
-
abortMultipartUpload: (file, { uploadId, key }) =>
|
|
111
|
-
fetchData({
|
|
112
|
-
path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_ABORT,
|
|
113
|
-
apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
|
|
114
|
-
body: {
|
|
115
|
-
uploadId,
|
|
116
|
-
objectKey: key,
|
|
117
|
-
originalFileName: file.name,
|
|
118
|
-
},
|
|
119
|
-
}),
|
|
120
111
|
});
|
|
121
112
|
__classPrivateFieldGet(this, _FileUpload_uppyIns, "f").addFile({
|
|
122
113
|
source: "manual",
|
|
@@ -138,6 +129,45 @@ class FileUpload {
|
|
|
138
129
|
});
|
|
139
130
|
});
|
|
140
131
|
}
|
|
132
|
+
uploadFileBackend(_a) {
|
|
133
|
+
return __awaiter(this, arguments, void 0, function* ({ file, arrayMetaData, scanId }) {
|
|
134
|
+
if (!checkParameters(file, arrayMetaData, scanId)) {
|
|
135
|
+
throw new Error(REQUIRED_MESSAGE);
|
|
136
|
+
}
|
|
137
|
+
if (!checkMetaDataValue(arrayMetaData)) {
|
|
138
|
+
throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const res = yield fetchData({
|
|
142
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
143
|
+
apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
|
|
144
|
+
body: {
|
|
145
|
+
objectKey: file.name,
|
|
146
|
+
contentType: file.type,
|
|
147
|
+
objectMetadata: arrayMetaData,
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const totalChunks = getFileChunks(file);
|
|
151
|
+
for (let i = 0; i < totalChunks.length; i++) {
|
|
152
|
+
const data = yield fetchData({
|
|
153
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
154
|
+
apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
|
|
155
|
+
body: {
|
|
156
|
+
objectKey: res === null || res === void 0 ? void 0 : res.key,
|
|
157
|
+
uploadId: res === null || res === void 0 ? void 0 : res.uploadId,
|
|
158
|
+
partNumber: i + 1,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
yield axios.put(data === null || data === void 0 ? void 0 : data.url, totalChunks[i], {
|
|
162
|
+
headers: { "Content-Type": file.type, "X-Api-Key": __classPrivateFieldGet(this, _FileUpload_accessKey, "f") },
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return { message: "successfully uploaded" };
|
|
166
|
+
} catch (error) {
|
|
167
|
+
throw new Error(`Failed to upload: ${error === null || error === void 0 ? void 0 : error.message}`);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
141
171
|
}
|
|
142
172
|
(_FileUpload_uppyIns = new WeakMap()), (_FileUpload_accessKey = new WeakMap());
|
|
143
173
|
export default FileUpload;
|
package/dist/esm/utils.d.ts
CHANGED
|
@@ -6,7 +6,9 @@ export interface FetchDataOptions {
|
|
|
6
6
|
baseUrl?: string;
|
|
7
7
|
apiKey?: string;
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
|
+
throwError?: boolean;
|
|
9
10
|
}
|
|
10
11
|
export declare function fetchData(options: FetchDataOptions): Promise<any>;
|
|
11
12
|
export declare function checkParameters(...args: any[]): boolean;
|
|
12
13
|
export declare function checkMetaDataValue(arr: ObjMetaData[]): boolean;
|
|
14
|
+
export declare function getFileChunks(file: File, chunkSize?: number): Blob[];
|
package/dist/esm/utils.js
CHANGED
|
@@ -11,8 +11,7 @@ import axios from "axios";
|
|
|
11
11
|
import { APP_AUTH_BASE_URL, requiredMetaData } from "./constants.js";
|
|
12
12
|
export function fetchData(options) {
|
|
13
13
|
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
-
const { path, body, queryParams, baseUrl = APP_AUTH_BASE_URL, apiKey = "", headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" }, } = options;
|
|
15
|
-
console.log(body, "body", path, "path");
|
|
14
|
+
const { path, body, queryParams, baseUrl = APP_AUTH_BASE_URL, apiKey = "", throwError = false, headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" }, } = options;
|
|
16
15
|
const apiUrl = `${baseUrl}${path}${queryParams ? `?${new URLSearchParams(queryParams)}` : ""}`;
|
|
17
16
|
try {
|
|
18
17
|
const res = yield axios.post(apiUrl, body, { headers });
|
|
@@ -20,10 +19,16 @@ export function fetchData(options) {
|
|
|
20
19
|
return res.data;
|
|
21
20
|
}
|
|
22
21
|
console.error(`Error: Unexpected response status ${res.status}`);
|
|
22
|
+
if (throwError) {
|
|
23
|
+
throw new Error(`Failed to upload`);
|
|
24
|
+
}
|
|
23
25
|
return {};
|
|
24
26
|
}
|
|
25
27
|
catch (error) {
|
|
26
28
|
console.error(error, "while uploading");
|
|
29
|
+
if (throwError) {
|
|
30
|
+
throw new Error(`Failed to upload: ${error.message}`);
|
|
31
|
+
}
|
|
27
32
|
return {};
|
|
28
33
|
}
|
|
29
34
|
});
|
|
@@ -60,3 +65,13 @@ export function checkMetaDataValue(arr) {
|
|
|
60
65
|
}
|
|
61
66
|
return true;
|
|
62
67
|
}
|
|
68
|
+
export function getFileChunks(file, chunkSize = 5 * 1024 * 1024) {
|
|
69
|
+
const totalSize = file.size;
|
|
70
|
+
const chunks = [];
|
|
71
|
+
let start = 0;
|
|
72
|
+
while (start < totalSize) {
|
|
73
|
+
chunks.push(file.slice(start, start + chunkSize));
|
|
74
|
+
start += chunkSize;
|
|
75
|
+
}
|
|
76
|
+
return chunks;
|
|
77
|
+
}
|
package/dist/fileUpload.d.ts
CHANGED
|
@@ -17,6 +17,9 @@ interface UploadOptions {
|
|
|
17
17
|
export default class FileUpload {
|
|
18
18
|
#private;
|
|
19
19
|
constructor(accessKey: string);
|
|
20
|
-
|
|
20
|
+
uploadFileFrontend({ file, arrayMetaData, scanId }: UploadOptions): Promise<unknown>;
|
|
21
|
+
uploadFileBackend({ file, arrayMetaData, scanId }: UploadOptions): Promise<{
|
|
22
|
+
message: string;
|
|
23
|
+
}>;
|
|
21
24
|
}
|
|
22
25
|
export {};
|
package/dist/fileUpload.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const axios_1 = __importDefault(require("axios"));
|
|
3
7
|
const constants_js_1 = require("./constants.js");
|
|
4
8
|
const utils_js_1 = require("./utils.js");
|
|
5
9
|
const Uppy = require("fix-esm").require("@uppy/core");
|
|
@@ -10,7 +14,7 @@ class FileUpload {
|
|
|
10
14
|
constructor(accessKey) {
|
|
11
15
|
this.#accessKey = accessKey;
|
|
12
16
|
}
|
|
13
|
-
async
|
|
17
|
+
async uploadFileFrontend({ file, arrayMetaData, scanId }) {
|
|
14
18
|
if (!(0, utils_js_1.checkParameters)(file, arrayMetaData, scanId)) {
|
|
15
19
|
throw new Error(constants_js_1.REQUIRED_MESSAGE);
|
|
16
20
|
}
|
|
@@ -29,7 +33,7 @@ class FileUpload {
|
|
|
29
33
|
createMultipartUpload: (file) => {
|
|
30
34
|
const objectKey = `${scanId}.${file.extension}`;
|
|
31
35
|
return (0, utils_js_1.fetchData)({
|
|
32
|
-
path: constants_js_1.
|
|
36
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
33
37
|
apiKey: this.#accessKey,
|
|
34
38
|
body: {
|
|
35
39
|
objectKey,
|
|
@@ -39,7 +43,7 @@ class FileUpload {
|
|
|
39
43
|
});
|
|
40
44
|
},
|
|
41
45
|
completeMultipartUpload: (file, { uploadId, key, parts }) => (0, utils_js_1.fetchData)({
|
|
42
|
-
path: constants_js_1.
|
|
46
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
|
|
43
47
|
apiKey: this.#accessKey,
|
|
44
48
|
body: {
|
|
45
49
|
uploadId,
|
|
@@ -49,7 +53,7 @@ class FileUpload {
|
|
|
49
53
|
},
|
|
50
54
|
}),
|
|
51
55
|
signPart: (file, partData) => (0, utils_js_1.fetchData)({
|
|
52
|
-
path: constants_js_1.
|
|
56
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
53
57
|
apiKey: this.#accessKey,
|
|
54
58
|
body: {
|
|
55
59
|
objectKey: partData.key,
|
|
@@ -57,15 +61,6 @@ class FileUpload {
|
|
|
57
61
|
partNumber: partData.partNumber,
|
|
58
62
|
},
|
|
59
63
|
}),
|
|
60
|
-
abortMultipartUpload: (file, { uploadId, key }) => (0, utils_js_1.fetchData)({
|
|
61
|
-
path: constants_js_1.UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_ABORT,
|
|
62
|
-
apiKey: this.#accessKey,
|
|
63
|
-
body: {
|
|
64
|
-
uploadId,
|
|
65
|
-
objectKey: key,
|
|
66
|
-
originalFileName: file.name,
|
|
67
|
-
},
|
|
68
|
-
}),
|
|
69
64
|
});
|
|
70
65
|
this.#uppyIns.addFile({
|
|
71
66
|
source: "manual",
|
|
@@ -86,5 +81,41 @@ class FileUpload {
|
|
|
86
81
|
});
|
|
87
82
|
});
|
|
88
83
|
}
|
|
84
|
+
async uploadFileBackend({ file, arrayMetaData, scanId }) {
|
|
85
|
+
if (!(0, utils_js_1.checkParameters)(file, arrayMetaData, scanId)) {
|
|
86
|
+
throw new Error(constants_js_1.REQUIRED_MESSAGE);
|
|
87
|
+
}
|
|
88
|
+
if (!(0, utils_js_1.checkMetaDataValue)(arrayMetaData)) {
|
|
89
|
+
throw new Error(constants_js_1.REQUIRED_MESSAGE_FOR_META_DATA);
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
const res = await (0, utils_js_1.fetchData)({
|
|
93
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
94
|
+
apiKey: this.#accessKey,
|
|
95
|
+
body: {
|
|
96
|
+
objectKey: file.name,
|
|
97
|
+
contentType: file.type,
|
|
98
|
+
objectMetadata: arrayMetaData,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
const totalChunks = (0, utils_js_1.getFileChunks)(file);
|
|
102
|
+
for (let i = 0; i < totalChunks.length; i++) {
|
|
103
|
+
const data = await (0, utils_js_1.fetchData)({
|
|
104
|
+
path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
105
|
+
apiKey: this.#accessKey,
|
|
106
|
+
body: {
|
|
107
|
+
objectKey: res?.key,
|
|
108
|
+
uploadId: res?.uploadId,
|
|
109
|
+
partNumber: i + 1,
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
await axios_1.default.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
|
|
113
|
+
}
|
|
114
|
+
return { message: "successfully uploaded" };
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
throw new Error(`Failed to upload: ${error?.message}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
89
120
|
}
|
|
90
121
|
exports.default = FileUpload;
|
package/dist/utils.d.ts
CHANGED
|
@@ -6,7 +6,9 @@ export interface FetchDataOptions {
|
|
|
6
6
|
baseUrl?: string;
|
|
7
7
|
apiKey?: string;
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
|
+
throwError?: boolean;
|
|
9
10
|
}
|
|
10
11
|
export declare function fetchData(options: FetchDataOptions): Promise<any>;
|
|
11
12
|
export declare function checkParameters(...args: any[]): boolean;
|
|
12
13
|
export declare function checkMetaDataValue(arr: ObjMetaData[]): boolean;
|
|
14
|
+
export declare function getFileChunks(file: File, chunkSize?: number): Blob[];
|
package/dist/utils.js
CHANGED
|
@@ -3,12 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.checkMetaDataValue = exports.checkParameters = exports.fetchData = void 0;
|
|
6
|
+
exports.getFileChunks = exports.checkMetaDataValue = exports.checkParameters = exports.fetchData = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const constants_js_1 = require("./constants.js");
|
|
9
9
|
async function fetchData(options) {
|
|
10
|
-
const { path, body, queryParams, baseUrl = constants_js_1.APP_AUTH_BASE_URL, apiKey = "", headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" }, } = options;
|
|
11
|
-
console.log(body, "body", path, "path");
|
|
10
|
+
const { path, body, queryParams, baseUrl = constants_js_1.APP_AUTH_BASE_URL, apiKey = "", throwError = false, headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" }, } = options;
|
|
12
11
|
const apiUrl = `${baseUrl}${path}${queryParams ? `?${new URLSearchParams(queryParams)}` : ""}`;
|
|
13
12
|
try {
|
|
14
13
|
const res = await axios_1.default.post(apiUrl, body, { headers });
|
|
@@ -16,10 +15,16 @@ async function fetchData(options) {
|
|
|
16
15
|
return res.data;
|
|
17
16
|
}
|
|
18
17
|
console.error(`Error: Unexpected response status ${res.status}`);
|
|
18
|
+
if (throwError) {
|
|
19
|
+
throw new Error(`Failed to upload`);
|
|
20
|
+
}
|
|
19
21
|
return {};
|
|
20
22
|
}
|
|
21
23
|
catch (error) {
|
|
22
24
|
console.error(error, "while uploading");
|
|
25
|
+
if (throwError) {
|
|
26
|
+
throw new Error(`Failed to upload: ${error.message}`);
|
|
27
|
+
}
|
|
23
28
|
return {};
|
|
24
29
|
}
|
|
25
30
|
}
|
|
@@ -58,3 +63,14 @@ function checkMetaDataValue(arr) {
|
|
|
58
63
|
return true;
|
|
59
64
|
}
|
|
60
65
|
exports.checkMetaDataValue = checkMetaDataValue;
|
|
66
|
+
function getFileChunks(file, chunkSize = 5 * 1024 * 1024) {
|
|
67
|
+
const totalSize = file.size;
|
|
68
|
+
const chunks = [];
|
|
69
|
+
let start = 0;
|
|
70
|
+
while (start < totalSize) {
|
|
71
|
+
chunks.push(file.slice(start, start + chunkSize));
|
|
72
|
+
start += chunkSize;
|
|
73
|
+
}
|
|
74
|
+
return chunks;
|
|
75
|
+
}
|
|
76
|
+
exports.getFileChunks = getFileChunks;
|
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -5,7 +5,7 @@ export const APP_TRY_ON_WEBSOCKET_URL: string = "wss://bucbzczxjk.execute-api.ap
|
|
|
5
5
|
export const APP_RECOMMENDATION_WEBSOCKET_URL: string = "wss://staging.wsnotify.api.getswan.co/scanning";
|
|
6
6
|
export const APP_POSE_DETECTION_WEBSOCKET_URL: string = "https://posedetect-service-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
|
|
7
7
|
|
|
8
|
-
export const
|
|
8
|
+
export const FILE_UPLOAD_ENDPOINT: {
|
|
9
9
|
UPLOAD_START: string;
|
|
10
10
|
UPLOAD_COMPLETE: string;
|
|
11
11
|
UPLOAD_SIGN_PART: string;
|
package/src/fileUpload.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { REQUIRED_MESSAGE, REQUIRED_MESSAGE_FOR_META_DATA, FILE_UPLOAD_ENDPOINT } from "./constants.js";
|
|
3
|
+
import { checkMetaDataValue, checkParameters, fetchData, getFileChunks } from "./utils.js";
|
|
3
4
|
const Uppy = require("fix-esm").require("@uppy/core");
|
|
4
5
|
const AwsS3Multipart = require("fix-esm").require("@uppy/aws-s3-multipart");
|
|
5
6
|
interface ObjMetaData {
|
|
@@ -28,7 +29,7 @@ export default class FileUpload {
|
|
|
28
29
|
this.#accessKey = accessKey;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
async
|
|
32
|
+
async uploadFileFrontend({ file, arrayMetaData, scanId }: UploadOptions) {
|
|
32
33
|
if (!checkParameters(file, arrayMetaData, scanId)) {
|
|
33
34
|
throw new Error(REQUIRED_MESSAGE);
|
|
34
35
|
}
|
|
@@ -48,7 +49,7 @@ export default class FileUpload {
|
|
|
48
49
|
createMultipartUpload: (file: any) => {
|
|
49
50
|
const objectKey = `${scanId}.${file.extension}`;
|
|
50
51
|
return fetchData({
|
|
51
|
-
path:
|
|
52
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
52
53
|
apiKey: this.#accessKey,
|
|
53
54
|
body: {
|
|
54
55
|
objectKey,
|
|
@@ -59,7 +60,7 @@ export default class FileUpload {
|
|
|
59
60
|
},
|
|
60
61
|
completeMultipartUpload: (file: any, { uploadId, key, parts }: { uploadId: string | number; key: string | number; parts: any }) =>
|
|
61
62
|
fetchData({
|
|
62
|
-
path:
|
|
63
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
|
|
63
64
|
apiKey: this.#accessKey,
|
|
64
65
|
body: {
|
|
65
66
|
uploadId,
|
|
@@ -71,7 +72,7 @@ export default class FileUpload {
|
|
|
71
72
|
|
|
72
73
|
signPart: (file: any, partData: any) =>
|
|
73
74
|
fetchData({
|
|
74
|
-
path:
|
|
75
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
75
76
|
apiKey: this.#accessKey,
|
|
76
77
|
body: {
|
|
77
78
|
objectKey: partData.key,
|
|
@@ -79,17 +80,6 @@ export default class FileUpload {
|
|
|
79
80
|
partNumber: partData.partNumber,
|
|
80
81
|
},
|
|
81
82
|
}),
|
|
82
|
-
|
|
83
|
-
abortMultipartUpload: (file: any, { uploadId, key }: { uploadId: string | number; key: string | number }) =>
|
|
84
|
-
fetchData({
|
|
85
|
-
path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_ABORT,
|
|
86
|
-
apiKey: this.#accessKey,
|
|
87
|
-
body: {
|
|
88
|
-
uploadId,
|
|
89
|
-
objectKey: key,
|
|
90
|
-
originalFileName: file.name,
|
|
91
|
-
},
|
|
92
|
-
}),
|
|
93
83
|
});
|
|
94
84
|
|
|
95
85
|
this.#uppyIns.addFile({
|
|
@@ -112,4 +102,41 @@ export default class FileUpload {
|
|
|
112
102
|
});
|
|
113
103
|
});
|
|
114
104
|
}
|
|
105
|
+
|
|
106
|
+
async uploadFileBackend({ file, arrayMetaData, scanId }: UploadOptions) {
|
|
107
|
+
if (!checkParameters(file, arrayMetaData, scanId)) {
|
|
108
|
+
throw new Error(REQUIRED_MESSAGE);
|
|
109
|
+
}
|
|
110
|
+
if (!checkMetaDataValue(arrayMetaData)) {
|
|
111
|
+
throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const res: { key: string; uploadId: string } = await fetchData({
|
|
116
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
|
|
117
|
+
apiKey: this.#accessKey,
|
|
118
|
+
body: {
|
|
119
|
+
objectKey: file.name,
|
|
120
|
+
contentType: file.type,
|
|
121
|
+
objectMetadata: arrayMetaData,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
const totalChunks = getFileChunks(file);
|
|
125
|
+
for (let i = 0; i < totalChunks.length; i++) {
|
|
126
|
+
const data: { url: string } = await fetchData({
|
|
127
|
+
path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
|
|
128
|
+
apiKey: this.#accessKey,
|
|
129
|
+
body: {
|
|
130
|
+
objectKey: res?.key,
|
|
131
|
+
uploadId: res?.uploadId,
|
|
132
|
+
partNumber: i + 1,
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
await axios.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
|
|
136
|
+
}
|
|
137
|
+
return { message: "successfully uploaded" };
|
|
138
|
+
} catch (error: any) {
|
|
139
|
+
throw new Error(`Failed to upload: ${error?.message}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
115
142
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import axios, { AxiosResponse } from "axios";
|
|
1
|
+
import axios, { AxiosError, AxiosResponse } from "axios";
|
|
2
2
|
import { APP_AUTH_BASE_URL, ObjMetaData, requiredMetaData } from "./constants.js";
|
|
3
3
|
|
|
4
4
|
export interface FetchDataOptions {
|
|
@@ -8,6 +8,7 @@ export interface FetchDataOptions {
|
|
|
8
8
|
baseUrl?: string;
|
|
9
9
|
apiKey?: string;
|
|
10
10
|
headers?: Record<string, string>;
|
|
11
|
+
throwError?: boolean;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export async function fetchData(options: FetchDataOptions): Promise<any> {
|
|
@@ -17,11 +18,10 @@ export async function fetchData(options: FetchDataOptions): Promise<any> {
|
|
|
17
18
|
queryParams,
|
|
18
19
|
baseUrl = APP_AUTH_BASE_URL,
|
|
19
20
|
apiKey = "",
|
|
21
|
+
throwError = false,
|
|
20
22
|
headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" },
|
|
21
23
|
} = options;
|
|
22
24
|
|
|
23
|
-
console.log(body, "body", path, "path");
|
|
24
|
-
|
|
25
25
|
const apiUrl = `${baseUrl}${path}${queryParams ? `?${new URLSearchParams(queryParams)}` : ""}`;
|
|
26
26
|
try {
|
|
27
27
|
const res: AxiosResponse<any> = await axios.post(apiUrl, body, { headers });
|
|
@@ -29,9 +29,15 @@ export async function fetchData(options: FetchDataOptions): Promise<any> {
|
|
|
29
29
|
return res.data;
|
|
30
30
|
}
|
|
31
31
|
console.error(`Error: Unexpected response status ${res.status}`);
|
|
32
|
+
if (throwError) {
|
|
33
|
+
throw new Error(`Failed to upload`);
|
|
34
|
+
}
|
|
32
35
|
return {};
|
|
33
|
-
} catch (error) {
|
|
36
|
+
} catch (error: any) {
|
|
34
37
|
console.error(error, "while uploading");
|
|
38
|
+
if (throwError) {
|
|
39
|
+
throw new Error(`Failed to upload: ${error.message}`);
|
|
40
|
+
}
|
|
35
41
|
return {};
|
|
36
42
|
}
|
|
37
43
|
}
|
|
@@ -69,3 +75,14 @@ export function checkMetaDataValue(arr: ObjMetaData[]): boolean {
|
|
|
69
75
|
}
|
|
70
76
|
return true;
|
|
71
77
|
}
|
|
78
|
+
|
|
79
|
+
export function getFileChunks(file: File, chunkSize = 5 * 1024 * 1024) {
|
|
80
|
+
const totalSize = file.size;
|
|
81
|
+
const chunks = [];
|
|
82
|
+
let start = 0;
|
|
83
|
+
while (start < totalSize) {
|
|
84
|
+
chunks.push(file.slice(start, start + chunkSize));
|
|
85
|
+
start += chunkSize;
|
|
86
|
+
}
|
|
87
|
+
return chunks;
|
|
88
|
+
}
|