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