Last active
November 24, 2017 21:08
-
-
Save johnelliott/14245b95e4465b589adfeb6da9cdb54e to your computer and use it in GitHub Desktop.
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
const debug = require('debug')('hub:etl') | |
const path = require('path') | |
const Sqlite3 = require('sqlite3').verbose() | |
const exiftool = require('node-exiftool') | |
const watch4jpegs = require('./lib/watch.js') | |
require('dotenv').config() | |
const STORAGE_PATH = process.env.STORAGE_PATH | |
if (!STORAGE_PATH) { | |
console.error(new Error('no STORAGE_PATH')) | |
process.exit(1) | |
} | |
const EXIFTOOL_PATH = process.env.EXIFTOOL_PATH | |
if (!EXIFTOOL_PATH) { | |
console.error(new Error('no EXIFTOOL_PATH')) | |
process.exit(1) | |
} | |
const ep = new exiftool.ExiftoolProcess(EXIFTOOL_PATH) | |
const createImages = `CREATE TABLE image ( | |
id INTEGER PRIMARY KEY, | |
file_name NOT NULL DEFAULT '', | |
date_time_created datetime NOT NULL, | |
full_path NOT NULL DEFAULT '', | |
thumbnail TEXT | |
);` | |
const db = new Sqlite3.Database(path.join(__dirname, 'cam.db'), (err, result) => { | |
if (err) { | |
return debug('db open error', err) | |
} | |
debug('db open') | |
return db.run(createImages, (err, result) => { | |
if (err && err.message.match(/table image already exists/)) { | |
debug('image table exists') | |
} else if (err) { | |
return debug('db create iamge table error', err) | |
} | |
}) | |
}) | |
function exifDateTimeToSQLite3Time (time) { | |
return [time.split(' ')[0].split(':').join('-'), time.split(' ')[1]].join(' ') | |
} | |
function addImageToDatabase ({ fileName, dateTimeOriginal, fullPath, thumbnail }) { | |
db.run( | |
`INSERT INTO image ( | |
'file_name', | |
'date_time_created', | |
'full_path', | |
'thumbnail' | |
) values ( | |
'${fileName}', | |
'${dateTimeOriginal}', | |
'${fullPath}', | |
'${thumbnail}' | |
);` | |
) | |
} | |
function getImgData (path) { | |
ep | |
.open() | |
.then(pid => debug('Started exiftool process %s', pid)) | |
.then(() => ep.readMetadata(path, ['b', 'FileName', 'ThumbnailImage', 'DateTimeOriginal'])) | |
.then(result => { | |
// create db entry | |
debug(result.data) | |
const model = { | |
fileName: result.data[0].FileName, | |
dateTimeOriginal: exifDateTimeToSQLite3Time(result.data[0].DateTimeOriginal), | |
fullPath: path, | |
thumbnail: result.data[0].ThumbnailImage | |
} | |
debug('model to add', model) | |
return addImageToDatabase(model) | |
}) | |
.then(() => ep.close()) | |
.catch(debug) | |
} | |
if (require.main === module) { | |
watch4jpegs(STORAGE_PATH, getImgData) | |
} | |
exports.addImageToDatabase = addImageToDatabase |
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
const debug = require('debug')('hub:watch') | |
const watch = require('node-watch') | |
module.exports = function watch4jpegs (dir, cb) { | |
watch(dir, { recursive: true }, (evt, name) => { | |
if (evt === 'update' && new RegExp(/jpg$/, 'i').test(name)) { | |
debug('Saw new image', name) | |
cb(name) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment