-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit. I suck at this. So this is like all of it.
- Loading branch information
0 parents
commit 40c7dad
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
Exift | |||
----- | |||
|
|||
Reads EXIF-data from images and outputs it as JSON. Uses `exiftool`. | |||
|
|||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,122 @@ | |||
// | |||
// # Exift | |||
// | |||
// Read EXIF data. | |||
// | |||
|
|||
// | |||
// ## Constructor | |||
// | |||
// Creates a new Exift object. | |||
// | |||
// If `exiftool` is not in your path do: | |||
// | |||
// var exif = new Exift(); | |||
// exif.exiftool = '/path/to/exiftool'; | |||
// | |||
var Exift = module.exports = function () { | |||
/* Get dependencies */ | |||
this.lib = {}; | |||
this.lib.stat = require('fs').stat; | |||
this.lib.watch = require('fs').watchFile; | |||
this.lib.spawn = require('child_process').spawn; | |||
|
|||
/* Change this if exiftool is not in your path. */ | |||
this.exiftool = 'exiftool'; | |||
}; | |||
|
|||
// | |||
// ### Extends EventEmitter | |||
// | |||
require('util').inherits(Exift, require('events').EventEmitter); | |||
|
|||
|
|||
// | |||
// ## Read EXIF | |||
// | |||
// Tries to read data from the given path. If successfull the callback | |||
// will recieve the read data. | |||
// | |||
// **parameters** | |||
// | |||
// * path | |||
// | |||
// Path to the file/directory to read. | |||
// | |||
// * callback | |||
// | |||
// A function that will be called when data is read. Follows the Node.js | |||
// standard with an error argument first and result second. | |||
// | |||
Exift.prototype.readData = function (path, fn) { | |||
var self = this; | |||
self.lib.stat(path, function (err, stat) { | |||
if (err) { | |||
fn(err); | |||
} | |||
else { | |||
var et = self.lib.spawn(self.exiftool, ['-j', '-sort', path]); | |||
var exif = ''; | |||
var err = ''; | |||
var hasErr = false; | |||
|
|||
et.stdout.on('data', function (data) { | |||
exif += data; | |||
}); | |||
|
|||
et.stderr.on('data', function (data) { | |||
err += data; | |||
hasErr = true; | |||
}); | |||
|
|||
et.on('exit', function (code) { | |||
if (hasErr && code !== 0) { | |||
if (err.length === 0) { | |||
err = 'Exiftool exited with code: ' + code; | |||
} | |||
fn(new Error(err)); | |||
} | |||
else { | |||
var json = exif.toString(); | |||
fn(null, JSON.parse(json)); | |||
} | |||
}); | |||
} | |||
}); | |||
}; | |||
|
|||
// | |||
// ## Watch a path | |||
// | |||
// Watches the given path for changes and re-reads exif data. | |||
// | |||
// **parameters** | |||
// | |||
// * path | |||
// | |||
// The path to watch. | |||
// | |||
// **events** | |||
// | |||
// * data | |||
// | |||
// Emitted every time new data is parsed. | |||
// | |||
// * error | |||
// | |||
// Emitted when readData returns an error. | |||
// | |||
Exift.prototype.watch = function (path) { | |||
var self = this; | |||
this.lib.watch(path, function (e, filename) { | |||
self.readData(path, function (err, data) { | |||
if (err) { | |||
self.emit('error', err); | |||
} | |||
else { | |||
self.emit('data', data); | |||
} | |||
}); | |||
}); | |||
} | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
{ "name": "exift" | |||
, "version": "0.0.1" | |||
, "author": "Simon Ljungberg <hi@iamsim.me>" | |||
, "devDependencies": | |||
{ "docco": "" | |||
, "mocha": "" | |||
} } |