datasync-blob 1.1.20 → 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,
@@ -148,24 +149,45 @@ class DsBlob extends _react.Component {
148
149
  context = canvas.getContext('2d');
149
150
  context.drawImage(imageSource, 0, 0, width, height);
150
151
 
151
- //Jpeg conversion
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
- let canvasData = canvas.toDataURL('image/jpg', jpegCompressionRatio); //Jpeg conversion
154
+ canvas.toBlob(async blob => {
155
+ if (blob) {
156
+ console.log("Binary blob created, size:", blob.size);
157
+ if (this.debugging) console.log("jpegCompressionRatio->", jpegCompressionRatio, " blob.size = ", blob.size);
154
158
 
155
- console.log("canvasData = ", canvasData);
156
- if (this.debugging) console.log("jpegCompressionRatio->", jpegCompressionRatio, " canvasData.length = ", canvasData.length);
157
- this.props.uploadPicture({
158
- data: canvasData ? canvasData : "",
159
- item_id: this.props.item_id
160
- });
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
163
+ const reader = new FileReader();
164
+ reader.onload = () => {
165
+ const binaryData = reader.result; // ArrayBuffer - genuine binary data
166
+ console.log("Fallback: Using binary data, size:", binaryData.byteLength);
167
+
168
+ // Store both display URL and binary data
169
+ this.props.uploadPicture({
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
176
+ });
177
+ };
178
+ reader.readAsArrayBuffer(blob);
179
+ } else {
180
+ console.error("Failed to create blob from canvas");
181
+ }
182
+ }, 'image/jpeg', jpegCompressionRatio);
161
183
  };
162
184
 
163
185
  //-----------------------------------------------------------------------------------------------------------------------------------------
164
186
  storeBase64ImageToImg = e => {
165
187
  var image = document.createElement('img');
166
- image.onload = this.reduceBinaryImage;
167
- console.log("storeImageToImg:e.currentTarget.result = ", e.currentTarget.result);
168
- alert("storeImageToImg !");
188
+ image.onload = this.reduceBase64Image;
189
+ console.log("storeBase64ImageToImg:e.currentTarget.result = ", e.currentTarget.result);
190
+ alert("storeBase64ImageToImg !");
169
191
  image.src = e.currentTarget.result;
170
192
  };
171
193
 
@@ -217,12 +239,29 @@ class DsBlob extends _react.Component {
217
239
 
218
240
  //-----------------------------------------------------------------------------------------------------------------------------------------
219
241
  resetUpload = () => {
242
+ // Clean up any blob URLs before resetting
243
+ this.cleanupBlobUrl();
220
244
  this.props.uploadPicture({
221
245
  data: "",
222
246
  item_id: this.props.item_id
223
247
  });
224
248
  };
225
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
+
226
265
  //-----------------------------------------------------------------------------------------------------------------------------------------
227
266
  onInputChange = e => {
228
267
  const errs = [];
@@ -262,6 +301,41 @@ class DsBlob extends _react.Component {
262
301
  return errs.forEach(err => alert(err));
263
302
  }
264
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
+ };
265
339
  render() {
266
340
  const upload_picture_label = {
267
341
  cursor: "pointer"
@@ -323,7 +397,7 @@ class DsBlob extends _react.Component {
323
397
  }
324
398
  }, /*#__PURE__*/_react.default.createElement("img", {
325
399
  style: this.state.hover_image ? div_show_image_hover : div_show_image,
326
- src: this.props.removebase64 ? "data:image/png;base64" : "" + this.props.data,
400
+ src: this.getImageSrc(),
327
401
  className: this.props.pictureStyle
328
402
  }), !this.props.readOnly && /*#__PURE__*/_react.default.createElement("input", {
329
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.20",
3
+ "version": "1.1.23",
4
4
  "description": "Datasync Blob component",
5
5
  "main": "./dist/components/DsBlob.js",
6
6
  "files": [