@swan-admin/swan-ai-measurements 1.0.89 → 1.0.90

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.
@@ -18,8 +18,6 @@ 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
+ uploadFileBackend({ file, arrayMetaData, scanId }: UploadOptions): Promise<unknown>;
24
22
  }
25
23
  export {};
@@ -137,40 +137,57 @@ class FileUpload {
137
137
  if (!checkMetaDataValue(arrayMetaData)) {
138
138
  throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
139
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
- throwError: true,
150
- });
151
- console.log(res, "res for start");
152
- const totalChunks = getFileChunks(file);
153
- console.log(totalChunks, "total chunks");
154
- for (let i = 0; i < totalChunks.length; i++) {
155
- const data = yield fetchData({
156
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
157
- apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
158
- body: {
159
- objectKey: res === null || res === void 0 ? void 0 : res.key,
160
- uploadId: res === null || res === void 0 ? void 0 : res.uploadId,
161
- partNumber: i + 1,
162
- },
163
- throwError: true,
164
- });
165
- console.log(data, "data for signed url");
166
- yield axios.put(data === null || data === void 0 ? void 0 : data.url, totalChunks[i], {
167
- headers: { "Content-Type": file.type, "X-Api-Key": __classPrivateFieldGet(this, _FileUpload_accessKey, "f") },
168
- });
169
- }
170
- return { message: "successfully uploaded" };
171
- } catch (error) {
172
- throw new Error(`Failed to upload: ${(error === null || error === void 0 ? void 0 : error.message) || "something went wrong"}`);
173
- }
140
+ return new Promise((resolve, reject) =>
141
+ __awaiter(this, void 0, void 0, function* () {
142
+ try {
143
+ const res = yield fetchData({
144
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
145
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
146
+ body: {
147
+ objectKey: file.name,
148
+ contentType: file.type,
149
+ objectMetadata: arrayMetaData,
150
+ },
151
+ throwError: true,
152
+ });
153
+ console.log(res, "res for start");
154
+ const totalChunks = getFileChunks(file);
155
+ console.log(totalChunks, "total chunks");
156
+ const parts = [];
157
+ for (let i = 0; i < totalChunks.length; i++) {
158
+ const data = yield fetchData({
159
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
160
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
161
+ body: {
162
+ objectKey: res === null || res === void 0 ? void 0 : res.key,
163
+ uploadId: res === null || res === void 0 ? void 0 : res.uploadId,
164
+ partNumber: i + 1,
165
+ },
166
+ throwError: true,
167
+ });
168
+ console.log(data, "data for signed url");
169
+ const val = yield axios.put(data === null || data === void 0 ? void 0 : data.url, totalChunks[i], {
170
+ headers: { "Content-Type": file.type, "X-Api-Key": __classPrivateFieldGet(this, _FileUpload_accessKey, "f") },
171
+ });
172
+ parts.push({ PartNumber: i + 1, ETag: '"958057f9cd1d264e94fcc0d2ccabc09f"' });
173
+ console.log(val === null || val === void 0 ? void 0 : val.data, "after uploading");
174
+ }
175
+ const completeValue = yield fetchData({
176
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
177
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
178
+ body: {
179
+ uploadId: res === null || res === void 0 ? void 0 : res.uploadId,
180
+ objectKey: res === null || res === void 0 ? void 0 : res.key,
181
+ parts,
182
+ originalFileName: file.name,
183
+ },
184
+ });
185
+ resolve({ message: "successfully uploaded", data: completeValue });
186
+ } catch (error) {
187
+ reject(error);
188
+ }
189
+ })
190
+ );
174
191
  });
175
192
  }
176
193
  }
@@ -18,8 +18,6 @@ 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
+ uploadFileBackend({ file, arrayMetaData, scanId }: UploadOptions): Promise<unknown>;
24
22
  }
25
23
  export {};
@@ -88,39 +88,54 @@ class FileUpload {
88
88
  if (!(0, utils_js_1.checkMetaDataValue)(arrayMetaData)) {
89
89
  throw new Error(constants_js_1.REQUIRED_MESSAGE_FOR_META_DATA);
90
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
- throwError: true,
101
- });
102
- console.log(res, "res for start");
103
- const totalChunks = (0, utils_js_1.getFileChunks)(file);
104
- console.log(totalChunks, "total chunks");
105
- for (let i = 0; i < totalChunks.length; i++) {
106
- const data = await (0, utils_js_1.fetchData)({
107
- path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
91
+ return new Promise(async (resolve, reject) => {
92
+ try {
93
+ const res = await (0, utils_js_1.fetchData)({
94
+ path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_START,
108
95
  apiKey: this.#accessKey,
109
96
  body: {
110
- objectKey: res?.key,
111
- uploadId: res?.uploadId,
112
- partNumber: i + 1,
97
+ objectKey: file.name,
98
+ contentType: file.type,
99
+ objectMetadata: arrayMetaData,
113
100
  },
114
101
  throwError: true,
115
102
  });
116
- console.log(data, "data for signed url");
117
- await axios_1.default.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
103
+ console.log(res, "res for start");
104
+ const totalChunks = (0, utils_js_1.getFileChunks)(file);
105
+ console.log(totalChunks, "total chunks");
106
+ const parts = [];
107
+ for (let i = 0; i < totalChunks.length; i++) {
108
+ const data = await (0, utils_js_1.fetchData)({
109
+ path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
110
+ apiKey: this.#accessKey,
111
+ body: {
112
+ objectKey: res?.key,
113
+ uploadId: res?.uploadId,
114
+ partNumber: i + 1,
115
+ },
116
+ throwError: true,
117
+ });
118
+ console.log(data, "data for signed url");
119
+ const val = await axios_1.default.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
120
+ parts.push({ PartNumber: i + 1, ETag: '"958057f9cd1d264e94fcc0d2ccabc09f"' });
121
+ console.log(val?.data, "after uploading");
122
+ }
123
+ const completeValue = await (0, utils_js_1.fetchData)({
124
+ path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
125
+ apiKey: this.#accessKey,
126
+ body: {
127
+ uploadId: res?.uploadId,
128
+ objectKey: res?.key,
129
+ parts,
130
+ originalFileName: file.name,
131
+ },
132
+ });
133
+ resolve({ message: "successfully uploaded", data: completeValue });
118
134
  }
119
- return { message: "successfully uploaded" };
120
- }
121
- catch (error) {
122
- throw new Error(`Failed to upload: ${error?.message || "something went wrong"}`);
123
- }
135
+ catch (error) {
136
+ reject(error);
137
+ }
138
+ });
124
139
  }
125
140
  }
126
141
  exports.default = FileUpload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.0.89",
3
+ "version": "1.0.90",
4
4
  "description": "provides ai measurement suggestion",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/fileUpload.ts CHANGED
@@ -110,38 +110,52 @@ export default class FileUpload {
110
110
  if (!checkMetaDataValue(arrayMetaData)) {
111
111
  throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
112
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
- throwError: true,
124
- });
125
- console.log(res, "res for start");
126
- const totalChunks = getFileChunks(file);
127
- console.log(totalChunks, "total chunks");
128
- for (let i = 0; i < totalChunks.length; i++) {
129
- const data: { url: string } = await fetchData({
130
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
113
+ return new Promise(async (resolve, reject) => {
114
+ try {
115
+ const res: { key: string; uploadId: string } = await fetchData({
116
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
131
117
  apiKey: this.#accessKey,
132
118
  body: {
133
- objectKey: res?.key,
134
- uploadId: res?.uploadId,
135
- partNumber: i + 1,
119
+ objectKey: file.name,
120
+ contentType: file.type,
121
+ objectMetadata: arrayMetaData,
136
122
  },
137
123
  throwError: true,
138
124
  });
139
- console.log(data, "data for signed url");
140
- await axios.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
125
+ console.log(res, "res for start");
126
+ const totalChunks = getFileChunks(file);
127
+ console.log(totalChunks, "total chunks");
128
+ const parts = [];
129
+ for (let i = 0; i < totalChunks.length; i++) {
130
+ const data: { url: string } = await fetchData({
131
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
132
+ apiKey: this.#accessKey,
133
+ body: {
134
+ objectKey: res?.key,
135
+ uploadId: res?.uploadId,
136
+ partNumber: i + 1,
137
+ },
138
+ throwError: true,
139
+ });
140
+ console.log(data, "data for signed url");
141
+ const val = await axios.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
142
+ parts.push({ PartNumber: i + 1, ETag: '"958057f9cd1d264e94fcc0d2ccabc09f"' });
143
+ console.log(val?.data, "after uploading");
144
+ }
145
+ const completeValue = await fetchData({
146
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
147
+ apiKey: this.#accessKey,
148
+ body: {
149
+ uploadId: res?.uploadId,
150
+ objectKey: res?.key,
151
+ parts,
152
+ originalFileName: file.name,
153
+ },
154
+ });
155
+ resolve({ message: "successfully uploaded", data: completeValue });
156
+ } catch (error: any) {
157
+ reject(error);
141
158
  }
142
- return { message: "successfully uploaded" };
143
- } catch (error: any) {
144
- throw new Error(`Failed to upload: ${error?.message || "something went wrong"}`);
145
- }
159
+ });
146
160
  }
147
161
  }
package/src/utils.ts CHANGED
@@ -76,7 +76,7 @@ export function checkMetaDataValue(arr: ObjMetaData[]): boolean {
76
76
  return true;
77
77
  }
78
78
 
79
- export function getFileChunks(file: File, chunkSize = 5 * 1024 * 1024) {
79
+ export function getFileChunks(file: File, chunkSize = 5 * 1024 * 1024): Blob[] {
80
80
  const totalSize = file.size;
81
81
  const chunks = [];
82
82
  let start = 0;