@crystaldesign/media-upload 24.13.0-beta.13 → 24.13.0-beta.14

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.
@@ -203,6 +203,24 @@ var DivaFile = /*#__PURE__*/function () {
203
203
  this._state = 'CHANGED';
204
204
  }
205
205
  }
206
+ }, {
207
+ key: "setMediaItemAndRef",
208
+ value: function setMediaItemAndRef(_ref) {
209
+ var item = _ref.item,
210
+ ref = _ref.ref;
211
+ this._state = 'SAVED';
212
+ this.itemRef = ref;
213
+ this._url = ref.url;
214
+ this._organizationId = ref.organizationId;
215
+ this._mediaType = ref.mediaType;
216
+ this._functionType = ref.functionType;
217
+ this._ref = ref._id;
218
+ this.item = item;
219
+ this._description = item.description;
220
+ this._borderPercentage = item.borderPercentage;
221
+ this._fileName = item.fileName;
222
+ this._classes = item.classes;
223
+ }
206
224
  }, {
207
225
  key: "fileName",
208
226
  get: function get() {
@@ -265,49 +283,56 @@ var DivaFile = /*#__PURE__*/function () {
265
283
  }, {
266
284
  key: "save",
267
285
  value: function () {
268
- var _save = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3(params) {
269
- var _this$fileObject, _this$itemRef, _this$itemRef$url, _this$itemRef2, _file2, _url, response, blob, formData, mediaitem, _url2, mediaitemref, _mediaitemref, data, media, _media$spinImages, _media, ref, item, _this$item;
270
- return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
271
- while (1) switch (_context3.prev = _context3.next) {
286
+ var _save = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5(params) {
287
+ var mediaItemRef, _refUrl, _this$fileObject, _this$itemRef, _this$itemRef$url, _this$itemRef2, _file2, _url, response, blob, formData, mediaitem, _url2, mediaitemref, refs, data, _mediaitemref, media, _media, ref, item, _this$item, _params$refIds, _refs, _this$itemRef3, _this$itemRef4, result;
288
+ return _regeneratorRuntime__default["default"].wrap(function _callee5$(_context5) {
289
+ while (1) switch (_context5.prev = _context5.next) {
272
290
  case 0:
273
291
  if (!(!this.itemRef && this._state != 'NEW')) {
274
- _context3.next = 15;
292
+ _context5.next = 22;
275
293
  break;
276
294
  }
277
295
  if (this._ref) {
278
- _context3.next = 3;
296
+ _context5.next = 3;
279
297
  break;
280
298
  }
281
299
  throw new Error('If state is not NEW, there must be a ref present');
282
300
  case 3:
283
- _context3.next = 5;
301
+ _context5.next = 5;
284
302
  return this.getMediaItemRef(this._ref, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
285
303
  case 5:
304
+ mediaItemRef = _context5.sent;
305
+ this.itemRef = mediaItemRef;
306
+ if (!this._functionType) this._functionType = this.itemRef.functionType;
307
+ if (!this._description) this._description = this.itemRef.description;
308
+ if (!this._fileName) this._fileName = this.itemRef.fileName;
309
+ this._mediaType = this.itemRef.mediaType;
310
+ if (!(this._organizationId && this._organizationId != this.itemRef.organizationId)) {
311
+ _context5.next = 13;
312
+ break;
313
+ }
314
+ throw new Error('The existing mediaItemRef belongs to a different organization');
315
+ case 13:
316
+ this._organizationId = this.itemRef.organizationId;
317
+ this._url = this.itemRef.url;
318
+ this._ref = this.itemRef._id;
286
319
  if (this.item) {
287
- _context3.next = 15;
320
+ _context5.next = 22;
288
321
  break;
289
322
  }
290
323
  if (this.itemRef) {
291
- _context3.next = 8;
324
+ _context5.next = 19;
292
325
  break;
293
326
  }
294
327
  throw new Error('If state is not NEW, there must be a url present');
295
- case 8:
296
- if (!this.itemRef.refUrl) {
297
- _context3.next = 13;
298
- break;
299
- }
300
- _context3.next = 11;
301
- return this.getMediaItem(this.itemRef.refUrl, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
302
- case 11:
303
- _context3.next = 15;
304
- break;
305
- case 13:
306
- _context3.next = 15;
307
- return this.getMediaItem(this.itemRef.url, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
308
- case 15:
328
+ case 19:
329
+ _context5.next = 21;
330
+ return this.getMediaItem((_refUrl = this.itemRef.refUrl) !== null && _refUrl !== void 0 ? _refUrl : this.itemRef.url, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
331
+ case 21:
332
+ this.item = _context5.sent;
333
+ case 22:
309
334
  if (!(this._state == 'NEW' && !this.isExternalVid(this._url))) {
310
- _context3.next = 63;
335
+ _context5.next = 70;
311
336
  break;
312
337
  }
313
338
  // create the mediaItem
@@ -315,39 +340,39 @@ var DivaFile = /*#__PURE__*/function () {
315
340
  // for this type we only support url
316
341
  _file2 = (_this$fileObject = this.fileObject) === null || _this$fileObject === void 0 ? void 0 : _this$fileObject.file;
317
342
  if (!(!_file2 && !this._spinFiles)) {
318
- _context3.next = 31;
343
+ _context5.next = 38;
319
344
  break;
320
345
  }
321
346
  if (this._url) {
322
- _context3.next = 20;
347
+ _context5.next = 27;
323
348
  break;
324
349
  }
325
350
  throw new Error('Either file or url must be specified');
326
- case 20:
351
+ case 27:
327
352
  // if only a url is providied get the actual file
328
353
  _url = new URL(this._url);
329
354
  if (_url.protocol.startsWith('http')) {
330
355
  // add a dummy query to avoid the browser cache (cached response is missing cors headers)
331
356
  _url.searchParams.set('dt', Date.now().toString());
332
357
  }
333
- _context3.next = 24;
358
+ _context5.next = 31;
334
359
  return fetch(_url.toString());
335
- case 24:
336
- response = _context3.sent;
360
+ case 31:
361
+ response = _context5.sent;
337
362
  if (!(response.status === 400)) {
338
- _context3.next = 27;
363
+ _context5.next = 34;
339
364
  break;
340
365
  }
341
366
  throw new Error('Error on saving media.');
342
- case 27:
343
- _context3.next = 29;
367
+ case 34:
368
+ _context5.next = 36;
344
369
  return response.blob();
345
- case 29:
346
- blob = _context3.sent;
370
+ case 36:
371
+ blob = _context5.sent;
347
372
  _file2 = new File([blob], this._fileName, {
348
373
  type: blob.type
349
374
  });
350
- case 31:
375
+ case 38:
351
376
  formData = new FormData();
352
377
  mediaitem = {
353
378
  fileName: this._fileName,
@@ -365,45 +390,26 @@ var DivaFile = /*#__PURE__*/function () {
365
390
  if (this._borderPercentage) {
366
391
  mediaitem.borderPercentage = this._borderPercentage;
367
392
  }
368
- if (!this._spinFiles) {
369
- _context3.next = 44;
370
- break;
371
- }
372
- formData.append('mediaItems', JSON.stringify(mediaitem));
373
- this._spinFiles.forEach(function (sf) {
374
- return formData.append('files', sf.file);
375
- });
376
- _url2 = "".concat(this.config.url, "/v1/mediaitems/bulk?spin=true");
377
- if (this.createRef) {
378
- mediaitemref = {
379
- functionType: this._functionType
380
- };
381
- formData.append('mediaItemRefs', JSON.stringify(mediaitemref));
382
- _url2 = _url2 + '&ref=true';
383
- }
384
- _context3.next = 52;
385
- break;
386
- case 44:
387
393
  if (!_file2) {
388
- _context3.next = 51;
394
+ _context5.next = 51;
389
395
  break;
390
396
  }
391
397
  formData.append('mediaItem', JSON.stringify(mediaitem));
392
398
  formData.append('file', _file2);
393
399
  _url2 = "".concat(this.config.url, "/v1/mediaitems");
394
- if (this.createRef) {
395
- _mediaitemref = {
400
+ if (this.createRef && !(params !== null && params !== void 0 && params.refCount)) {
401
+ mediaitemref = {
396
402
  functionType: this._functionType
397
403
  };
398
- formData.append('mediaItemRef', JSON.stringify(_mediaitemref));
404
+ formData.append('mediaItemRef', JSON.stringify(mediaitemref));
399
405
  _url2 = _url2 + '?ref=true';
400
406
  }
401
- _context3.next = 52;
407
+ _context5.next = 52;
402
408
  break;
403
409
  case 51:
404
410
  throw new Error('Either file or url must be specified');
405
411
  case 52:
406
- _context3.next = 54;
412
+ _context5.next = 54;
407
413
  return fetch(_url2, {
408
414
  method: 'POST',
409
415
  body: formData,
@@ -411,7 +417,7 @@ var DivaFile = /*#__PURE__*/function () {
411
417
  Authorization: "Bearer ".concat(this.config.jwt)
412
418
  } : {}
413
419
  }).then(/*#__PURE__*/function () {
414
- var _ref = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(r) {
420
+ var _ref2 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(r) {
415
421
  var error, err;
416
422
  return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
417
423
  while (1) switch (_context.prev = _context.next) {
@@ -443,30 +449,79 @@ var DivaFile = /*#__PURE__*/function () {
443
449
  }, _callee, null, [[1, 8]]);
444
450
  }));
445
451
  return function (_x2) {
446
- return _ref.apply(this, arguments);
452
+ return _ref2.apply(this, arguments);
447
453
  };
448
454
  }());
449
455
  case 54:
450
- data = _context3.sent;
451
- if (this._spinFiles) {
452
- media = data.success[0];
453
- this._thumbnail = (_media$spinImages = media.spinImages) === null || _media$spinImages === void 0 ? void 0 : _media$spinImages[0];
454
- } else {
455
- media = data;
456
+ data = _context5.sent;
457
+ if (!(this.createRef && params !== null && params !== void 0 && params.refCount)) {
458
+ _context5.next = 61;
459
+ break;
456
460
  }
461
+ _mediaitemref = {
462
+ functionType: this._functionType
463
+ };
464
+ _url2 = "".concat(this.config.url, "/v1/mediaitems/").concat(data._id, "/refs/").concat(params.refCount);
465
+ _context5.next = 60;
466
+ return fetch(_url2, {
467
+ method: 'POST',
468
+ body: JSON.stringify(_mediaitemref),
469
+ headers: this.config.jwt ? {
470
+ 'Content-Type': 'application/json',
471
+ Authorization: "Bearer ".concat(this.config.jwt)
472
+ } : {}
473
+ }).then(/*#__PURE__*/function () {
474
+ var _ref3 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2(r) {
475
+ var error, err;
476
+ return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
477
+ while (1) switch (_context2.prev = _context2.next) {
478
+ case 0:
479
+ if (r.ok) {
480
+ _context2.next = 12;
481
+ break;
482
+ }
483
+ _context2.prev = 1;
484
+ _context2.next = 4;
485
+ return r.json();
486
+ case 4:
487
+ err = _context2.sent;
488
+ error = err.error;
489
+ _context2.next = 11;
490
+ break;
491
+ case 8:
492
+ _context2.prev = 8;
493
+ _context2.t0 = _context2["catch"](1);
494
+ throw new Error('Error while saving');
495
+ case 11:
496
+ throw new Error(error);
497
+ case 12:
498
+ return _context2.abrupt("return", r.json());
499
+ case 13:
500
+ case "end":
501
+ return _context2.stop();
502
+ }
503
+ }, _callee2, null, [[1, 8]]);
504
+ }));
505
+ return function (_x3) {
506
+ return _ref3.apply(this, arguments);
507
+ };
508
+ }());
509
+ case 60:
510
+ refs = _context5.sent;
511
+ case 61:
512
+ media = data;
457
513
  _media = media, ref = _media.ref, item = _objectWithoutProperties__default["default"](_media, _excluded);
458
514
  this.item = item;
459
- this.itemRef = ref;
515
+ this.itemRef = ref !== null && ref !== void 0 ? ref : refs[0];
460
516
  this._ref = (_this$itemRef = this.itemRef) === null || _this$itemRef === void 0 ? void 0 : _this$itemRef._id;
461
517
  this._url = (_this$itemRef$url = (_this$itemRef2 = this.itemRef) === null || _this$itemRef2 === void 0 ? void 0 : _this$itemRef2.url) !== null && _this$itemRef$url !== void 0 ? _this$itemRef$url : this.item.url;
462
- _context3.next = 67;
463
- break;
464
- case 63:
518
+ return _context5.abrupt("return", refs);
519
+ case 70:
465
520
  if (!(this._state == 'CHANGED')) {
466
- _context3.next = 67;
521
+ _context5.next = 83;
467
522
  break;
468
523
  }
469
- _context3.next = 66;
524
+ _context5.next = 73;
470
525
  return fetch("".concat(this.config.url, "/v1/mediaitems/").concat((_this$item = this.item) === null || _this$item === void 0 ? void 0 : _this$item._id), {
471
526
  method: 'PATCH',
472
527
  body: JSON.stringify({
@@ -494,48 +549,115 @@ var DivaFile = /*#__PURE__*/function () {
494
549
  Authorization: "Bearer ".concat(this.config.jwt)
495
550
  } : {})
496
551
  }).then(/*#__PURE__*/function () {
497
- var _ref2 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2(r) {
552
+ var _ref4 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3(r) {
498
553
  var error, err;
499
- return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
500
- while (1) switch (_context2.prev = _context2.next) {
554
+ return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
555
+ while (1) switch (_context3.prev = _context3.next) {
501
556
  case 0:
502
557
  if (r.ok) {
503
- _context2.next = 12;
558
+ _context3.next = 12;
504
559
  break;
505
560
  }
506
- _context2.prev = 1;
507
- _context2.next = 4;
561
+ _context3.prev = 1;
562
+ _context3.next = 4;
508
563
  return r.json();
509
564
  case 4:
510
- err = _context2.sent;
565
+ err = _context3.sent;
511
566
  error = err.error;
512
- _context2.next = 11;
567
+ _context3.next = 11;
513
568
  break;
514
569
  case 8:
515
- _context2.prev = 8;
516
- _context2.t0 = _context2["catch"](1);
570
+ _context3.prev = 8;
571
+ _context3.t0 = _context3["catch"](1);
517
572
  throw new Error('Error while saving');
518
573
  case 11:
519
574
  throw new Error(error);
520
575
  case 12:
521
- return _context2.abrupt("return", r.json());
576
+ return _context3.abrupt("return", r.json());
522
577
  case 13:
523
578
  case "end":
524
- return _context2.stop();
579
+ return _context3.stop();
525
580
  }
526
- }, _callee2, null, [[1, 8]]);
581
+ }, _callee3, null, [[1, 8]]);
527
582
  }));
528
- return function (_x3) {
529
- return _ref2.apply(this, arguments);
583
+ return function (_x4) {
584
+ return _ref4.apply(this, arguments);
585
+ };
586
+ }());
587
+ case 73:
588
+ this.item = _context5.sent;
589
+ _refs = (_params$refIds = params === null || params === void 0 ? void 0 : params.refIds) !== null && _params$refIds !== void 0 ? _params$refIds : [this.ref];
590
+ if (!(_refs !== null && _refs !== void 0 && _refs.length)) {
591
+ _context5.next = 83;
592
+ break;
593
+ }
594
+ _context5.next = 78;
595
+ return fetch("".concat(this.config.url, "/v1/mediaitems/refs/bulk"), {
596
+ method: 'PATCH',
597
+ body: JSON.stringify({
598
+ ids: _refs,
599
+ patch: [{
600
+ op: 'replace',
601
+ path: '/functionType',
602
+ value: this._functionType
603
+ }]
604
+ }),
605
+ headers: _objectSpread$1({
606
+ 'Content-Type': 'application/json'
607
+ }, this.config.jwt ? {
608
+ Authorization: "Bearer ".concat(this.config.jwt)
609
+ } : {})
610
+ }).then(/*#__PURE__*/function () {
611
+ var _ref5 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4(r) {
612
+ var error, err;
613
+ return _regeneratorRuntime__default["default"].wrap(function _callee4$(_context4) {
614
+ while (1) switch (_context4.prev = _context4.next) {
615
+ case 0:
616
+ if (r.ok) {
617
+ _context4.next = 12;
618
+ break;
619
+ }
620
+ _context4.prev = 1;
621
+ _context4.next = 4;
622
+ return r.json();
623
+ case 4:
624
+ err = _context4.sent;
625
+ error = err.error;
626
+ _context4.next = 11;
627
+ break;
628
+ case 8:
629
+ _context4.prev = 8;
630
+ _context4.t0 = _context4["catch"](1);
631
+ throw new Error('Error while saving');
632
+ case 11:
633
+ throw new Error(error);
634
+ case 12:
635
+ return _context4.abrupt("return", r.json());
636
+ case 13:
637
+ case "end":
638
+ return _context4.stop();
639
+ }
640
+ }, _callee4, null, [[1, 8]]);
641
+ }));
642
+ return function (_x5) {
643
+ return _ref5.apply(this, arguments);
530
644
  };
531
645
  }());
532
- case 66:
533
- this.item = _context3.sent;
534
- case 67:
646
+ case 78:
647
+ result = _context5.sent;
648
+ this.itemRef = result[0];
649
+ this._ref = (_this$itemRef3 = this.itemRef) === null || _this$itemRef3 === void 0 ? void 0 : _this$itemRef3._id;
650
+ this._url = (_this$itemRef4 = this.itemRef) === null || _this$itemRef4 === void 0 ? void 0 : _this$itemRef4.url;
651
+ return _context5.abrupt("return", result.map(function (r) {
652
+ return r._id;
653
+ }));
654
+ case 83:
655
+ return _context5.abrupt("return", []);
656
+ case 84:
535
657
  case "end":
536
- return _context3.stop();
658
+ return _context5.stop();
537
659
  }
538
- }, _callee3, this);
660
+ }, _callee5, this);
539
661
  }));
540
662
  function save(_x) {
541
663
  return _save.apply(this, arguments);
@@ -545,60 +667,104 @@ var DivaFile = /*#__PURE__*/function () {
545
667
  }, {
546
668
  key: "load",
547
669
  value: function () {
548
- var _load = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4(params) {
670
+ var _load = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee6(params) {
671
+ var _mediaItemRef$refUrl;
549
672
  var mediaItemRef;
550
- return _regeneratorRuntime__default["default"].wrap(function _callee4$(_context4) {
551
- while (1) switch (_context4.prev = _context4.next) {
673
+ return _regeneratorRuntime__default["default"].wrap(function _callee6$(_context6) {
674
+ while (1) switch (_context6.prev = _context6.next) {
552
675
  case 0:
553
676
  if (!(!this.ref || !(params !== null && params !== void 0 && params.organizationId) && !this._organizationId)) {
554
- _context4.next = 2;
677
+ _context6.next = 2;
555
678
  break;
556
679
  }
557
680
  throw new Error('Ref or OrganizationId must be set to load data form server');
558
681
  case 2:
559
682
  if (!this.itemRef) {
560
- _context4.next = 4;
683
+ _context6.next = 4;
561
684
  break;
562
685
  }
563
- return _context4.abrupt("return");
686
+ return _context6.abrupt("return");
564
687
  case 4:
565
- _context4.next = 6;
688
+ _context6.next = 6;
566
689
  return this.getMediaItemRef(this.ref, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
567
690
  case 6:
568
- mediaItemRef = this.itemRef;
691
+ mediaItemRef = _context6.sent;
692
+ this.itemRef = mediaItemRef;
693
+ if (!(this._organizationId && this._organizationId != this.itemRef.organizationId)) {
694
+ _context6.next = 10;
695
+ break;
696
+ }
697
+ throw new Error('The existing mediaItemRef belongs to a different organization');
698
+ case 10:
699
+ this._ref = mediaItemRef._id;
569
700
  this._url = mediaItemRef.url;
570
701
  this._organizationId = mediaItemRef.organizationId;
571
702
  this._mediaType = mediaItemRef.mediaType;
572
703
  this._functionType = mediaItemRef.functionType;
573
704
  this._state = 'SAVED';
574
- if (!mediaItemRef.refUrl) {
575
- _context4.next = 17;
576
- break;
577
- }
578
- _context4.next = 15;
579
- return this.getMediaItem(mediaItemRef.refUrl, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
580
- case 15:
581
- _context4.next = 19;
582
- break;
583
- case 17:
584
- _context4.next = 19;
585
- return this.getMediaItem(mediaItemRef.url, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
586
- case 19:
705
+ _context6.next = 18;
706
+ return this.getMediaItem((_mediaItemRef$refUrl = mediaItemRef.refUrl) !== null && _mediaItemRef$refUrl !== void 0 ? _mediaItemRef$refUrl : mediaItemRef.url, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
707
+ case 18:
708
+ this.item = _context6.sent;
587
709
  this._description = this.item.description;
588
710
  this._borderPercentage = this.item.borderPercentage;
589
711
  this._fileName = this.item.fileName;
590
712
  this._classes = this.item.classes;
591
713
  case 23:
592
714
  case "end":
593
- return _context4.stop();
715
+ return _context6.stop();
594
716
  }
595
- }, _callee4, this);
717
+ }, _callee6, this);
596
718
  }));
597
- function load(_x4) {
719
+ function load(_x6) {
598
720
  return _load.apply(this, arguments);
599
721
  }
600
722
  return load;
601
723
  }()
724
+ }, {
725
+ key: "loadMany",
726
+ value: function () {
727
+ var _loadMany = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee7(params) {
728
+ var _params$refIds2, _params$organizationI, _refs$0$refUrl;
729
+ var refs, mediaItem;
730
+ return _regeneratorRuntime__default["default"].wrap(function _callee7$(_context7) {
731
+ while (1) switch (_context7.prev = _context7.next) {
732
+ case 0:
733
+ if (params !== null && params !== void 0 && (_params$refIds2 = params.refIds) !== null && _params$refIds2 !== void 0 && _params$refIds2.length) {
734
+ _context7.next = 2;
735
+ break;
736
+ }
737
+ throw new Error('refs are undefined');
738
+ case 2:
739
+ if (!(!(params !== null && params !== void 0 && params.organizationId) && !this._organizationId)) {
740
+ _context7.next = 4;
741
+ break;
742
+ }
743
+ throw new Error('Ref or OrganizationId must be set to load data form server');
744
+ case 4:
745
+ _context7.next = 6;
746
+ return this.getMediaItemRefs(params.refIds, (_params$organizationI = params === null || params === void 0 ? void 0 : params.organizationId) !== null && _params$organizationI !== void 0 ? _params$organizationI : this._organizationId);
747
+ case 6:
748
+ refs = _context7.sent;
749
+ _context7.next = 9;
750
+ return this.getMediaItem((_refs$0$refUrl = refs[0].refUrl) !== null && _refs$0$refUrl !== void 0 ? _refs$0$refUrl : refs[0].url, (params === null || params === void 0 ? void 0 : params.organizationId) || this._organizationId);
751
+ case 9:
752
+ mediaItem = _context7.sent;
753
+ return _context7.abrupt("return", {
754
+ mediaItem: mediaItem,
755
+ refs: refs
756
+ });
757
+ case 11:
758
+ case "end":
759
+ return _context7.stop();
760
+ }
761
+ }, _callee7, this);
762
+ }));
763
+ function loadMany(_x7) {
764
+ return _loadMany.apply(this, arguments);
765
+ }
766
+ return loadMany;
767
+ }()
602
768
  }, {
603
769
  key: "clone",
604
770
  value: function clone() {
@@ -607,10 +773,10 @@ var DivaFile = /*#__PURE__*/function () {
607
773
  }, {
608
774
  key: "getMediaItem",
609
775
  value: function () {
610
- var _getMediaItem = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5(imageUrl, organizationId) {
776
+ var _getMediaItem = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee8(imageUrl, organizationId) {
611
777
  var url, filter, mediaItem;
612
- return _regeneratorRuntime__default["default"].wrap(function _callee5$(_context5) {
613
- while (1) switch (_context5.prev = _context5.next) {
778
+ return _regeneratorRuntime__default["default"].wrap(function _callee8$(_context8) {
779
+ while (1) switch (_context8.prev = _context8.next) {
614
780
  case 0:
615
781
  url = new URL("".concat(this.config.url, "/v1/mediaitems"));
616
782
  filter = "{url,eq,".concat(imageUrl, "}");
@@ -619,7 +785,7 @@ var DivaFile = /*#__PURE__*/function () {
619
785
  }
620
786
  url.searchParams.append('limit', '1');
621
787
  url.searchParams.append('filter', encodeURIComponent("[".concat(filter, "]")));
622
- _context5.next = 7;
788
+ _context8.next = 7;
623
789
  return fetch(url.toString(), {
624
790
  method: 'GET',
625
791
  headers: this.config.jwt ? {
@@ -629,18 +795,18 @@ var DivaFile = /*#__PURE__*/function () {
629
795
  return r.json();
630
796
  });
631
797
  case 7:
632
- _context5.next = 9;
633
- return _context5.sent;
798
+ _context8.next = 9;
799
+ return _context8.sent;
634
800
  case 9:
635
- mediaItem = _context5.sent;
636
- this.item = mediaItem === null || mediaItem === void 0 ? void 0 : mediaItem.data[0];
801
+ mediaItem = _context8.sent;
802
+ return _context8.abrupt("return", mediaItem === null || mediaItem === void 0 ? void 0 : mediaItem.data[0]);
637
803
  case 11:
638
804
  case "end":
639
- return _context5.stop();
805
+ return _context8.stop();
640
806
  }
641
- }, _callee5, this);
807
+ }, _callee8, this);
642
808
  }));
643
- function getMediaItem(_x5, _x6) {
809
+ function getMediaItem(_x8, _x9) {
644
810
  return _getMediaItem.apply(this, arguments);
645
811
  }
646
812
  return getMediaItem;
@@ -648,10 +814,10 @@ var DivaFile = /*#__PURE__*/function () {
648
814
  }, {
649
815
  key: "getMediaItemRef",
650
816
  value: function () {
651
- var _getMediaItemRef = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee6(ref, organizationId) {
817
+ var _getMediaItemRef = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee9(ref, organizationId) {
652
818
  var url, filter, mediaItemRef;
653
- return _regeneratorRuntime__default["default"].wrap(function _callee6$(_context6) {
654
- while (1) switch (_context6.prev = _context6.next) {
819
+ return _regeneratorRuntime__default["default"].wrap(function _callee9$(_context9) {
820
+ while (1) switch (_context9.prev = _context9.next) {
655
821
  case 0:
656
822
  url = new URL("".concat(this.config.url, "/v1/mediaitems/refs"));
657
823
  filter = "{_id,eq,".concat(ref, "}");
@@ -660,7 +826,7 @@ var DivaFile = /*#__PURE__*/function () {
660
826
  }
661
827
  url.searchParams.append('limit', '1');
662
828
  url.searchParams.append('filter', encodeURIComponent("[".concat(filter, "]")));
663
- _context6.next = 7;
829
+ _context9.next = 7;
664
830
  return fetch(url.toString(), {
665
831
  method: 'GET',
666
832
  headers: this.config.jwt ? {
@@ -670,35 +836,63 @@ var DivaFile = /*#__PURE__*/function () {
670
836
  return r.json();
671
837
  });
672
838
  case 7:
673
- _context6.next = 9;
674
- return _context6.sent;
839
+ _context9.next = 9;
840
+ return _context9.sent;
675
841
  case 9:
676
- mediaItemRef = _context6.sent;
677
- this.itemRef = mediaItemRef === null || mediaItemRef === void 0 ? void 0 : mediaItemRef.data[0];
678
- if (!this._functionType) this._functionType = this.itemRef.functionType;
679
- if (!this._description) this._description = this.itemRef.description;
680
- if (!this._fileName) this._fileName = this.itemRef.fileName;
681
- this._mediaType = this.itemRef.mediaType;
682
- if (!(this._organizationId && this._organizationId != this.itemRef.organizationId)) {
683
- _context6.next = 17;
684
- break;
685
- }
686
- throw new Error('The existing mediaItemRef belongs to a different organization');
687
- case 17:
688
- this._organizationId = this.itemRef.organizationId;
689
- this._url = this.itemRef.url;
690
- this._ref = this.itemRef._id;
691
- case 20:
842
+ mediaItemRef = _context9.sent;
843
+ return _context9.abrupt("return", mediaItemRef === null || mediaItemRef === void 0 ? void 0 : mediaItemRef.data[0]);
844
+ case 11:
692
845
  case "end":
693
- return _context6.stop();
846
+ return _context9.stop();
694
847
  }
695
- }, _callee6, this);
848
+ }, _callee9, this);
696
849
  }));
697
- function getMediaItemRef(_x7, _x8) {
850
+ function getMediaItemRef(_x10, _x11) {
698
851
  return _getMediaItemRef.apply(this, arguments);
699
852
  }
700
853
  return getMediaItemRef;
701
854
  }()
855
+ }, {
856
+ key: "getMediaItemRefs",
857
+ value: function () {
858
+ var _getMediaItemRefs = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee10(refs, organizationId) {
859
+ var url, filter, mediaItemRef;
860
+ return _regeneratorRuntime__default["default"].wrap(function _callee10$(_context10) {
861
+ while (1) switch (_context10.prev = _context10.next) {
862
+ case 0:
863
+ url = new URL("".concat(this.config.url, "/v1/mediaitems/refs"));
864
+ filter = "{_id,in,".concat(refs.join(','), "}");
865
+ if (organizationId) {
866
+ filter = filter + ",{organizationId,eq,".concat(organizationId, "}");
867
+ }
868
+ url.searchParams.append('limit', refs.length.toString());
869
+ url.searchParams.append('filter', encodeURIComponent("[".concat(filter, "]")));
870
+ _context10.next = 7;
871
+ return fetch(url.toString(), {
872
+ method: 'GET',
873
+ headers: this.config.jwt ? {
874
+ Authorization: "Bearer ".concat(this.config.jwt)
875
+ } : {}
876
+ }).then(function (r) {
877
+ return r.json();
878
+ });
879
+ case 7:
880
+ _context10.next = 9;
881
+ return _context10.sent;
882
+ case 9:
883
+ mediaItemRef = _context10.sent;
884
+ return _context10.abrupt("return", mediaItemRef === null || mediaItemRef === void 0 ? void 0 : mediaItemRef.data);
885
+ case 11:
886
+ case "end":
887
+ return _context10.stop();
888
+ }
889
+ }, _callee10, this);
890
+ }));
891
+ function getMediaItemRefs(_x12, _x13) {
892
+ return _getMediaItemRefs.apply(this, arguments);
893
+ }
894
+ return getMediaItemRefs;
895
+ }()
702
896
  }, {
703
897
  key: "getMediaTypeFromFile",
704
898
  value: function getMediaTypeFromFile(file) {
@@ -819,9 +1013,9 @@ function index (_ref) {
819
1013
  var initState = /*#__PURE__*/function () {
820
1014
  var _ref2 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(data) {
821
1015
  var _newState$0$classes;
822
- var newState, files, i, file, newFile, fileEndingIndex;
823
- return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
824
- while (1) switch (_context.prev = _context.next) {
1016
+ var newState, files, groupedFiles, _loop, i, _i, group, _yield$group$files$0$, mediaItem, refs, _loop2, j, fileEndingIndex;
1017
+ return _regeneratorRuntime__default["default"].wrap(function _callee$(_context3) {
1018
+ while (1) switch (_context3.prev = _context3.next) {
825
1019
  case 0:
826
1020
  newState = [];
827
1021
  setLoading(true);
@@ -830,33 +1024,107 @@ function index (_ref) {
830
1024
  } else {
831
1025
  files = [data];
832
1026
  }
1027
+ groupedFiles = [];
1028
+ _loop = /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _loop() {
1029
+ var file, newFile, _group;
1030
+ return _regeneratorRuntime__default["default"].wrap(function _loop$(_context2) {
1031
+ while (1) switch (_context2.prev = _context2.next) {
1032
+ case 0:
1033
+ file = files[i];
1034
+ if (file instanceof DivaFile) {
1035
+ newState.push(file);
1036
+ } else {
1037
+ newFile = new DivaFile(config, file, createRef);
1038
+ _group = groupedFiles.find(function (g) {
1039
+ return g.url === newFile.url && g.files.length <= 250;
1040
+ });
1041
+ if (!_group) {
1042
+ groupedFiles.push({
1043
+ files: [newFile],
1044
+ url: newFile.url
1045
+ });
1046
+ } else {
1047
+ _group.files.push(newFile);
1048
+ }
1049
+ }
1050
+ case 2:
1051
+ case "end":
1052
+ return _context2.stop();
1053
+ }
1054
+ }, _loop);
1055
+ });
833
1056
  i = 0;
834
- case 4:
1057
+ case 6:
835
1058
  if (!(i < files.length)) {
836
- _context.next = 17;
1059
+ _context3.next = 11;
837
1060
  break;
838
1061
  }
839
- file = files[i];
840
- if (!(file instanceof DivaFile)) {
841
- _context.next = 10;
1062
+ return _context3.delegateYield(_loop(), "t0", 8);
1063
+ case 8:
1064
+ i++;
1065
+ _context3.next = 6;
1066
+ break;
1067
+ case 11:
1068
+ _i = 0;
1069
+ case 12:
1070
+ if (!(_i < groupedFiles.length)) {
1071
+ _context3.next = 29;
842
1072
  break;
843
1073
  }
844
- newState.push(file);
845
- _context.next = 14;
846
- break;
847
- case 10:
848
- newFile = new DivaFile(config, file, createRef);
849
- _context.next = 13;
850
- return newFile.load({
1074
+ group = groupedFiles[_i];
1075
+ _context3.next = 16;
1076
+ return group.files[0].loadMany({
1077
+ refIds: group.files.map(function (f) {
1078
+ return f.ref;
1079
+ }),
851
1080
  organizationId: organizationId
852
1081
  });
853
- case 13:
854
- newState.push(newFile);
855
- case 14:
856
- i++;
857
- _context.next = 4;
1082
+ case 16:
1083
+ _yield$group$files$0$ = _context3.sent;
1084
+ mediaItem = _yield$group$files$0$.mediaItem;
1085
+ refs = _yield$group$files$0$.refs;
1086
+ _loop2 = /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _loop2() {
1087
+ var ref, file;
1088
+ return _regeneratorRuntime__default["default"].wrap(function _loop2$(_context) {
1089
+ while (1) switch (_context.prev = _context.next) {
1090
+ case 0:
1091
+ ref = refs[j];
1092
+ file = group.files.find(function (f) {
1093
+ return f.ref === ref._id;
1094
+ });
1095
+ if (file) {
1096
+ _context.next = 4;
1097
+ break;
1098
+ }
1099
+ throw new Error('file with refid ' + ref._id + ' not found');
1100
+ case 4:
1101
+ file.setMediaItemAndRef({
1102
+ item: mediaItem,
1103
+ ref: ref
1104
+ });
1105
+ newState.push(file);
1106
+ case 6:
1107
+ case "end":
1108
+ return _context.stop();
1109
+ }
1110
+ }, _loop2);
1111
+ });
1112
+ j = 0;
1113
+ case 21:
1114
+ if (!(j < refs.length)) {
1115
+ _context3.next = 26;
1116
+ break;
1117
+ }
1118
+ return _context3.delegateYield(_loop2(), "t1", 23);
1119
+ case 23:
1120
+ j++;
1121
+ _context3.next = 21;
1122
+ break;
1123
+ case 26:
1124
+ _i++;
1125
+ _context3.next = 12;
858
1126
  break;
859
- case 17:
1127
+ case 29:
860
1128
  setState(newState);
861
1129
  if (newState[0].fileName) {
862
1130
  fileEndingIndex = newState[0].fileName.lastIndexOf('.');
@@ -868,9 +1136,9 @@ function index (_ref) {
868
1136
  setClasses((_newState$0$classes = newState[0].classes) !== null && _newState$0$classes !== void 0 ? _newState$0$classes : []);
869
1137
  setType(newState[0].mediaType == 'SPIN' ? 'spin' : 'standard');
870
1138
  setLoading(false);
871
- case 25:
1139
+ case 37:
872
1140
  case "end":
873
- return _context.stop();
1141
+ return _context3.stop();
874
1142
  }
875
1143
  }, _callee);
876
1144
  }));