commit 30c1b47d403a7a7dee3947fb874e1c648e87cc2b Author: Johannes J. Schmidt Date: Fri Dec 13 19:11:15 2013 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..8c86fc7 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,14 @@ +{ + "curly": true, + "eqeqeq": true, + "immed": true, + "latedef": true, + "newcap": true, + "noarg": true, + "sub": true, + "undef": true, + "unused": true, + "boss": true, + "eqnull": true, + "node": true +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..66c2d15 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +couchmagick +=========== +Run ImageMagicks `convert` on CouchDB documents. + +Installation +------------ +via npm: + +```bash +npm install couchmagick -g +``` + +Configuration +------------- +Add couchmagick to the `os_daemons`: +```ini +[os_daemons] +couchmagick = couchmagick +``` + +Now CouchDB takes care of the couchmagick process. + +```ini +[couchmagick] +user = mein-user +password = secure +dbs = photos, _users +filter = 'myapp/myfilter' +``` + +Contributing +------------ +Lint your code via `npm run jshint`. + +License +------- +Copyright (c) 2012-2013 Johannes J. Schmidt, null2 GmbH + +Licensed under the MIT license. diff --git a/bin/couchmagick b/bin/couchmagick new file mode 100755 index 0000000..7714cb3 --- /dev/null +++ b/bin/couchmagick @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require('..')(); diff --git a/index.js b/index.js new file mode 100644 index 0000000..1f784d1 --- /dev/null +++ b/index.js @@ -0,0 +1,159 @@ +/* couchmagick + * (c) 2013 Johannes J. Schmidt, null2 GmbH, Berlin + */ + +'use strict'; + +var pkg = require('./package.json'); +var couchdbWorker = require('couchdb-worker'); +var url = require('url'); + +// logging +function info(msg) { + process.stdout.write(JSON.stringify(["log", msg]) + '\n'); +} +function error(msg) { + process.stdout.write(JSON.stringify(["log", msg, {"level": "error"}]) + '\n'); +} +function debug(msg) { + process.stdout.write(JSON.stringify(["log", msg, {"level": "debug"}]) + '\n'); +} + +// configuration +// [httpd] +// {"bind_address": "127.0.0.1", "port": "5984"} +// [couchmagick] +// {"dbs": "myfotos"} +var config = {}; +function parseConfig(data) { + debug('parsing config: ' + data); + + try { + data = JSON.parse(data); + } catch(e) { + error('Error parsing config: ' + e); + } + + // [httpd] + if (data.port) { + config.port = data.port; + } + if (data.bind_address) { + config.bind_address = data.bind_address; + } + + // [couchmagick] + if (data.dbs) { + config.dbs = data.dbs.split(/,\s+/); + } + if (data.user) { + config.user = data.user; + } + if (data.password) { + config.password = data.password; + } + if (data.filter) { + config.filter = data.filter; + } + + return config; +} + +// process function +function convert(doc, db, next) { + info('processing doc ' + doc._id + '...'); + next(null); +} + +// run workers +function run(config) { + if (!config.bind_address) { + return; + } + if (!config.dbs) { + return; + } + + info('using config: ' + JSON.stringify(config)); + + var server = url.format({ + protocol: 'http', + hostname: config.bind_address, + port: config.port + }); + debug('using server: ' + server); + + var auth; + if (config.user && config.password) { + auth = { + user: config.user, + password: config.password + }; + } + + var followOptions = {}; + if (config.filter) { + followOptions.filter = config.filter; + } + + config.dbs.forEach(function(db) { + debug('starting worker for ' + db); + + var options = { + id: pkg.name, + db: { + url: url.resolve(server, db), + auth: auth + }, + follow: followOptions, + process: convert + }; + + debug('using worker options: ' + JSON.stringify(options)); + + try { + var worker = couchdbWorker(options); + + worker.on('error', function(err) { + error('Serious error: ' + err); + }); + + worker.on('worker:complete', function(doc) { + debug('Completed: ' + doc._id); + }); + + worker.on('worker:error', function(err, doc) { + info('Error processing ' + doc._id + ' :' + err); + }); + + worker.start(); + } catch(e) { + error('Error starting worker: ' + e); + } + }); +} + + +module.exports = function() { + process.stdin.on('data', function(data) { + if (typeof data !== 'string') { + data = data.toString(); + } + + data.replace(/\s+$/, '').split(/\n+/).forEach(parseConfig); + + run(config); + }); + + process.stdin.on('end', function () { + process.exit(0); + }); + + // restart on config change + process.stdout.write(JSON.stringify(["register", "httpd"]) + '\n'); + process.stdout.write(JSON.stringify(["register", pkg.name]) + '\n'); + + // get config + process.stdout.write(JSON.stringify(["get", "httpd"]) + '\n'); + process.stdout.write(JSON.stringify(["get", pkg.name]) + '\n'); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..d53ef2f --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "couchmagick", + "version": "0.0.1", + "description": "Run ImageMagicks `convert` on CouchDB documents.", + "main": "index.js", + "preferGlobal": true, + "bin": "bin/couchmagick", + "repository": { + "type": "git", + "url": "https://github.com/null2/couchmagick" + }, + "scripts": { + "jshint": "jshint -c .jshintrc index.js" + }, + "keywords": [ + "couchdb", + "worker", + "convert", + "imagemagick", + "image-processing", + "os_daemon" + ], + "author": "Johannes J. Schmidt", + "license": "MIT", + "bugs": { + "url": "https://github.com/null2/couchmagick/issues" + }, + "homepage": "https://github.com/null2/couchmagick", + "dependencies": { + "couchdb-worker": "~3.1.1" + }, + "devDependencies": { + "jshint": "~2.3.0" + } +}