-
Notifications
You must be signed in to change notification settings - Fork 12
/
smushit.js
149 lines (131 loc) · 3.19 KB
/
smushit.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var smushit = require('./lib/smushit')
, fs = require('fs')
, url = require('url')
, http = require('http')
, FileUtil = require('./lib/FileUtil.js');
var saveBinary = function(binaryUrl, path, callback){
var urlObj = url.parse(binaryUrl),
options = {
host: urlObj.host
, port: urlObj.port
, path: urlObj.pathname
}
var request = http.get(options, function(res){
var data = '';
res.setEncoding('binary')
res.on('data', function(chunk){
data += chunk;
})
res.on('end', function(){
fs.writeFile(path, data, 'binary', callback)
})
});
if(callback){
request.on("error", callback);
}
};
exports.smushit = function(inputs, settings){
var defaults = {
verbose: true,
inputs: [],
recursive: false,
verbose: true
};
settings = settings || {};
for (key in defaults) {
settings[key] = (typeof settings[key] !== 'undefined') ? settings[key] : defaults[key];
}
// Normalize inputs to array if just given a string
if(typeof inputs === 'string') {
inputs = [inputs];
}
var log = function(msg){
if(settings.verbose){
console.log("[smushit] " + msg);
}
};
var files = [];
inputs.forEach(function(item){
if(!fs.existsSync(item)){
log("no such file or directory: " + item);
return;
}
var stats = fs.statSync(item);
if(stats.isFile()){
if(files.indexOf(item) == -1){
files.push(item);
}
}else if(stats.isDirectory()){
FileUtil.list(item, {
recursive: settings.recursive,
excludeDirectory: true,
matchFunction: function(fileItem){
if(fileItem.name.match(/\.(png|gif|jpg|jpeg)/i)){
if(files.indexOf(fileItem.fullName) == -1){
files.push(fileItem.fullName);
}
return true;
}
return false;
}
});
}
});
var finished = 0,
reports = {
total: files.length,
finished: 0,
ok: 0,
items: []
},
onItemFinished = function(error, item, response){
if(settings.onItemComplete){
settings.onItemComplete(error, item, response);
}
if(++reports.finished >= files.length){
log("smushit completed: total " + reports.total + ", saving " + reports.ok );
if(settings.onComplete){
settings.onComplete(reports);
}
}
};
if(files.length == 0){
log("please specify image(s) need to smushit!");
if(settings.onComplete){
settings.onComplete(reports);
}
return;
};
files.forEach(function(item){
log("start smash " + item);
if(settings.onItemStart){
settings.onItemStart(item);
}
smushit.smushit(item, function(response){
reports.items.push(response);
try{
response = JSON.parse(response);
}catch(err){
log("item: " + item + " response:" + response);
onItemFinished(err, item, response);
return;
}
if(response.error){
log("item: " + item + " error: " + response.error);
onItemFinished(null, item, response);
return;
}
reports.ok += 1;
log("item: " + item + " saving: " + response.percent + "%");
saveBinary(response.dest, settings.output || item, function(e){
if(e){
log("Fail to save image at: " + item);
}
onItemFinished(e, item, response);
});
}, function(error){
log(error.message || error.msg);
onItemFinished(error, item);
}, settings.service);
});
};