Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#Exiv2
# Exiv2

Exiv2 is a native c++ extension for [io.js](https://iojs.org/en/index.html) and
[node.js](https://nodejs.org/) that provides support for reading and writing
image metadata via the [Exiv2 library](http://www.exiv2.org).
Exiv2 is a native C++ extension for [node.js](https://nodejs.org) that provides
support for reading and writing image metadata via the [Exiv2 library](http://www.exiv2.org).

## Dependencies

Expand All @@ -25,6 +24,10 @@ You'll also need to install pkg-config to help locate the library and headers.

brew install pkg-config exiv2

### FreeBSD

pkg install pkgconf exiv2

### Other systems

See the [Exiv2 download page](http://www.exiv2.org/download.html) for more
Expand Down Expand Up @@ -53,6 +56,13 @@ the tests:
console.log("DateTimeOriginal: " + tags["Exif.Photo.DateTimeOriginal"]);
});

var fs = require('exiv2');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be var fs = require("fs");


ex.getImageTags(fs.readFileSync('./photo.jpg'), function(err, tags) {
console.log("DateTime: " + tags["Exif.Image.DateTime"]);
console.log("DateTimeOriginal: " + tags["Exif.Photo.DateTimeOriginal"]);
});

### Load preview images:

var ex = require('exiv2')
Expand Down
74 changes: 58 additions & 16 deletions exiv2node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ typedef std::map<std::string, std::string> tag_map_t;
class Exiv2Worker : public Nan::AsyncWorker {
public:
Exiv2Worker(Nan::Callback *callback, const std::string fileName)
: Nan::AsyncWorker(callback), fileName(fileName) {}
: Nan::AsyncWorker(callback), isBuf(false), fileName(fileName) {}
Exiv2Worker(Nan::Callback *callback, const char* buf, const long len)
: Nan::AsyncWorker(callback), isBuf(true), buf(reinterpret_cast<const Exiv2::byte*>(buf)), bufLen(len) {}
~Exiv2Worker() {}

protected:
const std::string fileName;
const bool isBuf;
const Exiv2::byte* buf = nullptr;
const long bufLen = -1;
const std::string fileName = "";
std::string exifException;
Exiv2::Image::AutoPtr image() {
return this->isBuf
? Exiv2::ImageFactory::open(this->buf, this->bufLen)
: Exiv2::ImageFactory::open(this->fileName);
}
};

// - - -
Expand All @@ -34,6 +44,8 @@ class GetTagsWorker : public Exiv2Worker {
public:
GetTagsWorker(Nan::Callback *callback, const std::string fileName)
: Exiv2Worker(callback, fileName) {}
GetTagsWorker(Nan::Callback *callback, const char* buf, const long len)
: Exiv2Worker(callback, buf, len) {}
~GetTagsWorker() {}

// Should become protected...
Expand All @@ -43,7 +55,7 @@ class GetTagsWorker : public Exiv2Worker {
// structures here.
void Execute () {
try {
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(fileName);
Exiv2::Image::AutoPtr image = this->image();
assert(image.get() != 0);
image->readMetadata();

Expand Down Expand Up @@ -106,12 +118,16 @@ NAN_METHOD(GetImageTags) {

/* Usage arguments */
if (info.Length() <= 1 || !info[1]->IsFunction())
return Nan::ThrowTypeError("Usage: <filename> <callback function>");
return Nan::ThrowTypeError("Usage: <filename/buffer> <callback function>");

Nan::Callback *callback = new Nan::Callback(info[1].As<Function>());
std::string filename = std::string(*Nan::Utf8String(info[0]));

Nan::AsyncQueueWorker(new GetTagsWorker(callback, filename));
if (info[0]->IsString()) {
std::string filename = std::string(*Nan::Utf8String(info[0]));
Nan::AsyncQueueWorker(new GetTagsWorker(callback, filename));
} else {
Local<Object> buf = info[0]->ToObject();
Nan::AsyncQueueWorker(new GetTagsWorker(callback, Buffer::Data(buf), Buffer::Length(buf)));
}
return;
}

Expand All @@ -121,6 +137,8 @@ class SetTagsWorker : public Exiv2Worker {
public:
SetTagsWorker(Nan::Callback *callback, const std::string fileName)
: Exiv2Worker(callback, fileName) {}
SetTagsWorker(Nan::Callback *callback, const char* buf, const long len)
: Exiv2Worker(callback, buf, len) {}
~SetTagsWorker() {}

// Should become protected...
Expand All @@ -130,7 +148,7 @@ class SetTagsWorker : public Exiv2Worker {
// structures here.
void Execute () {
try {
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(fileName);
Exiv2::Image::AutoPtr image = this->image();
assert(image.get() != 0);

image->readMetadata();
Expand Down Expand Up @@ -187,7 +205,14 @@ NAN_METHOD(SetImageTags) {
Nan::Callback *callback = new Nan::Callback(info[2].As<Function>());
std::string filename = std::string(*Nan::Utf8String(info[0]));

SetTagsWorker *worker = new SetTagsWorker(callback, filename);
SetTagsWorker *worker;
if (info[0]->IsString()) {
std::string filename = std::string(*Nan::Utf8String(info[0]));
worker = new SetTagsWorker(callback, filename);
} else {
Local<Object> buf = info[0]->ToObject();
worker = new SetTagsWorker(callback, Buffer::Data(buf), Buffer::Length(buf));
}

Local<Object> tags = Local<Object>::Cast(info[1]);
Local<Array> keys = Nan::GetOwnPropertyNames(tags).ToLocalChecked();
Expand All @@ -209,6 +234,8 @@ class DeleteTagsWorker : public Exiv2Worker {
public:
DeleteTagsWorker(Nan::Callback *callback, const std::string fileName)
: Exiv2Worker(callback, fileName) {}
DeleteTagsWorker(Nan::Callback *callback, const char* buf, const long len)
: Exiv2Worker(callback, buf, len) {}
~DeleteTagsWorker() {}

// Should become protected...
Expand All @@ -218,7 +245,7 @@ class DeleteTagsWorker : public Exiv2Worker {
// structures here.
void Execute () {
try {
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(fileName);
Exiv2::Image::AutoPtr image = this->image();
assert(image.get() != 0);

image->readMetadata();
Expand Down Expand Up @@ -281,7 +308,14 @@ NAN_METHOD(DeleteImageTags) {
Nan::Callback *callback = new Nan::Callback(info[2].As<Function>());
std::string filename = std::string(*Nan::Utf8String(info[0]));

DeleteTagsWorker *worker = new DeleteTagsWorker(callback, filename);
DeleteTagsWorker *worker;
if (info[0]->IsString()) {
std::string filename = std::string(*Nan::Utf8String(info[0]));
worker = new DeleteTagsWorker(callback, filename);
} else {
Local<Object> buf = info[0]->ToObject();
worker = new DeleteTagsWorker(callback, Buffer::Data(buf), Buffer::Length(buf));
}

Local<Array> keys = Local<Array>::Cast(info[1]);
for (unsigned i = 0; i < keys->Length(); i++) {
Expand All @@ -299,13 +333,15 @@ class GetPreviewsWorker : public Exiv2Worker {
public:
GetPreviewsWorker(Nan::Callback *callback, const std::string fileName)
: Exiv2Worker(callback, fileName) {}
GetPreviewsWorker(Nan::Callback *callback, const char* buf, const long len)
: Exiv2Worker(callback, buf, len) {}
~GetPreviewsWorker() {}

// Executed inside the worker-thread. Not safe to access V8, or V8 data
// structures here.
void Execute () {
try {
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(fileName);
Exiv2::Image::AutoPtr image = this->image();
assert(image.get() != 0);
image->readMetadata();

Expand All @@ -316,8 +352,8 @@ class GetPreviewsWorker : public Exiv2Worker {
for (Exiv2::PreviewPropertiesList::iterator pos = list.begin(); pos != list.end(); pos++) {
Exiv2::PreviewImage image = manager.getPreviewImage(*pos);

previews.push_back(Preview(pos->mimeType_, pos->height_,
pos->width_, (char*) image.pData(), pos->size_));
previews.push_back(Preview(image.mimeType(), image.height(),
image.width(), (char*) image.pData(), image.size()));
}
} catch (std::exception& e) {
exifException.append(e.what());
Expand Down Expand Up @@ -386,12 +422,18 @@ NAN_METHOD(GetImagePreviews) {

/* Usage arguments */
if (info.Length() <= 1 || !info[1]->IsFunction())
return Nan::ThrowTypeError("Usage: <filename> <callback function>");
return Nan::ThrowTypeError("Usage: <filename/buffer> <callback function>");

Nan::Callback *callback = new Nan::Callback(info[1].As<Function>());
std::string filename = std::string(*Nan::Utf8String(info[0]));

Nan::AsyncQueueWorker(new GetPreviewsWorker(callback, filename));
if (info[0]->IsString()) {
std::string filename = std::string(*Nan::Utf8String(info[0]));
Nan::AsyncQueueWorker(new GetPreviewsWorker(callback, filename));
} else {
Local<Object> buf = info[0]->ToObject();
Nan::AsyncQueueWorker(new GetPreviewsWorker(callback, Buffer::Data(buf), Buffer::Length(buf)));
}
return;
}

Expand Down
55 changes: 53 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ describe('exiv2', function(){
})
});

it("should callback with image's tags when called with a buffer", function(done) {
exiv.getImageTags(fs.readFileSync(dir + '/books.jpg'), function(err, tags) {
should.not.exist(err);

tags.should.have.property('Exif.Image.DateTime', '2008:12:16 21:28:36');
tags.should.have.property('Exif.Photo.DateTimeOriginal', '2008:12:16 21:28:36');
done();
})
});

it('should callback with null on untagged file', function(done) {
exiv.getImageTags(dir + '/damien.jpg', function(err, tags) {
should.not.exist(err);
Expand All @@ -25,7 +35,15 @@ describe('exiv2', function(){
})
});

it('should throw if no file path is provided', function() {
it('should callback with null on untagged buffer', function(done) {
exiv.getImageTags(fs.readFileSync(dir + '/damien.jpg'), function(err, tags) {
should.not.exist(err);
should.not.exist(tags);
done();
})
});

it('should throw if no file path or buffer is provided', function() {
(function(){
exiv.getImageTags()
}).should.throw();
Expand All @@ -44,6 +62,15 @@ describe('exiv2', function(){
done();
});
});

it('should report an error on an empty buffer', function(done) {
exiv.getImageTags(new Buffer(''), function(err, tags) {
should.exist(err);
should.not.exist(tags);
done();
});
});

});

describe('.setImageTags()', function(){
Expand Down Expand Up @@ -75,6 +102,7 @@ describe('exiv2', function(){
fs.unlink(temp, done);
});


it('should throw if no file path is provided', function() {
(function(){
exiv.setImageTags()
Expand Down Expand Up @@ -134,7 +162,21 @@ describe('exiv2', function(){
});
});

it('should callback with an empty array for files no previews', function(done) {
it("should callback with image's previews when called with a buffer", function(done) {
exiv.getImagePreviews(fs.readFileSync(dir + '/books.jpg'), function(err, previews) {
should.not.exist(err);
previews.should.be.an.instanceof(Array);
previews.should.have.lengthOf(1);
previews[0].should.have.property('mimeType', 'image/jpeg');
previews[0].should.have.property('height', 120);
previews[0].should.have.property('width', 160);
previews[0].should.have.property('data').with.instanceof(Buffer);
previews[0].data.should.have.property('length', 6071);
done();
});
});

it('should callback with an empty array for files with no previews', function(done) {
exiv.getImagePreviews(dir + '/damien.jpg', function(err, previews) {
should.not.exist(err);
previews.should.be.an.instanceof(Array);
Expand All @@ -143,6 +185,15 @@ describe('exiv2', function(){
})
});

it('should callback with an empty array for buffers with no previews', function(done) {
exiv.getImagePreviews(fs.readFileSync(dir + '/damien.jpg'), function(err, previews) {
should.not.exist(err);
previews.should.be.an.instanceof(Array);
previews.should.have.lengthOf(0);
done();
})
});


it('should throw if no file path is provided', function() {
(function(){
Expand Down