@tradly/asset 1.0.19 → 1.0.21

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.
@@ -164,26 +164,123 @@ var MediaApiService = /*#__PURE__*/function () {
164
164
  return fetchMedia;
165
165
  }()
166
166
  /**
167
- * Get S3 signed upload URLs from API
167
+ * Upload a single file to S3 using signed URL
168
+ * Handles both web File objects and React Native file URIs
168
169
  */
169
170
  )
170
171
  }, {
171
- key: "getS3SignedUrls",
172
+ key: "uploadFileToS3",
172
173
  value: (function () {
173
- var _getS3SignedUrls = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(files, authKey) {
174
- var auth_key, fileData, headers, _response$data$data, response, _t2;
174
+ var _uploadFileToS = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(signedUrl, fileUriOrFile, mimeType) {
175
+ var blob, response, _response, uploadResponse, errorText, _t2;
175
176
  return _regenerator().w(function (_context3) {
176
177
  while (1) switch (_context3.p = _context3.n) {
178
+ case 0:
179
+ _context3.p = 0;
180
+ if (!(typeof fileUriOrFile === "string")) {
181
+ _context3.n = 4;
182
+ break;
183
+ }
184
+ _context3.n = 1;
185
+ return fetch(fileUriOrFile);
186
+ case 1:
187
+ response = _context3.v;
188
+ if (response.ok) {
189
+ _context3.n = 2;
190
+ break;
191
+ }
192
+ throw new Error("Failed to fetch file: ".concat(response.status, " ").concat(response.statusText));
193
+ case 2:
194
+ _context3.n = 3;
195
+ return response.blob();
196
+ case 3:
197
+ blob = _context3.v;
198
+ _context3.n = 9;
199
+ break;
200
+ case 4:
201
+ if (!(fileUriOrFile && fileUriOrFile.uri)) {
202
+ _context3.n = 8;
203
+ break;
204
+ }
205
+ _context3.n = 5;
206
+ return fetch(fileUriOrFile.uri);
207
+ case 5:
208
+ _response = _context3.v;
209
+ if (_response.ok) {
210
+ _context3.n = 6;
211
+ break;
212
+ }
213
+ throw new Error("Failed to fetch file: ".concat(_response.status, " ").concat(_response.statusText));
214
+ case 6:
215
+ _context3.n = 7;
216
+ return _response.blob();
217
+ case 7:
218
+ blob = _context3.v;
219
+ _context3.n = 9;
220
+ break;
221
+ case 8:
222
+ // Web: fileUriOrFile is already a File/Blob object
223
+ blob = fileUriOrFile;
224
+ case 9:
225
+ _context3.n = 10;
226
+ return fetch(signedUrl, {
227
+ method: "PUT",
228
+ body: blob,
229
+ headers: {
230
+ "Content-Type": mimeType
231
+ }
232
+ });
233
+ case 10:
234
+ uploadResponse = _context3.v;
235
+ if (uploadResponse.ok) {
236
+ _context3.n = 12;
237
+ break;
238
+ }
239
+ _context3.n = 11;
240
+ return uploadResponse.text().catch(function () {
241
+ return "";
242
+ });
243
+ case 11:
244
+ errorText = _context3.v;
245
+ throw new Error("S3 upload failed: ".concat(uploadResponse.status, " ").concat(uploadResponse.statusText).concat(errorText ? " - ".concat(errorText) : ""));
246
+ case 12:
247
+ _context3.n = 14;
248
+ break;
249
+ case 13:
250
+ _context3.p = 13;
251
+ _t2 = _context3.v;
252
+ throw new Error(_t2.message || "Failed to upload file to S3");
253
+ case 14:
254
+ return _context3.a(2);
255
+ }
256
+ }, _callee3, null, [[0, 13]]);
257
+ }));
258
+ function uploadFileToS3(_x3, _x4, _x5) {
259
+ return _uploadFileToS.apply(this, arguments);
260
+ }
261
+ return uploadFileToS3;
262
+ }()
263
+ /**
264
+ * Get S3 signed upload URLs from API
265
+ */
266
+ )
267
+ }, {
268
+ key: "getS3SignedUrls",
269
+ value: (function () {
270
+ var _getS3SignedUrls = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(files, authKey) {
271
+ var auth_key, fileData, headers, _response$data$data, response, _t3;
272
+ return _regenerator().w(function (_context4) {
273
+ while (1) switch (_context4.p = _context4.n) {
177
274
  case 0:
178
275
  auth_key = authKey || this.authKey;
179
276
  if (auth_key) {
180
- _context3.n = 1;
277
+ _context4.n = 1;
181
278
  break;
182
279
  }
183
280
  throw new Error("Authentication key is required for upload");
184
281
  case 1:
185
282
  if (this.apiBaseUrl) {
186
- _context3.n = 2;
283
+ _context4.n = 2;
187
284
  break;
188
285
  }
189
286
  throw new Error('API base URL is required. The package automatically detects it from ENVIRONMENT, or you can set apiBaseUrl in config: new MediaApiService({ apiBaseUrl: "https://api.tradly.app" })');
@@ -203,8 +300,8 @@ var MediaApiService = /*#__PURE__*/function () {
203
300
  if (this.bearerToken) {
204
301
  headers["Authorization"] = "Bearer ".concat(this.bearerToken);
205
302
  }
206
- _context3.p = 3;
207
- _context3.n = 4;
303
+ _context4.p = 3;
304
+ _context4.n = 4;
208
305
  return (0, _axios.default)({
209
306
  method: "POST",
210
307
  url: "".concat(this.apiBaseUrl, "/v1/utils/S3signedUploadURL"),
@@ -214,28 +311,28 @@ var MediaApiService = /*#__PURE__*/function () {
214
311
  }
215
312
  });
216
313
  case 4:
217
- response = _context3.v;
314
+ response = _context4.v;
218
315
  if (!response.data.error) {
219
- _context3.n = 5;
316
+ _context4.n = 5;
220
317
  break;
221
318
  }
222
319
  throw new Error(response.data.error);
223
320
  case 5:
224
- return _context3.a(2, response.data.result || ((_response$data$data = response.data.data) === null || _response$data$data === void 0 ? void 0 : _response$data$data.result) || response.data);
321
+ return _context4.a(2, response.data.result || ((_response$data$data = response.data.data) === null || _response$data$data === void 0 ? void 0 : _response$data$data.result) || response.data);
225
322
  case 6:
226
- _context3.p = 6;
227
- _t2 = _context3.v;
228
- console.error("Error getting S3 signed URLs:", _t2);
323
+ _context4.p = 6;
324
+ _t3 = _context4.v;
325
+ console.error("Error getting S3 signed URLs:", _t3);
229
326
  if (this.onError) {
230
- this.onError(_t2);
327
+ this.onError(_t3);
231
328
  }
232
- throw _t2;
329
+ throw _t3;
233
330
  case 7:
234
- return _context3.a(2);
331
+ return _context4.a(2);
235
332
  }
236
- }, _callee3, this, [[3, 6]]);
333
+ }, _callee4, this, [[3, 6]]);
237
334
  }));
238
- function getS3SignedUrls(_x3, _x4) {
335
+ function getS3SignedUrls(_x6, _x7) {
239
336
  return _getS3SignedUrls.apply(this, arguments);
240
337
  }
241
338
  return getS3SignedUrls;
@@ -248,14 +345,14 @@ var MediaApiService = /*#__PURE__*/function () {
248
345
  }, {
249
346
  key: "uploadMedia",
250
347
  value: (function () {
251
- var _uploadMedia = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(files, authKey) {
252
- var auth_key, all_files_uri, upload_files, upload_full_files, i, element, file_data, responseFiles, index, path, fileURI, originalFile, fileBody, uri, fileResponse, arrayBuffer, res, errorText, mediaData, _t3, _t4, _t5;
253
- return _regenerator().w(function (_context4) {
254
- while (1) switch (_context4.p = _context4.n) {
348
+ var _uploadMedia = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(files, authKey) {
349
+ var auth_key, all_files_uri, upload_files, upload_full_files, i, element, cleanFileName, file_data, responseFiles, index, path, fileURI, originalFile, mimeType, fileInput, mediaData, _t4, _t5;
350
+ return _regenerator().w(function (_context5) {
351
+ while (1) switch (_context5.p = _context5.n) {
255
352
  case 0:
256
353
  auth_key = authKey || this.authKey;
257
354
  if (auth_key) {
258
- _context4.n = 1;
355
+ _context5.n = 1;
259
356
  break;
260
357
  }
261
358
  throw new Error("Authentication key is required for upload");
@@ -266,7 +363,7 @@ var MediaApiService = /*#__PURE__*/function () {
266
363
  i = 0;
267
364
  case 2:
268
365
  if (!(i < files.length)) {
269
- _context4.n = 27;
366
+ _context5.n = 14;
270
367
  break;
271
368
  }
272
369
  element = files[i]; // Check if file already has a path (from previous upload)
@@ -274,9 +371,11 @@ var MediaApiService = /*#__PURE__*/function () {
274
371
  all_files_uri.push(element.path);
275
372
  } else {
276
373
  // Prepare file data for upload
374
+ // Clean file name: remove spaces (following user's pattern: fileName.replace(/ /g, ""))
375
+ cleanFileName = element.name.replace(/\s/g, "");
277
376
  file_data = {
278
- name: element.name.replace(/\s/g, "-"),
279
- type: element.type
377
+ name: cleanFileName,
378
+ type: element.type || "image/jpeg" // Default MIME type
280
379
  };
281
380
  upload_files.push(file_data);
282
381
  upload_full_files.push(element);
@@ -284,138 +383,47 @@ var MediaApiService = /*#__PURE__*/function () {
284
383
 
285
384
  // Upload files when we've processed all files
286
385
  if (!(files.length === i + 1 && upload_files.length > 0)) {
287
- _context4.n = 26;
386
+ _context5.n = 13;
288
387
  break;
289
388
  }
290
- _context4.p = 3;
291
- _context4.n = 4;
389
+ _context5.p = 3;
390
+ _context5.n = 4;
292
391
  return this.getS3SignedUrls(upload_full_files, auth_key);
293
392
  case 4:
294
- responseFiles = _context4.v;
393
+ responseFiles = _context5.v;
295
394
  index = 0;
296
395
  case 5:
297
396
  if (!(index < responseFiles.length)) {
298
- _context4.n = 23;
397
+ _context5.n = 10;
299
398
  break;
300
399
  }
301
400
  path = responseFiles[index].signedUrl;
302
401
  fileURI = responseFiles[index].fileUri;
303
402
  originalFile = upload_full_files[index];
304
- _context4.p = 6;
305
- fileBody = originalFile; // Handle React Native file URIs
306
- // If file has a uri property, it's from React Native - fetch it first
307
- if (!(originalFile.uri && typeof originalFile.uri === "string")) {
308
- _context4.n = 16;
309
- break;
310
- }
311
- // For React Native, handle file://, content://, or http:// URIs
312
- uri = originalFile.uri;
313
- _context4.p = 7;
314
- // For React Native file:// URIs, fetch should work
315
- // Convert file:// URI to blob for S3 upload
316
- console.log("Fetching file from URI:", uri);
317
- _context4.n = 8;
318
- return fetch(uri);
319
- case 8:
320
- fileResponse = _context4.v;
321
- if (fileResponse.ok) {
322
- _context4.n = 9;
323
- break;
324
- }
325
- throw new Error("Failed to fetch file: ".concat(fileResponse.status, " ").concat(fileResponse.statusText));
326
- case 9:
327
- if (!fileResponse.blob) {
328
- _context4.n = 11;
329
- break;
330
- }
331
- _context4.n = 10;
332
- return fileResponse.blob();
333
- case 10:
334
- fileBody = _context4.v;
335
- console.log("File converted to blob, size:", fileBody.size);
336
- _context4.n = 14;
337
- break;
338
- case 11:
339
- if (!fileResponse.arrayBuffer) {
340
- _context4.n = 13;
341
- break;
342
- }
343
- _context4.n = 12;
344
- return fileResponse.arrayBuffer();
345
- case 12:
346
- arrayBuffer = _context4.v;
347
- // Check if Blob constructor is available
348
- if (typeof Blob !== "undefined") {
349
- fileBody = new Blob([arrayBuffer], {
350
- type: upload_files[index].type
351
- });
352
- } else {
353
- // If Blob is not available, use arrayBuffer directly
354
- fileBody = arrayBuffer;
355
- }
356
- _context4.n = 14;
357
- break;
358
- case 13:
359
- // Last resort: use response directly (might not work for all cases)
360
- fileBody = fileResponse;
361
- case 14:
362
- _context4.n = 16;
363
- break;
364
- case 15:
365
- _context4.p = 15;
366
- _t3 = _context4.v;
367
- console.error("Error fetching file from URI:", _t3);
368
- console.error("URI was:", uri);
369
- console.error("File details:", {
370
- name: originalFile.name,
371
- type: originalFile.type
372
- });
373
- throw new Error("Failed to read file from URI: ".concat(uri, ". Error: ").concat(_t3.message, ". Make sure the file URI is accessible."));
374
- case 16:
375
- // Upload to S3 using PUT request
376
- console.log("Uploading to S3:", path);
377
- console.log("File type:", upload_files[index].type);
378
- _context4.n = 17;
379
- return fetch(path, {
380
- method: "PUT",
381
- headers: {
382
- "Content-Type": upload_files[index].type
383
- },
384
- body: fileBody
385
- });
386
- case 17:
387
- res = _context4.v;
388
- if (!res.ok) {
389
- _context4.n = 18;
390
- break;
391
- }
392
- console.log("File ".concat(index + 1, " uploaded successfully"));
403
+ mimeType = upload_files[index].type;
404
+ _context5.p = 6;
405
+ // Upload file to S3 using the signed URL
406
+ // uploadFileToS3 handles both web File objects and React Native URIs
407
+ fileInput = originalFile.uri || originalFile;
408
+ _context5.n = 7;
409
+ return this.uploadFileToS3(path, fileInput, mimeType);
410
+ case 7:
393
411
  all_files_uri.push(fileURI);
394
- _context4.n = 20;
412
+ _context5.n = 9;
395
413
  break;
396
- case 18:
397
- _context4.n = 19;
398
- return res.text().catch(function () {
399
- return "";
400
- });
401
- case 19:
402
- errorText = _context4.v;
403
- console.error("Failed to upload file ".concat(index + 1, ":"), res.status, res.statusText, errorText);
404
- throw new Error("S3 upload failed: ".concat(res.status, " ").concat(res.statusText));
405
- case 20:
406
- _context4.n = 22;
407
- break;
408
- case 21:
409
- _context4.p = 21;
410
- _t4 = _context4.v;
414
+ case 8:
415
+ _context5.p = 8;
416
+ _t4 = _context5.v;
411
417
  console.error("Error uploading file ".concat(index + 1, ":"), _t4);
412
- case 22:
418
+ // Re-throw to stop the upload process
419
+ throw _t4;
420
+ case 9:
413
421
  index++;
414
- _context4.n = 5;
422
+ _context5.n = 5;
415
423
  break;
416
- case 23:
424
+ case 10:
417
425
  if (!(all_files_uri.length > 0)) {
418
- _context4.n = 24;
426
+ _context5.n = 11;
419
427
  break;
420
428
  }
421
429
  mediaData = all_files_uri.map(function (url, index) {
@@ -424,11 +432,11 @@ var MediaApiService = /*#__PURE__*/function () {
424
432
  type: 1,
425
433
  parent: 0,
426
434
  url: url,
427
- name: originalFile.name.replace(/\s/g, "-"),
435
+ name: originalFile.name.replace(/\s/g, ""),
428
436
  mime_type: originalFile.type
429
437
  };
430
438
  }); // Save to media API - POST /v1/media with { media: [...] }
431
- _context4.n = 24;
439
+ _context5.n = 11;
432
440
  return this.apiCall({
433
441
  method: "POST",
434
442
  path: "/v1/media",
@@ -436,27 +444,27 @@ var MediaApiService = /*#__PURE__*/function () {
436
444
  media: mediaData
437
445
  }
438
446
  });
439
- case 24:
440
- _context4.n = 26;
447
+ case 11:
448
+ _context5.n = 13;
441
449
  break;
442
- case 25:
443
- _context4.p = 25;
444
- _t5 = _context4.v;
450
+ case 12:
451
+ _context5.p = 12;
452
+ _t5 = _context5.v;
445
453
  console.error("Upload error:", _t5);
446
454
  if (this.onError) {
447
455
  this.onError(_t5);
448
456
  }
449
457
  throw _t5;
450
- case 26:
458
+ case 13:
451
459
  i++;
452
- _context4.n = 2;
460
+ _context5.n = 2;
453
461
  break;
454
- case 27:
455
- return _context4.a(2, all_files_uri);
462
+ case 14:
463
+ return _context5.a(2, all_files_uri);
456
464
  }
457
- }, _callee4, this, [[7, 15], [6, 21], [3, 25]]);
465
+ }, _callee5, this, [[6, 8], [3, 12]]);
458
466
  }));
459
- function uploadMedia(_x5, _x6) {
467
+ function uploadMedia(_x8, _x9) {
460
468
  return _uploadMedia.apply(this, arguments);
461
469
  }
462
470
  return uploadMedia;