-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
108 lines (92 loc) · 3.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Worker Convert
var request = require("request");
var _ = require("underscore");
var path = require('path');
var fs = require('fs');
var util = require('util');
var spawn = require('child_process').spawn;
var Worker = require("couchdb-worker").attachments;
function resize(doc, name, version, options, done) {
options || (options = {});
options.format || (options.format = 'jpg');
options.arguments || (options.arguments = []);
var attachments = doc._attachments || {},
url = this.server
+ '/' + encodeURIComponent(this.db)
+ '/' + encodeURIComponent(doc._id)
+ '/' + encodeURIComponent(name),
basename = name.replace(/\..*$/, ''),
prefix = '/tmp/' + encodeURIComponent(doc._id) + '-' + version + '-' + basename.replace('/', '-') + '-',
suffix = '.' + options.format,
args = ['-'].concat(options.arguments).concat([prefix + '%04d' + suffix]);
convert = spawn('convert', args);
convert.stderr.pipe(process.stderr);
convert.on('exit', (function(code) {
var i = 0, // convert starts with 0
attachments = {},
filename;
if (code !== 0) {
return done(code);
}
while (path.existsSync(prefix + String('0000' + i).slice(-4) + suffix)) {
filename = prefix + String('0000' + i).slice(-4) + suffix;
attachments[version + '/' + basename + String('0000' + i).slice(-4) + suffix] = {
content_type: 'image/' + options.format,
data: fs.readFileSync(filename).toString('base64')
};
fs.unlinkSync(filename);
i++;
}
done(code, attachments);
}).bind(this));
// request image and send it to imagemagick
request(url).pipe(convert.stdin);
}
var config = {
name: 'convert',
server: process.env.COUCH_SERVER || "http://127.0.0.1:5984",
defaults: {
formats: ['jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff', 'bmp', 'svg'],
versions: {
thumbnail: {
format: 'jpg',
arguments: [
'-thumbnail', '135x135',
'-colorspace', 'sRGB'
]
}
}
},
processor: {
check: function(doc, name) {
var folder = name.split('/', 1)[0];
// ignore own folders by version name
return !_.any(_.keys(this.config.versions), function(version) { return version === folder; })
// only process formats we know
&& this.config.formats.indexOf(name.toLowerCase().replace(/^.*\.([^\.]+)$/, '$1')) > -1;
},
process: function(doc, name, done) {
var cnt = _.size(this.config.versions);
_.each(this.config.versions, function(options, version) {
var attachments = doc._attachments || {};
this._log('render ' + doc._id + '/' + version + '/' + name);
resize.call(this, doc, name, version, options, function(code, attachment) {
if (code !== 0) {
console.warn("error in `convert`")
this._log('error ' + doc._id + '/' + version + '/' + name);
} else {
_.extend(attachments, attachment);
this._log('done ' + doc._id + '/' + version + '/' + name);
}
cnt--;
if (cnt === 0) done(null, { _attachments: attachments });
}.bind(this));
}, this);
}
}
};
if (process.env.COUCH_DB) {
new Worker(config, process.env.COUCH_DB);
} else {
console.error('I need the environment variable COUCH_DB');
}