Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
colorhook committed Jun 26, 2012
1 parent 93189b3 commit 8d5f77f
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 16 deletions.
22 changes: 21 additions & 1 deletion README.md
Expand Up @@ -25,6 +25,13 @@ smushit file1 file2 file3
smushit file1 file2 file3 -R
```

//smash file and save by new name
smushit image-file-name -o new-file-name


//change the default service. by default, `node-smushit` use the !Yahoo smushi.it service, you can create your owne smush service.
//`If you are in China, you will know how important to create your own smush.it service. fuck the GFW & POOR NETWORK SPEED.`

use it in node

```javascript
Expand All @@ -51,10 +58,23 @@ smushit.smushit('images-folder-path', {
},
onComplete: function(reports){

}
},
service: 'http://my-custom-domain-service/'
});
```

Changelog
------------
> v0.3.0
* add custom smushit serivce feature
* add global service config for CLI
* can save the smashed file by new name

> v0.2.0
* add callback while the image(s) smashed completed

> v0.1.0
* smash by file, filelist, directory



41 changes: 37 additions & 4 deletions cli.js
@@ -1,7 +1,9 @@
#!/usr/bin/env node

var smushit = require('./smushit')
, util = require('util');
, util = require('util')
, Persist = require('./lib/persist').Persist
, persist = new Persist(__dirname + '/persist.json');

var responses = {
error: function (message) {
Expand All @@ -24,6 +26,9 @@ var responses = {
' Traversing:',
' -R, --recursive scan directories recursively',
'',
' Output:',
' -o, --output the path to save',
'',
' Other:',
' -h, --help print this help page',
' --version print program version',
Expand All @@ -47,7 +52,7 @@ var responses = {

},
version: function () {
util.puts('smushit v0.1.0');
util.puts('smushit v0.3.0');
}
};

Expand All @@ -61,17 +66,45 @@ if (argv.help || argv.h) {
respond('help');
} else if (argv.version) {
respond('version');
} else if (argv._.length) {
} else if (argv.c || argv.config){
var s = argv.c || argv.config;

if(s === true){
persist.each(function(key, value){
console.log(' smushit config: %s = %s ', key, value);
});
return;
}
s = s.split('=');
var key = s[0],
value = s[1];

if(value == undefined){
console.log(' smushit config: %s = %s ', key, persist.getItem(key));
}else if(value === ''){
persist.removeItem(key);
console.log(' smushit delete config key: %s', key);
}else{
persist.setItem(key, value);
console.log(' smushit config: %s = %s ', key, persist.getItem(key));
}
}else if(argv._.length) {
var settings = {};

if (argv.R || argv.recursive) {
settings.recursive = true;
}

if (argv.v | argv.verbose) {
if (argv.v || argv.verbose) {
settings.verbose = true;
}

if(argv.o || argv.output){
settings.output = argv.o || argv.output;
}

settings.service = persist.getItem('service');

smushit.smushit(argv._, settings);

} else {
Expand Down
68 changes: 68 additions & 0 deletions lib/persist.js
@@ -0,0 +1,68 @@
var fs = require('fs');



var Persist = function(path){
this.path = path || 'persist.json';
this.load();
this.__defineGetter__('length', function(){
var c = 0;
for(var i in this.map){
c++;
}
return c;
});
}


Persist.prototype = {

constructor: Persist,

load: function(){
var content;
try{
content = fs.readFileSync(this.path, 'utf-8');
this.map = JSON.parse(content);
}catch(err){
this.map = {};
}
},

save: function(){
var content = JSON.stringify(this.map, null, 4);
fs.writeFileSync(this.path, content, 'utf-8');
},

getItem: function(key){
this.load();
return this.map[key];
},

setItem: function(key, value){
this.load();
this.map[key] = value;
this.save();
},

removeItem: function(key, value){
this.load();
delete this.map[key];
this.save();
},

clear: function(){
this.map = {};
this.save();
},


each : function(callback){
for(var i in this.map){
callback(i, this.map[i]);
}
}
};

exports.Persist = Persist;
exports.persist = new Persist();
20 changes: 12 additions & 8 deletions lib/smushit.js
@@ -1,12 +1,12 @@
var fs = require("fs"),
url = require("url"),
http = require("http");


var SMUSH_HOST = 'www.smushit.com',
SMUSH_HOST_PATH = "/ysmush.it/ws.php",
exports.SMUSH_SERVICE = 'http://www.smushit.com/ysmush.it/ws.php';

//构建上传文件的请求body
buildRequestBody = function(fullName, uploadIdentifier, params){
var buildRequestBody = function(fullName, uploadIdentifier, params){
var boundary = '------multipartformboundary' + (new Date).getTime();
var dashdash = '--';
var crlf = '\r\n';
Expand Down Expand Up @@ -61,10 +61,14 @@ var SMUSH_HOST = 'www.smushit.com',
}
}

var smushit = function(str, success, fail){
var options = {
host: SMUSH_HOST,
path: SMUSH_HOST_PATH
var smushit = function(str, success, fail, customService){
var seriveEndpoint = customService || exports.SMUSH_SERVICE,

serviceParsed = url.parse(seriveEndpoint),
options = {
host: serviceParsed.hostname,
port: serviceParsed.port,
path: serviceParsed.pathname
},
httpRequest,
onRequestCompleted = function(response) {
Expand All @@ -80,7 +84,7 @@ var smushit = function(str, success, fail){
if(str.match(/https?:\/\//)){
options.method = "GET";
options.path += "?img=" + encodeURIComponent(str);
httpRequest = http.get(options, onRequestCompleted);
httpRequest = http.get(options, onRequestCompleted, onR);
}else{
var postData = buildRequestBody(str, "files");
options.method = "POST";
Expand Down
5 changes: 4 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "node-smushit",
"version": "0.2.0",
"version": "0.3.0",
"author": "colorhook <colorhook@gmail.com>",

"repository": {
Expand All @@ -10,6 +10,9 @@
"dependencies": {
"optimist": ">= 0.2.6"
},
"devDependencies": {
"nodeunit": "~>0.6.4"
},
"keywords": ["image", "compressor", "compression"],
"directories" : { "lib" : "./lib" },
"bin" : { "smushit" : "./cli.js" },
Expand Down
3 changes: 3 additions & 0 deletions persist.json
@@ -0,0 +1,3 @@
{
"service": "http://www.smushit.com/ysmush.it/ws.php"
}
5 changes: 3 additions & 2 deletions smushit.js
Expand Up @@ -135,7 +135,8 @@ exports.smushit = function(inputs, settings){
}
reports.ok += 1;
log("item: " + item + " saving: " + response.percent + "%");
saveBinary(response.dest, item, function(e){

saveBinary(response.dest, settings.output || item, function(e){
if(e){
log("Fail to save image at: " + item);
}
Expand All @@ -144,6 +145,6 @@ exports.smushit = function(inputs, settings){
}, function(error){
log(error.message || error.msg);
onItemFinished(error, item);
});
}, settings.service);
});
};
88 changes: 88 additions & 0 deletions test/persist-test.js
@@ -0,0 +1,88 @@
var Persist = require('../lib/persist').Persist;
var nodeunit = require("nodeunit");

var persist;

exports['persist'] = nodeunit.testCase({

setUp: function(callback){
persist = new Persist();
callback();
},

tearDown: function(callback){
persist.clear();
callback();
},

"persist getItem": function(test){
test.equal(null, persist.getItem('a'));
test.done();
},

"persist setItem & removeItem": function(test){
persist.clear();
test.equal(null, persist.getItem('a'));
persist.setItem('a', 'A');
test.equal('A', persist.getItem('a'));
persist.removeItem('a');
test.equal(null, persist.getItem('a'));
test.done();
},

"persist clear": function(test){
test.equal(null, persist.getItem('a'));
persist.setItem('a', 'A');
persist.setItem('b', 'B');
persist.setItem('c', 'C');
test.equal('A', persist.getItem('a'));
test.equal('B', persist.getItem('b'));
test.equal('C', persist.getItem('c'));
persist.removeItem('a');
test.equal(null, persist.getItem('a'));
test.equal('B', persist.getItem('b'));
test.equal('C', persist.getItem('c'));
persist.clear();
test.equal(null, persist.getItem('b'));
test.equal(null, persist.getItem('c'));
test.done();
},

"persist length": function(test){
persist.clear();
test.equal(0, persist.length);
persist.setItem('a', 'A');
test.equal(1, persist.length);
persist.setItem('b', 'B');
persist.setItem('c', 'C');
test.equal(3, persist.length);
persist.removeItem('a');
test.equal(2, persist.length);
persist.clear();
test.equal(0, persist.length);
test.done();
},

"persist each": function(test){
persist.clear();
var m = {
'a': 'A',
'b': 'B',
'c': 'C'
}
persist.setItem('a', 'A');
persist.setItem('b', 'B');
persist.setItem('c', 'C');
persist.each(function(key, value){
test.equal(m[key], value);
delete m[key];
});
var mKeyCount = 0;
for(var i in m){
mKeyCount ++;
}
test.equal(0, mKeyCount);
test.done();
}

});

0 comments on commit 8d5f77f

Please sign in to comment.