gomfunkel / node-exif

A node.js library to extract Exif metadata from images.
MIT License
581 stars 104 forks source link

callback called twice in ExifImage.prototype.processImage #16

Open alex-netkachov opened 11 years ago

alex-netkachov commented 11 years ago
112     } catch (error) {
113     callback(error);
114 }
115
116 callback(new Error('No Exif segment found in the given image.'));

When "catch" gets the exception it calls callback but then the same callback is called with a new error. The exception should be either re-thrown or "return" should terminate the function.

Regards, Alex

alex-netkachov commented 11 years ago

I refactored this method as follows:

ExifImage.prototype.processImage = function (data, callback) {

    var self = this;
    var offset = 0;

    if (data[offset++] == 0xFF && data[offset++] == 0xD8) {
        self.imageType = 'JPEG';
    } else {
        callback(new Error('The given image is not a JPEG and thus unsupported right now.'));
        return;
    }

    var error = null,
        exifData = null;

    try {
        while (offset < data.length) {
            if (0xFF !== data[offset++]) {
                error = new Error('Invalid marker found at offset ' +
                    (--offset) + '. Expected 0xFF but found 0x' +
                    data[offset].toString(16).toUpperCase() + '.');
                break;
            }

            if (0xE1 === data[offset++]) {
                exifData = self.extractExifData(data, offset + 2,
                    data.getShort(offset, true) - 2);
                break;
            } else {
                offset += data.getShort(offset, true);
            }
        }
    } catch (e) {
        error = e;
    }

    if (null !== error) {
        return callback(error);
    }
    if (null === exifData) {
        return callback(new Error('No Exif segment found in the given image.'));
    }

    callback(null, exifData);
};

so the exception handling is better now.

gomfunkel commented 10 years ago

Version 0.3.6 was published to npm a couple of minutes ago. Can you please check if the latest version solves your problem and leave a short feedback here? Thanks!