From 3ccab90d67fef380150aee1f67c398105e2e31f5 Mon Sep 17 00:00:00 2001 From: Cyril Adelekan Date: Sun, 6 Aug 2017 22:56:02 +0100 Subject: [PATCH 1/2] Make it possible for Buffers to be create with a new property imageDataType in the Options of node-exif --- lib/exif/ExifImage.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/exif/ExifImage.js b/lib/exif/ExifImage.js index 75fc4be..95d6d6c 100644 --- a/lib/exif/ExifImage.js +++ b/lib/exif/ExifImage.js @@ -107,7 +107,21 @@ function ExifImage (options, callback) { // callback(new Error('You have to provide an image, it is pretty hard to extract Exif data from nothing...')); return; } - + + if (ops.hasOwnProperty('imageDataType')) { + switch (ops.imageDataType.toLowerCase()) { + case 'base64': + ops.image = ops.image.replace(/^data\:([^\;]+)\;base64,/gmi, ''); + ops.image = Buffer.alloc(ops.image.length, ops.image, "base64"); + break; + case 'arraybuffer': + ops.image = new Buffer(ops.image); + break; + default: + break; + } + } + if (typeof callback !== 'function') { throw new Error('You have to provide a callback function.'); } From c0bd2742e5c9e09c16beacab94d92c33d5e804bf Mon Sep 17 00:00:00 2001 From: Cyril Adelekan Date: Sun, 6 Aug 2017 22:58:00 +0100 Subject: [PATCH 2/2] Add base64ToArrayBuffer funtion for buffer creation --- lib/exif/ExifImage.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/exif/ExifImage.js b/lib/exif/ExifImage.js index 95d6d6c..bee398b 100644 --- a/lib/exif/ExifImage.js +++ b/lib/exif/ExifImage.js @@ -9,6 +9,17 @@ var debug = require('debug')('exif'); var DEFAULT_MAX_ENTRIES=128; +function base64ToArrayBuffer(base64) { + base64 = base64.replace(/^data\:([^\;]+)\;base64,/gmi, ''); + var binary = atob(base64); + var len = binary.length; + var view = new Uint8Array(new ArrayBuffer(len)); + for (var i = 0; i < len; i++) { + view[i] = binary.charCodeAt(i); + } + return view.buffer; +} + /** * Represents an image with Exif information. When instantiating it you have to * provide an image and a callback function which is called once all metadata @@ -111,8 +122,7 @@ function ExifImage (options, callback) { if (ops.hasOwnProperty('imageDataType')) { switch (ops.imageDataType.toLowerCase()) { case 'base64': - ops.image = ops.image.replace(/^data\:([^\;]+)\;base64,/gmi, ''); - ops.image = Buffer.alloc(ops.image.length, ops.image, "base64"); + ops.image = new Buffer(base64ToArrayBuffer(ops.image)); break; case 'arraybuffer': ops.image = new Buffer(ops.image);