datasync-blob 1.1.22 → 1.1.23

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.
@@ -17,9 +17,10 @@ class DsBlob extends _react.Component {
17
17
  }
18
18
 
19
19
  //-----------------------------------------------------------------------------------------------------------------------------------------
20
- reduceBinaryImage = e => {
20
+ reduceBase64Image = e => {
21
21
  let imageSource = e.path && e.path[0] || e.srcElement; //Safari compliancy
22
22
 
23
+ alert("reduceBase64Image !");
23
24
  if (this.debugging) console.log("imageSource = ", imageSource);
24
25
  var canvas = document.createElement('canvas'),
25
26
  context,
@@ -150,21 +151,28 @@ class DsBlob extends _react.Component {
150
151
 
151
152
  //Binary conversion using toBlob to maintain binary output
152
153
  let jpegCompressionRatio = this.props.jpegQuality && this.props.jpegQuality > 0 && this.props.jpegQuality < 1 ? this.props.jpegQuality : 1;
153
- canvas.toBlob(blob => {
154
+ canvas.toBlob(async blob => {
154
155
  if (blob) {
155
156
  console.log("Binary blob created, size:", blob.size);
156
157
  if (this.debugging) console.log("jpegCompressionRatio->", jpegCompressionRatio, " blob.size = ", blob.size);
157
158
 
158
- // Convert blob to ArrayBuffer for binary data
159
+ // Fallback: use blob URL for display but keep binary data
160
+ const blobUrl = URL.createObjectURL(blob);
161
+
162
+ // Convert blob to ArrayBuffer to maintain genuine binary data
159
163
  const reader = new FileReader();
160
164
  reader.onload = () => {
161
- const binaryData = reader.result; // ArrayBuffer
162
- console.log("Binary data ArrayBuffer:", binaryData);
165
+ const binaryData = reader.result; // ArrayBuffer - genuine binary data
166
+ console.log("Fallback: Using binary data, size:", binaryData.byteLength);
163
167
 
164
- // Pass binary data directly to uploadPicture
168
+ // Store both display URL and binary data
165
169
  this.props.uploadPicture({
166
- data: binaryData,
167
- item_id: this.props.item_id
170
+ data: blobUrl,
171
+ // For display in img tag
172
+ binaryData: binaryData,
173
+ // Genuine JPEG binary stream
174
+ item_id: this.props.item_id,
175
+ isBlobUrl: true // Flag to track blob URLs for cleanup
168
176
  });
169
177
  };
170
178
  reader.readAsArrayBuffer(blob);
@@ -177,9 +185,9 @@ class DsBlob extends _react.Component {
177
185
  //-----------------------------------------------------------------------------------------------------------------------------------------
178
186
  storeBase64ImageToImg = e => {
179
187
  var image = document.createElement('img');
180
- image.onload = this.reduceBinaryImage;
181
- console.log("storeImageToImg:e.currentTarget.result = ", e.currentTarget.result);
182
- alert("storeImageToImg !");
188
+ image.onload = this.reduceBase64Image;
189
+ console.log("storeBase64ImageToImg:e.currentTarget.result = ", e.currentTarget.result);
190
+ alert("storeBase64ImageToImg !");
183
191
  image.src = e.currentTarget.result;
184
192
  };
185
193
 
@@ -231,12 +239,29 @@ class DsBlob extends _react.Component {
231
239
 
232
240
  //-----------------------------------------------------------------------------------------------------------------------------------------
233
241
  resetUpload = () => {
242
+ // Clean up any blob URLs before resetting
243
+ this.cleanupBlobUrl();
234
244
  this.props.uploadPicture({
235
245
  data: "",
236
246
  item_id: this.props.item_id
237
247
  });
238
248
  };
239
249
 
250
+ //-----------------------------------------------------------------------------------------------------------------------------------------
251
+ cleanupBlobUrl = () => {
252
+ // Revoke blob URL if it exists to prevent memory leaks
253
+ if (this.props.data && this.props.data.startsWith && this.props.data.startsWith('blob:')) {
254
+ URL.revokeObjectURL(this.props.data);
255
+ console.log("Blob URL cleaned up:", this.props.data);
256
+ }
257
+ };
258
+
259
+ //-----------------------------------------------------------------------------------------------------------------------------------------
260
+ componentWillUnmount() {
261
+ // Cleanup blob URLs when component unmounts
262
+ this.cleanupBlobUrl();
263
+ }
264
+
240
265
  //-----------------------------------------------------------------------------------------------------------------------------------------
241
266
  onInputChange = e => {
242
267
  const errs = [];
@@ -276,6 +301,41 @@ class DsBlob extends _react.Component {
276
301
  return errs.forEach(err => alert(err));
277
302
  }
278
303
  };
304
+
305
+ //-----------------------------------------------------------------------------------------------------------------------------------------
306
+ getImageSrc = () => {
307
+ if (!this.props.data) return "";
308
+
309
+ // Check if it's already a complete URL (cloud storage URL or blob URL)
310
+ if (this.props.data.startsWith('http://') || this.props.data.startsWith('https://') || this.props.data.startsWith('blob:')) {
311
+ return this.props.data;
312
+ }
313
+
314
+ // Check if it's already a data URL (base64)
315
+ if (this.props.data.startsWith('data:')) {
316
+ return this.props.data;
317
+ }
318
+
319
+ // Otherwise, treat as base64 and add appropriate prefix
320
+ const prefix = this.props.removebase64 ? "data:image/png;base64," : "";
321
+ return prefix + this.props.data;
322
+ };
323
+
324
+ //-----------------------------------------------------------------------------------------------------------------------------------------
325
+ getBinaryData = () => {
326
+ // Return the genuine JPEG binary stream (ArrayBuffer)
327
+ // This can be used by parent components for uploading or processing
328
+ return this.props.binaryData || null;
329
+ };
330
+
331
+ //-----------------------------------------------------------------------------------------------------------------------------------------
332
+ getImageType = () => {
333
+ // Return the type of image data being used
334
+ if (this.props.isCloudUrl) return 'cloud';
335
+ if (this.props.isBlobUrl) return 'blob';
336
+ if (this.props.data && this.props.data.startsWith('data:')) return 'base64';
337
+ return 'unknown';
338
+ };
279
339
  render() {
280
340
  const upload_picture_label = {
281
341
  cursor: "pointer"
@@ -337,7 +397,7 @@ class DsBlob extends _react.Component {
337
397
  }
338
398
  }, /*#__PURE__*/_react.default.createElement("img", {
339
399
  style: this.state.hover_image ? div_show_image_hover : div_show_image,
340
- src: this.props.removebase64 ? "data:image/png;base64" : "" + this.props.data,
400
+ src: this.getImageSrc(),
341
401
  className: this.props.pictureStyle
342
402
  }), !this.props.readOnly && /*#__PURE__*/_react.default.createElement("input", {
343
403
  style: this.state.hover_input ? div_show_image_hover_input : div_show_image_input,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datasync-blob",
3
- "version": "1.1.22",
3
+ "version": "1.1.23",
4
4
  "description": "Datasync Blob component",
5
5
  "main": "./dist/components/DsBlob.js",
6
6
  "files": [