-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_processor.js
66 lines (57 loc) · 1.8 KB
/
image_processor.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
var im = require('imagemagick')
, localCache = require('./local_cache')
;
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
};
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
var LOCAL_CACHE_TYPE = 'trash';
localCache.setMaxCacheTime(LOCAL_CACHE_TYPE, -1);
localCache.setMaxCacheCount(LOCAL_CACHE_TYPE, -1);
function runImagemagickProcessors(processors, i, srcData, callback){
console.info('runImagemagickProcessors: ');
if (i >= processors.length){
console.info('runImagemagickProcessors return srcData');
callback(null, srcData);
return;
}
var processor = processors[i];
console.info(processor);
var func = im[processor['function']];
if(typeof func != 'function')
throw "unsupport function for imagemagick: " + processor['function'];
var args = processor['arguments'];
args.srcData = srcData;
var tmpFileID = guid();
args.dstPath = localCache.path(LOCAL_CACHE_TYPE, tmpFileID);
// imagemagick
func.apply(im, [args, function(err, stdout, stderr){
if(err){
localCache.remove(LOCAL_CACHE_TYPE, tmpFileID);
callback(err);
console.info(err);
return;
}
localCache.read(LOCAL_CACHE_TYPE, tmpFileID, function(data){
if(data){
runImagemagickProcessors(processors, i+1, data, function(err, data){
callback(err, data);
localCache.remove(LOCAL_CACHE_TYPE, tmpFileID);
console.info('runImagemagickProcessors done');
});
}else{
callback('Unknown error', null);
console.info('runImagemagickProcessors Unknwon error');
localCache.remove(LOCAL_CACHE_TYPE, tmpFileID);
}
});
}]);
}
exports.processImage = function(processorsArray, srcData, callback){
runImagemagickProcessors(processorsArray, 0, srcData, callback);
};