@atlaskit/media-client 39.4.11 → 39.4.13

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.
@@ -11,6 +11,7 @@ import { isPathBasedEnabled } from '../../utils/isPathBasedEnabled';
11
11
  import { mapToMediaCdnUrl } from '../../utils/mapToMediaCdnUrl';
12
12
  import { mapToPathBasedUrl } from '../../utils/mapToPathBasedUrl';
13
13
  import { mapToSeedBasedCdnUrl } from '../../utils/mapToSeedBasedCdnUrl';
14
+ import { MEDIA_TOKEN_LENGTH_LIMIT } from '../../utils/mediaCdn';
14
15
  import { isRequestError, request } from '../../utils/request';
15
16
  import { createMapResponseToBlob } from '../../utils/request/createMapResponseToBlob';
16
17
  import { createMapResponseToJson } from '../../utils/request/createMapResponseToJson';
@@ -264,16 +265,20 @@ export class MediaStore {
264
265
  };
265
266
  return this.request('/upload/createWithFiles', requestOptions).then(createMapResponseToJson(metadata));
266
267
  }
267
- getFile(fileId, params = {}, traceContext) {
268
+ async getFile(fileId, params = {}, traceContext) {
268
269
  const metadata = {
269
270
  method: 'GET',
270
271
  endpoint: '/file/{fileId}'
271
272
  };
272
273
  const options = {
273
274
  ...metadata,
274
- authContext: {
275
- collectionName: params.collection
276
- },
275
+ ...(fg('platform_media_bounded_read_tokens') ? {
276
+ resolvedAuth: await this.resolveReadAuth(fileId, params.collection)
277
+ } : {
278
+ authContext: {
279
+ collectionName: params.collection
280
+ }
281
+ }),
277
282
  params,
278
283
  traceContext
279
284
  };
@@ -283,9 +288,7 @@ export class MediaStore {
283
288
  const {
284
289
  collection: collectionName
285
290
  } = params || {};
286
- const auth = await this.resolveAuth({
287
- collectionName
288
- });
291
+ const auth = await this.resolveReadAuth(id, collectionName);
289
292
  return this.createFileImageURL(id, auth, params);
290
293
  }
291
294
 
@@ -323,9 +326,13 @@ export class MediaStore {
323
326
  };
324
327
  const options = {
325
328
  ...metadata,
326
- authContext: {
327
- collectionName
328
- },
329
+ ...(fg('platform_media_bounded_read_tokens') ? {
330
+ resolvedAuth: await this.resolveReadAuth(id, collectionName)
331
+ } : {
332
+ authContext: {
333
+ collectionName
334
+ }
335
+ }),
329
336
  headers,
330
337
  params: {
331
338
  collection: collectionName,
@@ -335,9 +342,7 @@ export class MediaStore {
335
342
  return this.request(`/file/${id}/${binaryEndpoint}`, options, abortController, true).then(createMapResponseToBlob(metadata));
336
343
  }
337
344
  async getFileBinaryURL(id, collectionName, maxAge = FILE_CACHE_MAX_AGE, name) {
338
- const auth = await this.resolveAuth({
339
- collectionName
340
- });
345
+ const auth = await this.resolveReadAuth(id, collectionName);
341
346
  const options = {
342
347
  params: {
343
348
  dl: true,
@@ -416,13 +421,16 @@ export class MediaStore {
416
421
  endpoint: `/file/{fileId}/${imageEndpoint}`
417
422
  };
418
423
  let wmvParams = {};
419
- let authOptions = {
424
+ let authOptions = fg('platform_media_bounded_read_tokens') ? {
425
+ resolvedAuth: await this.resolveReadAuth(id, params && params.collection)
426
+ } : {
420
427
  authContext: {
421
428
  collectionName: params && params.collection
422
429
  }
423
430
  };
424
431
  if (fg('confluence_watermark_admin_ui') || fg('jira_enable_media_watermark_admin_ui')) {
425
- const auth = await this.resolveAuth(authOptions.authContext);
432
+ var _authOptions$resolved;
433
+ const auth = (_authOptions$resolved = authOptions.resolvedAuth) !== null && _authOptions$resolved !== void 0 ? _authOptions$resolved : await this.resolveAuth(authOptions.authContext);
426
434
  const wmv = getWatermarkVersionFromToken(auth.token);
427
435
  if (wmv) {
428
436
  wmvParams = {
@@ -448,28 +456,66 @@ export class MediaStore {
448
456
  return this.request(`/file/${id}/${imageEndpoint}`, options, controller, true).then(createMapResponseToBlob(metadata));
449
457
  }
450
458
  async getItems(ids, collectionName, traceContext, includeHashForDuplicateFiles) {
451
- const descriptors = ids.map(id => ({
452
- type: 'file',
453
- id,
454
- collection: collectionName
455
- }));
456
459
  const metadata = {
457
460
  method: 'POST',
458
461
  endpoint: '/items'
459
462
  };
460
- const options = {
461
- ...metadata,
462
- authContext: {
463
- collectionName
464
- },
465
- headers: jsonHeaders,
466
- body: JSON.stringify({
467
- descriptors,
468
- includeHashForDuplicateFiles
469
- }),
470
- traceContext
463
+ const requestItems = (fileIds, resolvedAuth) => {
464
+ const descriptors = fileIds.map(id => ({
465
+ type: 'file',
466
+ id,
467
+ collection: collectionName
468
+ }));
469
+ return this.request('/items', {
470
+ ...metadata,
471
+ authContext: {
472
+ collectionName
473
+ },
474
+ resolvedAuth,
475
+ headers: jsonHeaders,
476
+ body: JSON.stringify({
477
+ descriptors,
478
+ includeHashForDuplicateFiles
479
+ }),
480
+ traceContext
481
+ }).then(createMapResponseToJson(metadata));
482
+ };
483
+ if (!fg('platform_media_bounded_read_tokens')) {
484
+ return requestItems(ids);
485
+ }
486
+ const auth = await this.resolveAuth({
487
+ collectionName
488
+ });
489
+ if (auth.token.length <= MEDIA_TOKEN_LENGTH_LIMIT) {
490
+ return requestItems(ids, auth);
491
+ }
492
+ const fileAuth = await Promise.all(ids.map(id => this.resolveAuth({
493
+ collectionName,
494
+ access: [{
495
+ type: 'file',
496
+ id,
497
+ actions: ['read']
498
+ }]
499
+ })));
500
+ const groups = new Map();
501
+ fileAuth.forEach((resolvedAuth, index) => {
502
+ const key = JSON.stringify(resolvedAuth);
503
+ const group = groups.get(key);
504
+ if (group) {
505
+ group.ids.push(ids[index]);
506
+ } else {
507
+ groups.set(key, {
508
+ auth: resolvedAuth,
509
+ ids: [ids[index]]
510
+ });
511
+ }
512
+ });
513
+ const responses = await Promise.all(Array.from(groups.values(), group => requestItems(group.ids, group.auth)));
514
+ return {
515
+ data: {
516
+ items: responses.flatMap(response => response.data.items)
517
+ }
471
518
  };
472
- return this.request('/items', options).then(createMapResponseToJson(metadata));
473
519
  }
474
520
  async getImageMetadata(id, params, traceContext) {
475
521
  const metadata = {
@@ -478,9 +524,13 @@ export class MediaStore {
478
524
  };
479
525
  const options = {
480
526
  ...metadata,
481
- authContext: {
482
- collectionName: params && params.collection
483
- },
527
+ ...(fg('platform_media_bounded_read_tokens') ? {
528
+ resolvedAuth: await this.resolveReadAuth(id, params && params.collection)
529
+ } : {
530
+ authContext: {
531
+ collectionName: params && params.collection
532
+ }
533
+ }),
484
534
  params,
485
535
  traceContext
486
536
  };
@@ -501,9 +551,13 @@ export class MediaStore {
501
551
  } : {};
502
552
  const requestOptions = {
503
553
  ...metadata,
504
- authContext: {
505
- collectionName: options.collectionName
506
- },
554
+ ...(fg('platform_media_bounded_read_tokens') ? {
555
+ resolvedAuth: await this.resolveReadAuth(id, options.collectionName)
556
+ } : {
557
+ authContext: {
558
+ collectionName: options.collectionName
559
+ }
560
+ }),
507
561
  headers,
508
562
  params: {
509
563
  collection: options.collectionName,
@@ -530,9 +584,13 @@ export class MediaStore {
530
584
  } : {};
531
585
  const requestOptions = {
532
586
  ...metadata,
533
- authContext: {
534
- collectionName: options.collectionName
535
- },
587
+ ...(fg('platform_media_bounded_read_tokens') ? {
588
+ resolvedAuth: await this.resolveReadAuth(id, options.collectionName)
589
+ } : {
590
+ authContext: {
591
+ collectionName: options.collectionName
592
+ }
593
+ }),
536
594
  headers,
537
595
  params: {
538
596
  collection: options.collectionName,
@@ -689,6 +747,22 @@ export class MediaStore {
689
747
  }
690
748
  });
691
749
  }
750
+ async resolveReadAuth(id, collectionName) {
751
+ const auth = await this.resolveAuth({
752
+ collectionName
753
+ });
754
+ if (!fg('platform_media_bounded_read_tokens') || auth.token.length <= MEDIA_TOKEN_LENGTH_LIMIT) {
755
+ return auth;
756
+ }
757
+ return this.resolveAuth({
758
+ collectionName,
759
+ access: [{
760
+ type: 'file',
761
+ id,
762
+ actions: ['read']
763
+ }]
764
+ });
765
+ }
692
766
  get chunkHashAlgorithm() {
693
767
  return this._chunkHashAlgorithm;
694
768
  }