datasync-blob 1.1.22 → 1.1.24

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.
@@ -5,21 +5,26 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.DsBlob = void 0;
7
7
  var _react = _interopRequireWildcard(require("react"));
8
+ var _propTypes = _interopRequireDefault(require("prop-types"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
10
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
9
11
  class DsBlob extends _react.Component {
10
12
  constructor(props) {
11
13
  super(props);
12
14
  this.state = {
13
15
  hover_input: false,
14
- hover_image: false
16
+ hover_image: false,
17
+ data: props.data || "",
18
+ binaryData: props.binaryData || null
15
19
  };
16
20
  this.debugging = false;
17
21
  }
18
22
 
19
23
  //-----------------------------------------------------------------------------------------------------------------------------------------
20
- reduceBinaryImage = e => {
24
+ reduceBase64Image = e => {
21
25
  let imageSource = e.path && e.path[0] || e.srcElement; //Safari compliancy
22
26
 
27
+ alert("reduceBase64Image !");
23
28
  if (this.debugging) console.log("imageSource = ", imageSource);
24
29
  var canvas = document.createElement('canvas'),
25
30
  context,
@@ -77,17 +82,17 @@ class DsBlob extends _react.Component {
77
82
  let canvasData = canvas.toDataURL('image/jpg', jpegCompressionRatio); //Jpeg conversion
78
83
 
79
84
  if (this.debugging) console.log("jpegCompressionRatio->", jpegCompressionRatio, " canvasData.length = ", canvasData.length);
85
+ const processedData = this.props.removebase64 ? canvasData ? canvasData.substring("data:image/png;base64".length) : "" : canvasData ? canvasData : "";
86
+ this.setData(processedData);
80
87
  if (this.props.removebase64) {
81
88
  //remove base64 prefix if property is set
82
89
  this.props.uploadPicture({
83
- data: canvasData ? canvasData.substring("data:image/png;base64".length) : "",
84
- item_id: this.props.item_id
90
+ data: processedData
85
91
  });
86
92
  } else {
87
93
  //Keep base 64 prefix as it is
88
94
  this.props.uploadPicture({
89
- data: canvasData ? canvasData : "",
90
- item_id: this.props.item_id
95
+ data: processedData
91
96
  });
92
97
  }
93
98
  };
@@ -150,21 +155,30 @@ class DsBlob extends _react.Component {
150
155
 
151
156
  //Binary conversion using toBlob to maintain binary output
152
157
  let jpegCompressionRatio = this.props.jpegQuality && this.props.jpegQuality > 0 && this.props.jpegQuality < 1 ? this.props.jpegQuality : 1;
153
- canvas.toBlob(blob => {
158
+ canvas.toBlob(async blob => {
154
159
  if (blob) {
155
160
  console.log("Binary blob created, size:", blob.size);
156
161
  if (this.debugging) console.log("jpegCompressionRatio->", jpegCompressionRatio, " blob.size = ", blob.size);
157
162
 
158
- // Convert blob to ArrayBuffer for binary data
163
+ // Fallback: use blob URL for display but keep binary data
164
+ const blobUrl = URL.createObjectURL(blob);
165
+
166
+ // Convert blob to ArrayBuffer to maintain genuine binary data
159
167
  const reader = new FileReader();
160
168
  reader.onload = () => {
161
- const binaryData = reader.result; // ArrayBuffer
162
- console.log("Binary data ArrayBuffer:", binaryData);
169
+ const binaryData = reader.result; // ArrayBuffer - genuine binary data
170
+ console.log("Fallback: Using binary data, size:", binaryData.byteLength);
163
171
 
164
- // Pass binary data directly to uploadPicture
172
+ // Store both display URL and binary data
173
+ this.setData(blobUrl, binaryData);
165
174
  this.props.uploadPicture({
166
- data: binaryData,
167
- item_id: this.props.item_id
175
+ data: blobUrl,
176
+ // For display in img tag
177
+ binaryData: binaryData,
178
+ // Genuine JPEG binary stream
179
+ cloud_url_prefix: this.props.cloud_url_prefix,
180
+ // Pass cloud URL prefix if needed for parent component
181
+ isBlobUrl: true // Flag to track blob URLs for cleanup
168
182
  });
169
183
  };
170
184
  reader.readAsArrayBuffer(blob);
@@ -177,9 +191,9 @@ class DsBlob extends _react.Component {
177
191
  //-----------------------------------------------------------------------------------------------------------------------------------------
178
192
  storeBase64ImageToImg = e => {
179
193
  var image = document.createElement('img');
180
- image.onload = this.reduceBinaryImage;
181
- console.log("storeImageToImg:e.currentTarget.result = ", e.currentTarget.result);
182
- alert("storeImageToImg !");
194
+ image.onload = this.reduceBase64Image;
195
+ console.log("storeBase64ImageToImg:e.currentTarget.result = ", e.currentTarget.result);
196
+ alert("storeBase64ImageToImg !");
183
197
  image.src = e.currentTarget.result;
184
198
  };
185
199
 
@@ -231,12 +245,51 @@ class DsBlob extends _react.Component {
231
245
 
232
246
  //-----------------------------------------------------------------------------------------------------------------------------------------
233
247
  resetUpload = () => {
248
+ // Clean up any blob URLs before resetting
249
+ this.cleanupBlobUrl();
250
+ this.setData("", null);
234
251
  this.props.uploadPicture({
235
252
  data: "",
236
- item_id: this.props.item_id
253
+ cloud_url_prefix: this.props.cloud_url_prefix,
254
+ isBlobUrl: false
237
255
  });
256
+ //this.props.uploadPicture({data:"",cloud_url_prefix:this.props.cloud_url_prefix, isBlobUrl:true});
257
+ };
258
+
259
+ //-----------------------------------------------------------------------------------------------------------------------------------------
260
+ cleanupBlobUrl = () => {
261
+ // Only show alert if not in test environment
262
+ if (typeof jest === 'undefined') {
263
+ alert("cleanupBlobUrl !");
264
+ }
265
+ // Revoke blob URL if it exists to prevent memory leaks
266
+ if (this.state.data && this.state.data.startsWith('blob:')) {
267
+ URL.revokeObjectURL(this.state.data);
268
+ console.log("Blob URL cleaned up:", this.state.data);
269
+ }
238
270
  };
239
271
 
272
+ //-----------------------------------------------------------------------------------------------------------------------------------------
273
+ componentDidUpdate(prevProps) {
274
+ // Sync state with prop changes from parent
275
+ if (prevProps.data !== this.props.data) {
276
+ this.setState({
277
+ data: this.props.data || ""
278
+ });
279
+ }
280
+ if (prevProps.binaryData !== this.props.binaryData) {
281
+ this.setState({
282
+ binaryData: this.props.binaryData || null
283
+ });
284
+ }
285
+ }
286
+
287
+ //-----------------------------------------------------------------------------------------------------------------------------------------
288
+ componentWillUnmount() {
289
+ // Cleanup blob URLs when component unmounts
290
+ this.cleanupBlobUrl();
291
+ }
292
+
240
293
  //-----------------------------------------------------------------------------------------------------------------------------------------
241
294
  onInputChange = e => {
242
295
  const errs = [];
@@ -276,6 +329,54 @@ class DsBlob extends _react.Component {
276
329
  return errs.forEach(err => alert(err));
277
330
  }
278
331
  };
332
+
333
+ //-----------------------------------------------------------------------------------------------------------------------------------------
334
+ getImageSrc = () => {
335
+ if (!this.state.data) return "";
336
+
337
+ // Check if it's already a complete URL (cloud storage URL or blob URL)
338
+ if (this.state.data.startsWith('http://') || this.state.data.startsWith('https://') || this.state.data.startsWith('blob:')) {
339
+ return this.state.data;
340
+ }
341
+
342
+ // Check if it's already a data URL (base64)
343
+ if (this.state.data.startsWith('data:')) {
344
+ return this.state.data;
345
+ }
346
+
347
+ // Otherwise, treat as base64 and add appropriate prefix
348
+ const prefix = this.props.removebase64 ? "data:image/png;base64," : "";
349
+ return prefix + this.state.data;
350
+ };
351
+
352
+ //-----------------------------------------------------------------------------------------------------------------------------------------
353
+ getBinaryData = () => {
354
+ // Return the genuine JPEG binary stream (ArrayBuffer)
355
+ // This can be used by parent components for uploading or processing
356
+ return this.state.binaryData || this.props.binaryData || null;
357
+ };
358
+
359
+ //-----------------------------------------------------------------------------------------------------------------------------------------
360
+ //-----------------------------------------------------------------------------------------------------------------------------------------
361
+ // Data getter and setter methods for read-write access
362
+ getData = () => {
363
+ return this.state.data;
364
+ };
365
+ setData = (data, binaryData = null) => {
366
+ this.setState({
367
+ data: data || "",
368
+ binaryData: binaryData
369
+ });
370
+ };
371
+
372
+ //-----------------------------------------------------------------------------------------------------------------------------------------
373
+ getImageType = () => {
374
+ // Return the type of image data being used
375
+ if (this.props.isCloudUrl) return 'cloud';
376
+ if (this.props.isBlobUrl) return 'blob';
377
+ if (this.state.data && this.state.data.startsWith('data:')) return 'base64';
378
+ return 'unknown';
379
+ };
279
380
  render() {
280
381
  const upload_picture_label = {
281
382
  cursor: "pointer"
@@ -305,7 +406,7 @@ class DsBlob extends _react.Component {
305
406
  display: "none",
306
407
  cursor: "pointer"
307
408
  };
308
- return /*#__PURE__*/_react.default.createElement("div", null, !this.props.data && /*#__PURE__*/_react.default.createElement("label", {
409
+ return /*#__PURE__*/_react.default.createElement("div", null, !this.state.data && /*#__PURE__*/_react.default.createElement("label", {
309
410
  id: "upload-picture-label",
310
411
  className: this.props.buttonStyle,
311
412
  style: upload_picture_label
@@ -316,7 +417,7 @@ class DsBlob extends _react.Component {
316
417
  accept: "image/*",
317
418
  onChange: this.onInputChange,
318
419
  multiple: true
319
- })), this.props.data && this.props.data.length > 0 && /*#__PURE__*/_react.default.createElement("div", {
420
+ })), this.state.data && this.state.data.length > 0 && /*#__PURE__*/_react.default.createElement("div", {
320
421
  class: "show-image",
321
422
  style: div_show_image,
322
423
  onMouseOver: () => {
@@ -337,7 +438,7 @@ class DsBlob extends _react.Component {
337
438
  }
338
439
  }, /*#__PURE__*/_react.default.createElement("img", {
339
440
  style: this.state.hover_image ? div_show_image_hover : div_show_image,
340
- src: this.props.removebase64 ? "data:image/png;base64" : "" + this.props.data,
441
+ src: this.getImageSrc(),
341
442
  className: this.props.pictureStyle
342
443
  }), !this.props.readOnly && /*#__PURE__*/_react.default.createElement("input", {
343
444
  style: this.state.hover_input ? div_show_image_hover_input : div_show_image_input,
@@ -348,4 +449,31 @@ class DsBlob extends _react.Component {
348
449
  })));
349
450
  }
350
451
  }
351
- exports.DsBlob = DsBlob;
452
+ exports.DsBlob = DsBlob;
453
+ DsBlob.propTypes = {
454
+ // Image handling props (note: data and binaryData are managed as read-write state internally)
455
+ data: _propTypes.default.string,
456
+ binaryData: _propTypes.default.object,
457
+ // Upload callback
458
+ uploadPicture: _propTypes.default.func.isRequired,
459
+ // Image size constraints
460
+ width: _propTypes.default.number,
461
+ height: _propTypes.default.number,
462
+ maxWidth: _propTypes.default.number,
463
+ maxHeight: _propTypes.default.number,
464
+ // Image processing options
465
+ reduceImage: _propTypes.default.bool,
466
+ jpegQuality: _propTypes.default.number,
467
+ removebase64: _propTypes.default.bool,
468
+ // Cloud storage options
469
+ cloud_storage: _propTypes.default.bool,
470
+ cloud_url_prefix: _propTypes.default.string,
471
+ // UI props
472
+ Caption: _propTypes.default.string,
473
+ buttonStyle: _propTypes.default.string,
474
+ pictureStyle: _propTypes.default.string,
475
+ readOnly: _propTypes.default.bool,
476
+ // Status flags
477
+ isCloudUrl: _propTypes.default.bool,
478
+ isBlobUrl: _propTypes.default.bool
479
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datasync-blob",
3
- "version": "1.1.22",
3
+ "version": "1.1.24",
4
4
  "description": "Datasync Blob component",
5
5
  "main": "./dist/components/DsBlob.js",
6
6
  "files": [