UNPKG

7.14 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8var _react = _interopRequireWildcard(require("react"));
9
10var _reactNative = require("react-native");
11
12var _awsAmplify = require("aws-amplify");
13
14var _AmplifyTheme = _interopRequireDefault(require("../AmplifyTheme"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
17
18function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
19
20function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
22function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
23
24function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
26function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
27
28function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
29
30function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
31
32function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
33
34function _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); }; }
35
36function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
37
38function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
39
40function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
41
42function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
43
44var logger = new _awsAmplify.Logger('Storage.S3Image');
45
46var S3Image = /*#__PURE__*/function (_Component) {
47 _inherits(S3Image, _Component);
48
49 var _super = _createSuper(S3Image);
50
51 function S3Image(props) {
52 var _this;
53
54 _classCallCheck(this, S3Image);
55
56 _this = _super.call(this, props);
57 _this.state = {
58 src: null
59 };
60 return _this;
61 }
62
63 _createClass(S3Image, [{
64 key: "getImageSource",
65 value: function getImageSource() {
66 var _this2 = this;
67
68 var _this$props = this.props,
69 imgKey = _this$props.imgKey,
70 level = _this$props.level;
71
72 _awsAmplify.Storage.get(imgKey, {
73 level: level ? level : 'public'
74 }).then(function (url) {
75 logger.debug(url);
76
77 _this2.setState({
78 src: {
79 uri: url
80 }
81 });
82 })["catch"](function (err) {
83 return logger.warn(err);
84 });
85 }
86 }, {
87 key: "load",
88 value: function load() {
89 var _this$props2 = this.props,
90 imgKey = _this$props2.imgKey,
91 body = _this$props2.body,
92 contentType = _this$props2.contentType,
93 level = _this$props2.level;
94
95 if (!imgKey) {
96 logger.debug('empty imgKey');
97 return;
98 }
99
100 var that = this;
101 logger.debug('loading ' + imgKey + '...');
102
103 if (body) {
104 var type = contentType ? contentType : 'binary/octet-stream';
105 var opt = {
106 contentType: type,
107 level: level ? level : 'public'
108 };
109
110 var ret = _awsAmplify.Storage.put(imgKey, body, opt);
111
112 ret.then(function (data) {
113 logger.debug(data);
114 that.getImageSource();
115 })["catch"](function (err) {
116 return logger.warn(err);
117 });
118 } else {
119 that.getImageSource();
120 }
121 }
122 }, {
123 key: "componentDidMount",
124 value: function componentDidMount() {
125 this.load();
126 }
127 }, {
128 key: "componentDidUpdate",
129 value: function componentDidUpdate(prevProps) {
130 if (prevProps.imgKey !== this.props.imgKey || prevProps.body !== this.props.body) {
131 this.load();
132 }
133 }
134 }, {
135 key: "render",
136 value: function render() {
137 var src = this.state.src;
138
139 if (!src) {
140 return null;
141 }
142
143 var _this$props3 = this.props,
144 style = _this$props3.style,
145 resizeMode = _this$props3.resizeMode;
146 var theme = this.props.theme || _AmplifyTheme["default"];
147 var photoStyle = Object.assign({}, _reactNative.StyleSheet.flatten(theme.photo), style);
148 return /*#__PURE__*/_react["default"].createElement(_reactNative.Image, {
149 source: src,
150 resizeMode: resizeMode,
151 style: photoStyle
152 });
153 }
154 }]);
155
156 return S3Image;
157}(_react.Component);
158
159exports["default"] = S3Image;
\No newline at end of file