@atlaskit/media-file-preview 0.0.1

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.
Files changed (89) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +13 -0
  3. package/README.md +9 -0
  4. package/dist/cjs/analytics.js +50 -0
  5. package/dist/cjs/errors.js +143 -0
  6. package/dist/cjs/getPreview/cache.js +39 -0
  7. package/dist/cjs/getPreview/getPreview.js +119 -0
  8. package/dist/cjs/getPreview/helpers.js +167 -0
  9. package/dist/cjs/getPreview/index.js +56 -0
  10. package/dist/cjs/getPreview/objectURLCache.js +85 -0
  11. package/dist/cjs/getPreview/videoSnapshot.js +63 -0
  12. package/dist/cjs/globalScope/getSSRData.js +14 -0
  13. package/dist/cjs/globalScope/globalScope.js +66 -0
  14. package/dist/cjs/globalScope/index.js +37 -0
  15. package/dist/cjs/globalScope/printScript.js +32 -0
  16. package/dist/cjs/globalScope/types.js +5 -0
  17. package/dist/cjs/helpers.js +56 -0
  18. package/dist/cjs/index.js +12 -0
  19. package/dist/cjs/types.js +5 -0
  20. package/dist/cjs/useFilePreview.js +355 -0
  21. package/dist/es2019/analytics.js +44 -0
  22. package/dist/es2019/errors.js +90 -0
  23. package/dist/es2019/getPreview/cache.js +30 -0
  24. package/dist/es2019/getPreview/getPreview.js +75 -0
  25. package/dist/es2019/getPreview/helpers.js +77 -0
  26. package/dist/es2019/getPreview/index.js +3 -0
  27. package/dist/es2019/getPreview/objectURLCache.js +44 -0
  28. package/dist/es2019/getPreview/videoSnapshot.js +41 -0
  29. package/dist/es2019/globalScope/getSSRData.js +8 -0
  30. package/dist/es2019/globalScope/globalScope.js +48 -0
  31. package/dist/es2019/globalScope/index.js +2 -0
  32. package/dist/es2019/globalScope/printScript.js +16 -0
  33. package/dist/es2019/globalScope/types.js +1 -0
  34. package/dist/es2019/helpers.js +53 -0
  35. package/dist/es2019/index.js +1 -0
  36. package/dist/es2019/types.js +1 -0
  37. package/dist/es2019/useFilePreview.js +333 -0
  38. package/dist/esm/analytics.js +44 -0
  39. package/dist/esm/errors.js +133 -0
  40. package/dist/esm/getPreview/cache.js +32 -0
  41. package/dist/esm/getPreview/getPreview.js +112 -0
  42. package/dist/esm/getPreview/helpers.js +161 -0
  43. package/dist/esm/getPreview/index.js +3 -0
  44. package/dist/esm/getPreview/objectURLCache.js +78 -0
  45. package/dist/esm/getPreview/videoSnapshot.js +56 -0
  46. package/dist/esm/globalScope/getSSRData.js +8 -0
  47. package/dist/esm/globalScope/globalScope.js +56 -0
  48. package/dist/esm/globalScope/index.js +2 -0
  49. package/dist/esm/globalScope/printScript.js +25 -0
  50. package/dist/esm/globalScope/types.js +1 -0
  51. package/dist/esm/helpers.js +49 -0
  52. package/dist/esm/index.js +1 -0
  53. package/dist/esm/types.js +1 -0
  54. package/dist/esm/useFilePreview.js +348 -0
  55. package/dist/types/analytics.d.ts +28 -0
  56. package/dist/types/errors.d.ts +42 -0
  57. package/dist/types/getPreview/cache.d.ts +21 -0
  58. package/dist/types/getPreview/getPreview.d.ts +9 -0
  59. package/dist/types/getPreview/helpers.d.ts +10 -0
  60. package/dist/types/getPreview/index.d.ts +3 -0
  61. package/dist/types/getPreview/objectURLCache.d.ts +12 -0
  62. package/dist/types/getPreview/videoSnapshot.d.ts +1 -0
  63. package/dist/types/globalScope/getSSRData.d.ts +3 -0
  64. package/dist/types/globalScope/globalScope.d.ts +15 -0
  65. package/dist/types/globalScope/index.d.ts +4 -0
  66. package/dist/types/globalScope/printScript.d.ts +2 -0
  67. package/dist/types/globalScope/types.d.ts +8 -0
  68. package/dist/types/helpers.d.ts +10 -0
  69. package/dist/types/index.d.ts +2 -0
  70. package/dist/types/types.d.ts +12 -0
  71. package/dist/types/useFilePreview.d.ts +33 -0
  72. package/dist/types-ts4.5/analytics.d.ts +28 -0
  73. package/dist/types-ts4.5/errors.d.ts +42 -0
  74. package/dist/types-ts4.5/getPreview/cache.d.ts +21 -0
  75. package/dist/types-ts4.5/getPreview/getPreview.d.ts +9 -0
  76. package/dist/types-ts4.5/getPreview/helpers.d.ts +10 -0
  77. package/dist/types-ts4.5/getPreview/index.d.ts +3 -0
  78. package/dist/types-ts4.5/getPreview/objectURLCache.d.ts +12 -0
  79. package/dist/types-ts4.5/getPreview/videoSnapshot.d.ts +1 -0
  80. package/dist/types-ts4.5/globalScope/getSSRData.d.ts +3 -0
  81. package/dist/types-ts4.5/globalScope/globalScope.d.ts +15 -0
  82. package/dist/types-ts4.5/globalScope/index.d.ts +4 -0
  83. package/dist/types-ts4.5/globalScope/printScript.d.ts +2 -0
  84. package/dist/types-ts4.5/globalScope/types.d.ts +8 -0
  85. package/dist/types-ts4.5/helpers.d.ts +10 -0
  86. package/dist/types-ts4.5/index.d.ts +2 -0
  87. package/dist/types-ts4.5/types.d.ts +12 -0
  88. package/dist/types-ts4.5/useFilePreview.d.ts +33 -0
  89. package/package.json +98 -0
@@ -0,0 +1,133 @@
1
+ import _createClass from "@babel/runtime/helpers/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
4
+ import _inherits from "@babel/runtime/helpers/inherits";
5
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
+ import _wrapNativeSuper from "@babel/runtime/helpers/wrapNativeSuper";
8
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
9
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
10
+ import { MediaFileStateError } from '@atlaskit/media-client-react';
11
+
12
+ /**
13
+ * Primary reason is logged through Data Portal.
14
+ * Make sure all the values are whitelisted in Measure -> Event Regitry -> "mediaCardRender failed" event
15
+ */
16
+
17
+ export var MediaFilePreviewError = /*#__PURE__*/function (_Error) {
18
+ _inherits(MediaFilePreviewError, _Error);
19
+ var _super = _createSuper(MediaFilePreviewError);
20
+ function MediaFilePreviewError(primaryReason, secondaryError) {
21
+ var _this;
22
+ _classCallCheck(this, MediaFilePreviewError);
23
+ _this = _super.call(this, primaryReason);
24
+ // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
25
+ _this.primaryReason = primaryReason;
26
+ _this.secondaryError = secondaryError;
27
+ Object.setPrototypeOf(_assertThisInitialized(_this), (this instanceof MediaFilePreviewError ? this.constructor : void 0).prototype);
28
+
29
+ // https://v8.dev/docs/stack-trace-api
30
+ if ('captureStackTrace' in Error) {
31
+ Error.captureStackTrace(_assertThisInitialized(_this), this instanceof MediaFilePreviewError ? this.constructor : void 0);
32
+ }
33
+ return _this;
34
+ }
35
+ return _createClass(MediaFilePreviewError);
36
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
37
+ export var LocalPreviewError = /*#__PURE__*/function (_MediaFilePreviewErro) {
38
+ _inherits(LocalPreviewError, _MediaFilePreviewErro);
39
+ var _super2 = _createSuper(LocalPreviewError);
40
+ function LocalPreviewError(primaryReason, secondaryError) {
41
+ var _this2;
42
+ _classCallCheck(this, LocalPreviewError);
43
+ _this2 = _super2.call(this, primaryReason, secondaryError);
44
+ _this2.primaryReason = primaryReason;
45
+ _this2.secondaryError = secondaryError;
46
+ return _this2;
47
+ }
48
+ return _createClass(LocalPreviewError);
49
+ }(MediaFilePreviewError);
50
+ export var RemotePreviewError = /*#__PURE__*/function (_MediaFilePreviewErro2) {
51
+ _inherits(RemotePreviewError, _MediaFilePreviewErro2);
52
+ var _super3 = _createSuper(RemotePreviewError);
53
+ function RemotePreviewError(primaryReason, secondaryError) {
54
+ var _this3;
55
+ _classCallCheck(this, RemotePreviewError);
56
+ _this3 = _super3.call(this, primaryReason, secondaryError);
57
+ _this3.primaryReason = primaryReason;
58
+ _this3.secondaryError = secondaryError;
59
+ return _this3;
60
+ }
61
+ return _createClass(RemotePreviewError);
62
+ }(MediaFilePreviewError);
63
+ export var SsrPreviewError = /*#__PURE__*/function (_MediaFilePreviewErro3) {
64
+ _inherits(SsrPreviewError, _MediaFilePreviewErro3);
65
+ var _super4 = _createSuper(SsrPreviewError);
66
+ function SsrPreviewError(primaryReason, secondaryError) {
67
+ var _this4;
68
+ _classCallCheck(this, SsrPreviewError);
69
+ _this4 = _super4.call(this, primaryReason, secondaryError);
70
+ _this4.primaryReason = primaryReason;
71
+ _this4.secondaryError = secondaryError;
72
+ return _this4;
73
+ }
74
+ return _createClass(SsrPreviewError);
75
+ }(MediaFilePreviewError);
76
+ var getImageLoadPrimaryReason = function getImageLoadPrimaryReason(source) {
77
+ switch (source) {
78
+ case 'cache-remote':
79
+ return 'cache-remote-uri';
80
+ case 'cache-local':
81
+ return 'cache-local-uri';
82
+ case 'external':
83
+ return 'external-uri';
84
+ case 'local':
85
+ return 'local-uri';
86
+ case 'remote':
87
+ return 'remote-uri';
88
+ // This fail reason will come from a bug, most likely.
89
+ default:
90
+ return "unknown-uri";
91
+ }
92
+ };
93
+ export var ImageLoadError = /*#__PURE__*/function (_MediaFilePreviewErro4) {
94
+ _inherits(ImageLoadError, _MediaFilePreviewErro4);
95
+ var _super5 = _createSuper(ImageLoadError);
96
+ function ImageLoadError(source) {
97
+ _classCallCheck(this, ImageLoadError);
98
+ return _super5.call(this, getImageLoadPrimaryReason(source));
99
+ }
100
+ return _createClass(ImageLoadError);
101
+ }(MediaFilePreviewError);
102
+ export function isMediaFilePreviewError(err) {
103
+ return err instanceof MediaFilePreviewError;
104
+ }
105
+ export var isLocalPreviewError = function isLocalPreviewError(err) {
106
+ return err instanceof LocalPreviewError;
107
+ };
108
+ export var isRemotePreviewError = function isRemotePreviewError(err) {
109
+ return err instanceof RemotePreviewError;
110
+ };
111
+ export var isUnsupportedLocalPreviewError = function isUnsupportedLocalPreviewError(err) {
112
+ return isMediaFilePreviewError(err) && err.primaryReason === 'local-preview-unsupported';
113
+ };
114
+
115
+ // In a try/catch statement, the error caught is the type of unknown.
116
+ // We can use this helper to ensure that the error handled is the type of MediaFilePreviewError if unsure
117
+ // If updatePrimaryReason is true, if it's a MediaFilePreviewError already, it will update it's primary reason
118
+ export var ensureMediaFilePreviewError = function ensureMediaFilePreviewError(primaryReason, error, updatePrimaryReason) {
119
+ if (isMediaFilePreviewError(error)) {
120
+ if (updatePrimaryReason && error.primaryReason !== primaryReason) {
121
+ return new MediaFilePreviewError(primaryReason, error.secondaryError);
122
+ }
123
+ return error;
124
+ }
125
+ return new MediaFilePreviewError(primaryReason, error);
126
+ };
127
+ export function isMediaFileStateError(err) {
128
+ return err instanceof MediaFileStateError;
129
+ }
130
+ export function getFileStateErrorReason(err) {
131
+ var _err$details$reason, _err$details;
132
+ return (_err$details$reason = (_err$details = err.details) === null || _err$details === void 0 ? void 0 : _err$details.reason) !== null && _err$details$reason !== void 0 ? _err$details$reason : 'unknown';
133
+ }
@@ -0,0 +1,32 @@
1
+ import _createClass from "@babel/runtime/helpers/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ import { createObjectURLCache } from './objectURLCache';
5
+
6
+ // Dimensions are used to create a key.
7
+ // Cache is invalidated when different dimensions are provided.
8
+ export var getCacheKey = function getCacheKey(id, mode) {
9
+ var resizeMode = mode || 'crop';
10
+ return [id, resizeMode].join('-');
11
+ };
12
+ export var CardPreviewCacheImpl = /*#__PURE__*/_createClass(function CardPreviewCacheImpl(previewCache) {
13
+ var _this = this;
14
+ _classCallCheck(this, CardPreviewCacheImpl);
15
+ _defineProperty(this, "get", function (id, mode) {
16
+ var cacheKey = getCacheKey(id, mode);
17
+ return _this.previewCache.get(cacheKey);
18
+ });
19
+ _defineProperty(this, "set", function (id, mode, cardPreview) {
20
+ var cacheKey = getCacheKey(id, mode);
21
+ _this.previewCache.set(cacheKey, cardPreview);
22
+ });
23
+ _defineProperty(this, "remove", function (id, mode) {
24
+ var cacheKey = getCacheKey(id, mode);
25
+ _this.previewCache.remove(cacheKey);
26
+ });
27
+ _defineProperty(this, "clear", function () {
28
+ _this.previewCache.clear();
29
+ });
30
+ this.previewCache = previewCache;
31
+ });
32
+ export var mediaFilePreviewCache = new CardPreviewCacheImpl(createObjectURLCache());
@@ -0,0 +1,112 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
4
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
+ import { addFileAttrsToUrl } from '@atlaskit/media-client';
7
+ import { SsrPreviewError } from '../errors';
8
+ import { mediaFilePreviewCache } from './cache';
9
+ import { getLocalPreview, getRemotePreview } from './helpers';
10
+ var extendAndCachePreview = function extendAndCachePreview(id, mode, preview, mediaBlobUrlAttrs) {
11
+ var source;
12
+ switch (preview.source) {
13
+ case 'local':
14
+ source = 'cache-local';
15
+ break;
16
+ case 'remote':
17
+ source = 'cache-remote';
18
+ break;
19
+ case 'ssr-server':
20
+ source = 'cache-ssr-server';
21
+ break;
22
+ case 'ssr-client':
23
+ source = 'cache-ssr-client';
24
+ break;
25
+ default:
26
+ source = preview.source;
27
+ }
28
+ // We want to embed some meta context into dataURI for Copy/Paste to work.
29
+ var dataURI = mediaBlobUrlAttrs ? addFileAttrsToUrl(preview.dataURI, mediaBlobUrlAttrs) : preview.dataURI;
30
+ // We store new cardPreview into cache
31
+ mediaFilePreviewCache.set(id, mode, _objectSpread(_objectSpread({}, preview), {}, {
32
+ source: source,
33
+ dataURI: dataURI
34
+ }));
35
+ return _objectSpread(_objectSpread({}, preview), {}, {
36
+ dataURI: dataURI
37
+ });
38
+ };
39
+ export var getSSRPreview = function getSSRPreview(ssr, mediaClient, id, params, mediaBlobUrlAttrs) {
40
+ var dataURI;
41
+ try {
42
+ var rawDataURI = mediaClient.getImageUrlSync(id, params);
43
+ // We want to embed some meta context into dataURI for Copy/Paste to work.
44
+ dataURI = mediaBlobUrlAttrs ? addFileAttrsToUrl(rawDataURI, mediaBlobUrlAttrs) : rawDataURI;
45
+ var source = ssr === 'client' ? 'ssr-client' : 'ssr-server';
46
+ return {
47
+ dataURI: dataURI,
48
+ source: source,
49
+ orientation: 1
50
+ };
51
+ } catch (e) {
52
+ var reason = ssr === 'server' ? 'ssr-server-uri' : 'ssr-client-uri';
53
+ throw new SsrPreviewError(reason, e instanceof Error ? e : undefined);
54
+ }
55
+ };
56
+ export var isLocalPreview = function isLocalPreview(preview) {
57
+ var localSources = ['local', 'cache-local'];
58
+ return localSources.includes(preview.source);
59
+ };
60
+ export var isSSRClientPreview = function isSSRClientPreview(preview) {
61
+ var ssrClientSources = ['ssr-client', 'cache-ssr-client'];
62
+ return ssrClientSources.includes(preview.source);
63
+ };
64
+ export var isSSRDataPreview = function isSSRDataPreview(preview) {
65
+ return preview.source === 'ssr-data';
66
+ };
67
+ export var getAndCacheRemotePreview = /*#__PURE__*/function () {
68
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(mediaClient, id, dimensions, params, mediaBlobUrlAttrs, traceContext) {
69
+ var remotePreview;
70
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
71
+ while (1) switch (_context.prev = _context.next) {
72
+ case 0:
73
+ _context.next = 2;
74
+ return getRemotePreview(mediaClient, id, params, traceContext);
75
+ case 2:
76
+ remotePreview = _context.sent;
77
+ return _context.abrupt("return", extendAndCachePreview(id, params.mode, _objectSpread(_objectSpread({}, remotePreview), {}, {
78
+ dimensions: dimensions
79
+ }), mediaBlobUrlAttrs));
80
+ case 4:
81
+ case "end":
82
+ return _context.stop();
83
+ }
84
+ }, _callee);
85
+ }));
86
+ return function getAndCacheRemotePreview(_x, _x2, _x3, _x4, _x5, _x6) {
87
+ return _ref.apply(this, arguments);
88
+ };
89
+ }();
90
+ export var getAndCacheLocalPreview = /*#__PURE__*/function () {
91
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(id, filePreview, dimensions, mode, mediaBlobUrlAttrs) {
92
+ var localPreview;
93
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
94
+ while (1) switch (_context2.prev = _context2.next) {
95
+ case 0:
96
+ _context2.next = 2;
97
+ return getLocalPreview(filePreview);
98
+ case 2:
99
+ localPreview = _context2.sent;
100
+ return _context2.abrupt("return", extendAndCachePreview(id, mode, _objectSpread(_objectSpread({}, localPreview), {}, {
101
+ dimensions: dimensions
102
+ }), mediaBlobUrlAttrs));
103
+ case 4:
104
+ case "end":
105
+ return _context2.stop();
106
+ }
107
+ }, _callee2);
108
+ }));
109
+ return function getAndCacheLocalPreview(_x7, _x8, _x9, _x10, _x11) {
110
+ return _ref2.apply(this, arguments);
111
+ };
112
+ }();
@@ -0,0 +1,161 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
3
+ import { getMediaTypeFromMimeType } from '@atlaskit/media-common';
4
+ import { getOrientation } from '@atlaskit/media-ui';
5
+ import { LocalPreviewError, RemotePreviewError } from '../errors';
6
+ import { takeSnapshot } from './videoSnapshot';
7
+
8
+ /**
9
+ * This method tells the support for the media
10
+ * types covered in getCardPreviewFromFilePreview
11
+ */
12
+ export var isSupportedLocalPreview = function isSupportedLocalPreview(mediaType) {
13
+ return mediaType === 'image' || mediaType === 'video';
14
+ };
15
+ var getImageLocalPreview = /*#__PURE__*/function () {
16
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(value) {
17
+ var orientation, dataURI;
18
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
19
+ while (1) switch (_context.prev = _context.next) {
20
+ case 0:
21
+ _context.prev = 0;
22
+ _context.next = 3;
23
+ return getOrientation(value);
24
+ case 3:
25
+ orientation = _context.sent;
26
+ dataURI = URL.createObjectURL(value);
27
+ return _context.abrupt("return", {
28
+ dataURI: dataURI,
29
+ orientation: orientation,
30
+ source: 'local'
31
+ });
32
+ case 8:
33
+ _context.prev = 8;
34
+ _context.t0 = _context["catch"](0);
35
+ throw new LocalPreviewError('local-preview-image', _context.t0 instanceof Error ? _context.t0 : undefined);
36
+ case 11:
37
+ case "end":
38
+ return _context.stop();
39
+ }
40
+ }, _callee, null, [[0, 8]]);
41
+ }));
42
+ return function getImageLocalPreview(_x) {
43
+ return _ref.apply(this, arguments);
44
+ };
45
+ }();
46
+ var getVideoLocalPreview = /*#__PURE__*/function () {
47
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(value) {
48
+ var dataURI;
49
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
50
+ while (1) switch (_context2.prev = _context2.next) {
51
+ case 0:
52
+ _context2.prev = 0;
53
+ _context2.next = 3;
54
+ return takeSnapshot(value);
55
+ case 3:
56
+ dataURI = _context2.sent;
57
+ return _context2.abrupt("return", {
58
+ dataURI: dataURI,
59
+ orientation: 1,
60
+ source: 'local'
61
+ });
62
+ case 7:
63
+ _context2.prev = 7;
64
+ _context2.t0 = _context2["catch"](0);
65
+ throw new LocalPreviewError('local-preview-video', _context2.t0 instanceof Error ? _context2.t0 : undefined);
66
+ case 10:
67
+ case "end":
68
+ return _context2.stop();
69
+ }
70
+ }, _callee2, null, [[0, 7]]);
71
+ }));
72
+ return function getVideoLocalPreview(_x2) {
73
+ return _ref2.apply(this, arguments);
74
+ };
75
+ }();
76
+ export var getLocalPreview = /*#__PURE__*/function () {
77
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(filePreview) {
78
+ var value, resolvedFilePreview, _value, type, mediaType;
79
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
80
+ while (1) switch (_context3.prev = _context3.next) {
81
+ case 0:
82
+ _context3.prev = 0;
83
+ _context3.next = 3;
84
+ return filePreview;
85
+ case 3:
86
+ resolvedFilePreview = _context3.sent;
87
+ value = resolvedFilePreview.value;
88
+ _context3.next = 10;
89
+ break;
90
+ case 7:
91
+ _context3.prev = 7;
92
+ _context3.t0 = _context3["catch"](0);
93
+ throw new LocalPreviewError('local-preview-rejected', _context3.t0 instanceof Error ? _context3.t0 : undefined);
94
+ case 10:
95
+ if (!(typeof value === 'string')) {
96
+ _context3.next = 14;
97
+ break;
98
+ }
99
+ return _context3.abrupt("return", {
100
+ dataURI: value,
101
+ orientation: 1,
102
+ source: 'local'
103
+ });
104
+ case 14:
105
+ if (!(value instanceof Blob)) {
106
+ _context3.next = 23;
107
+ break;
108
+ }
109
+ _value = value, type = _value.type;
110
+ mediaType = getMediaTypeFromMimeType(type);
111
+ _context3.t1 = mediaType;
112
+ _context3.next = _context3.t1 === 'image' ? 20 : _context3.t1 === 'video' ? 21 : 22;
113
+ break;
114
+ case 20:
115
+ return _context3.abrupt("return", getImageLocalPreview(value));
116
+ case 21:
117
+ return _context3.abrupt("return", getVideoLocalPreview(value));
118
+ case 22:
119
+ throw new LocalPreviewError('local-preview-unsupported');
120
+ case 23:
121
+ throw new LocalPreviewError('local-preview-unsupported');
122
+ case 24:
123
+ case "end":
124
+ return _context3.stop();
125
+ }
126
+ }, _callee3, null, [[0, 7]]);
127
+ }));
128
+ return function getLocalPreview(_x3) {
129
+ return _ref3.apply(this, arguments);
130
+ };
131
+ }();
132
+ export var getRemotePreview = /*#__PURE__*/function () {
133
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(mediaClient, id, params, traceContext) {
134
+ var blob;
135
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
136
+ while (1) switch (_context4.prev = _context4.next) {
137
+ case 0:
138
+ _context4.prev = 0;
139
+ _context4.next = 3;
140
+ return mediaClient.getImage(id, params, undefined, undefined, traceContext);
141
+ case 3:
142
+ blob = _context4.sent;
143
+ return _context4.abrupt("return", {
144
+ dataURI: URL.createObjectURL(blob),
145
+ orientation: 1,
146
+ source: 'remote'
147
+ });
148
+ case 7:
149
+ _context4.prev = 7;
150
+ _context4.t0 = _context4["catch"](0);
151
+ throw new RemotePreviewError('remote-preview-fetch', _context4.t0 instanceof Error ? _context4.t0 : undefined);
152
+ case 10:
153
+ case "end":
154
+ return _context4.stop();
155
+ }
156
+ }, _callee4, null, [[0, 7]]);
157
+ }));
158
+ return function getRemotePreview(_x4, _x5, _x6, _x7) {
159
+ return _ref4.apply(this, arguments);
160
+ };
161
+ }();
@@ -0,0 +1,3 @@
1
+ export { mediaFilePreviewCache } from './cache';
2
+ export { getSSRPreview, isLocalPreview, isSSRClientPreview, isSSRDataPreview, getAndCacheRemotePreview, getAndCacheLocalPreview } from './getPreview';
3
+ export { isSupportedLocalPreview } from './helpers';
@@ -0,0 +1,78 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _get from "@babel/runtime/helpers/get";
4
+ import _inherits from "@babel/runtime/helpers/inherits";
5
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
8
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
9
+ import { EventEmitter2 } from 'eventemitter2';
10
+ import { LRUMap } from 'lru_map';
11
+ export var PREVIEW_CACHE_LRU_SIZE = 50;
12
+ var ExtendedLRUCache = /*#__PURE__*/function (_LRUMap) {
13
+ _inherits(ExtendedLRUCache, _LRUMap);
14
+ var _super = _createSuper(ExtendedLRUCache);
15
+ function ExtendedLRUCache(limit) {
16
+ var _this;
17
+ _classCallCheck(this, ExtendedLRUCache);
18
+ _this = _super.call(this, limit);
19
+ _this.eventEmitter = new EventEmitter2();
20
+ return _this;
21
+ }
22
+ _createClass(ExtendedLRUCache, [{
23
+ key: "shift",
24
+ value: function shift() {
25
+ var entry = _get(_getPrototypeOf(ExtendedLRUCache.prototype), "shift", this).call(this);
26
+ this.eventEmitter.emit('shift', entry);
27
+ return entry;
28
+ }
29
+ }, {
30
+ key: "on",
31
+ value: function on(event, callback) {
32
+ this.eventEmitter.on(event, callback);
33
+ }
34
+ }]);
35
+ return ExtendedLRUCache;
36
+ }(LRUMap);
37
+ export var ObjectURLCache = /*#__PURE__*/function () {
38
+ function ObjectURLCache(size) {
39
+ _classCallCheck(this, ObjectURLCache);
40
+ this.cache = new ExtendedLRUCache(size);
41
+ this.cache.on('shift', function (entry) {
42
+ if (entry && entry[1].dataURI) {
43
+ URL.revokeObjectURL(entry[1].dataURI);
44
+ }
45
+ });
46
+ }
47
+ _createClass(ObjectURLCache, [{
48
+ key: "has",
49
+ value: function has(key) {
50
+ return !!this.cache.find(key);
51
+ }
52
+ }, {
53
+ key: "get",
54
+ value: function get(key) {
55
+ return this.cache.get(key);
56
+ }
57
+ }, {
58
+ key: "set",
59
+ value: function set(key, value) {
60
+ this.cache.set(key, value);
61
+ }
62
+ }, {
63
+ key: "remove",
64
+ value: function remove(key) {
65
+ var removed = this.cache.delete(key);
66
+ removed && URL.revokeObjectURL(removed.dataURI);
67
+ }
68
+ }, {
69
+ key: "clear",
70
+ value: function clear() {
71
+ this.cache.clear();
72
+ }
73
+ }]);
74
+ return ObjectURLCache;
75
+ }();
76
+ export var createObjectURLCache = function createObjectURLCache() {
77
+ return new ObjectURLCache(PREVIEW_CACHE_LRU_SIZE);
78
+ };
@@ -0,0 +1,56 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
3
+ export var takeSnapshot = /*#__PURE__*/function () {
4
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(blob) {
5
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
6
+ while (1) switch (_context.prev = _context.next) {
7
+ case 0:
8
+ return _context.abrupt("return", new Promise(function (resolve, reject) {
9
+ var url = URL.createObjectURL(blob);
10
+ var video = document.createElement('video');
11
+ video.preload = 'metadata';
12
+ video.src = url;
13
+ video.muted = true;
14
+ video.play().catch(function () {
15
+ return reject(new Error('failed to play video'));
16
+ });
17
+
18
+ // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
19
+ video.addEventListener('timeupdate', function timeUpdateHandler() {
20
+ // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
21
+ video.removeEventListener('timeupdate', timeUpdateHandler);
22
+ video.pause();
23
+ URL.revokeObjectURL(url);
24
+ //create canvas to draw our first frame on.
25
+
26
+ if (!video.videoWidth && !video.videoHeight) {
27
+ return reject(new Error('error retrieving video dimensions'));
28
+ }
29
+ var canvas = document.createElement('canvas');
30
+ canvas.width = video.videoWidth;
31
+ canvas.height = video.videoHeight;
32
+ var context = canvas.getContext('2d');
33
+ if (!context) {
34
+ return reject(new Error('error creating canvas context'));
35
+ }
36
+ context.drawImage(video, 0, 0, canvas.width, canvas.height);
37
+ var dataURL = canvas.toDataURL('image/jpeg', 0.85);
38
+ resolve(dataURL);
39
+ });
40
+
41
+ // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
42
+ video.addEventListener('error', function () {
43
+ reject(new Error('failed to load video'));
44
+ URL.revokeObjectURL(url);
45
+ });
46
+ }));
47
+ case 1:
48
+ case "end":
49
+ return _context.stop();
50
+ }
51
+ }, _callee);
52
+ }));
53
+ return function takeSnapshot(_x) {
54
+ return _ref.apply(this, arguments);
55
+ };
56
+ }();
@@ -0,0 +1,8 @@
1
+ import { getKey, getMediaCardSSR } from './globalScope';
2
+ export var getSSRData = function getSSRData(identifier) {
3
+ var mediaCardSsr = getMediaCardSSR();
4
+ if (!mediaCardSsr) {
5
+ return;
6
+ }
7
+ return mediaCardSsr[getKey(identifier)];
8
+ };
@@ -0,0 +1,56 @@
1
+ import { printFunctionCall, printScript } from './printScript';
2
+ // ----- WARNING -----
3
+ // This is a very sensitive fraction of code.
4
+ // Any changes to this file must be tested directly in product before merging.
5
+ // The scripts printed here might differ from what we observe in our internal tests
6
+ // due to minimification, for example.
7
+ export var GLOBAL_MEDIA_CARD_SSR = 'mediaCardSsr';
8
+ export var GLOBAL_MEDIA_NAMESPACE = '__MEDIA_INTERNAL';
9
+ export function getMediaGlobalScope() {
10
+ var globalScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
11
+ // Must match GLOBAL_MEDIA_NAMESPACE. Can't reference the constant from here.
12
+ var namespace = '__MEDIA_INTERNAL';
13
+ if (!globalScope[namespace]) {
14
+ globalScope[namespace] = {};
15
+ }
16
+ return globalScope[namespace];
17
+ }
18
+ export function getMediaCardSSR() {
19
+ var globalScope = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
20
+ var globalMedia = getMediaGlobalScope(globalScope);
21
+ // Must match GLOBAL_MEDIA_CARD_SSR. Can't reference the constant from here.
22
+ var key = 'mediaCardSsr';
23
+ if (!globalMedia[key]) {
24
+ globalMedia[key] = {};
25
+ }
26
+ return globalMedia[key];
27
+ }
28
+ var dashed = function dashed(param) {
29
+ return param ? "-".concat(param) : '';
30
+ };
31
+ export var getKey = function getKey(_ref) {
32
+ var id = _ref.id,
33
+ collectionName = _ref.collectionName,
34
+ occurrenceKey = _ref.occurrenceKey;
35
+ return "".concat(id).concat(dashed(collectionName)).concat(dashed(occurrenceKey));
36
+ };
37
+ export var storeDataURI = function storeDataURI(key, dataURI, dimensions, error) {
38
+ var globalScope = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : window;
39
+ var mediaCardSsr = getMediaCardSSR(globalScope);
40
+ mediaCardSsr[key] = {
41
+ dataURI: dataURI,
42
+ dimensions: dimensions,
43
+ error: error
44
+ };
45
+ };
46
+ var generateScript = function generateScript(identifier, dataURI, dimensions, error) {
47
+ var functionCall = printFunctionCall(storeDataURI, getKey(identifier), dataURI, dimensions, error);
48
+ return printScript([getMediaCardSSR.toString(), getMediaGlobalScope.toString(), functionCall]);
49
+ };
50
+ export var generateScriptProps = function generateScriptProps(identifier, dataURI, dimensions, error) {
51
+ return {
52
+ dangerouslySetInnerHTML: {
53
+ __html: generateScript(identifier, dataURI, dimensions, error)
54
+ }
55
+ };
56
+ };
@@ -0,0 +1,2 @@
1
+ export { GLOBAL_MEDIA_NAMESPACE, GLOBAL_MEDIA_CARD_SSR, generateScriptProps, getKey } from './globalScope';
2
+ export { getSSRData } from './getSSRData';
@@ -0,0 +1,25 @@
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
+ var printParam = function printParam(param) {
3
+ if (typeof param === 'string') {
4
+ return "'".concat(param, "'");
5
+ } else if (_typeof(param) === 'object') {
6
+ return JSON.stringify(param);
7
+ } else if (param === undefined) {
8
+ return 'undefined';
9
+ }
10
+ return param;
11
+ };
12
+ var printParams = function printParams(args) {
13
+ return args.map(function (arg) {
14
+ return printParam(arg);
15
+ }).join(',');
16
+ };
17
+ export var printFunctionCall = function printFunctionCall(fn) {
18
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
19
+ args[_key - 1] = arguments[_key];
20
+ }
21
+ return "(".concat(fn.toString(), ")(").concat(printParams(args), ");");
22
+ };
23
+ export var printScript = function printScript(statements) {
24
+ return "(function(){\n ".concat(statements.join(';'), "\n})();\n");
25
+ };