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

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,60 @@ 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
+ console.log(parts, "parts");
176
+ const completeValue = yield fetchData({
177
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
178
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
179
+ body: {
180
+ uploadId: res === null || res === void 0 ? void 0 : res.uploadId,
181
+ objectKey: res === null || res === void 0 ? void 0 : res.key,
182
+ parts,
183
+ originalFileName: file.name,
184
+ },
185
+ throwError: true,
186
+ });
187
+ console.log(completeValue, "after complete");
188
+ resolve({ message: "successfully uploaded", data: completeValue });
189
+ } catch (error) {
190
+ reject(error);
191
+ }
192
+ })
193
+ );
174
194
  });
175
195
  }
176
196
  }
@@ -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,57 @@ 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,
95
+ apiKey: this.#accessKey,
96
+ body: {
97
+ objectKey: file.name,
98
+ contentType: file.type,
99
+ objectMetadata: arrayMetaData,
100
+ },
101
+ throwError: true,
102
+ });
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
+ console.log(parts, "parts");
124
+ const completeValue = await (0, utils_js_1.fetchData)({
125
+ path: constants_js_1.FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
108
126
  apiKey: this.#accessKey,
109
127
  body: {
110
- objectKey: res?.key,
111
128
  uploadId: res?.uploadId,
112
- partNumber: i + 1,
129
+ objectKey: res?.key,
130
+ parts,
131
+ originalFileName: file.name,
113
132
  },
114
133
  throwError: true,
115
134
  });
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 } });
135
+ console.log(completeValue, "after complete");
136
+ resolve({ message: "successfully uploaded", data: completeValue });
118
137
  }
119
- return { message: "successfully uploaded" };
120
- }
121
- catch (error) {
122
- throw new Error(`Failed to upload: ${error?.message || "something went wrong"}`);
123
- }
138
+ catch (error) {
139
+ reject(error);
140
+ }
141
+ });
124
142
  }
125
143
  }
126
144
  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.91",
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,56 @@ 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,
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
+ 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
+ console.log(parts, "parts");
146
+ const completeValue = await fetchData({
147
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
131
148
  apiKey: this.#accessKey,
132
149
  body: {
133
- objectKey: res?.key,
134
150
  uploadId: res?.uploadId,
135
- partNumber: i + 1,
151
+ objectKey: res?.key,
152
+ parts,
153
+ originalFileName: file.name,
136
154
  },
137
155
  throwError: true,
138
156
  });
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 } });
157
+ console.log(completeValue, "after complete");
158
+
159
+ resolve({ message: "successfully uploaded", data: completeValue });
160
+ } catch (error: any) {
161
+ reject(error);
141
162
  }
142
- return { message: "successfully uploaded" };
143
- } catch (error: any) {
144
- throw new Error(`Failed to upload: ${error?.message || "something went wrong"}`);
145
- }
163
+ });
146
164
  }
147
165
  }
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;