feature(pack) add: gzip, gunzip

This commit is contained in:
coderaiser 2014-06-20 04:21:25 -04:00
parent 1f533b9eeb
commit 797446b6c0
4 changed files with 42 additions and 62 deletions

View file

@ -53,36 +53,6 @@
}
};
exports.write = function(path, data, option, callback) {
var optionsPipe,
type = Util.getType(data);
Util.checkArgs(arguments, ['path', 'data', 'callback']);
if (!callback)
callback = option;
if (!type)
type = '';
else if (type instanceof Buffer)
type = 'string';
switch(type) {
case 'string':
fs.writeFile(path, data, callback);
break;
case 'object':
optionsPipe = {
gunzip : option === 'unzip',
gzip : option === 'zip'
};
pipe.create(data, path, optionsPipe, callback);
break;
}
};
exports.delete = function(path, callback) {
if (rimraf)
rimraf(path, callback);
@ -110,35 +80,17 @@
});
};
exports.copy = function(from, to, type, callback) {
var options;
exports.copy = function(from, to, callback) {
Util.checkArgs(arguments, ['from', 'to', 'callback']);
if (!callback) {
callback = type;
type = null;
}
switch(type) {
default:
(ncp || pipe.create)(from, to, callback);
break;
case 'zip':
options = {
gzip : true
};
pipe.create(from, to, function(error) {
var isDir = error && error.code === 'EISDIR';
pipe.create(from, to, options, callback);
break;
case 'unzip':
options = {
gunzip : true
};
pipe.create(from, to, options, callback);
break;
}
if (isDir)
ncp(from, to, callback);
else
callback(error);
});
};
function tryRequire(name) {

22
lib/server/pack.js Normal file
View file

@ -0,0 +1,22 @@
(function() {
'use strict';
var DIR = './',
pipe = require(DIR + 'pipe');
exports.gzip = function(from, to, callback) {
var options = {
gzip : true
};
pipe.create(from, to, options, callback);
};
exports.gunzip = function(from, to, callback) {
var options = {
gunzip : true
};
pipe.create(from, to, options, callback);
};
})();

View file

@ -34,7 +34,10 @@
name:'api.json'
}),
flop = require('./flop'),
DIR = './',
flop = require(DIR + 'flop'),
pack = require(DIR + 'pack'),
NOT_LOG = true;
@ -314,7 +317,7 @@
from = files.from;
to = files.to || from + '.zip';
flop.copy(from, to, 'zip', function(error) {
pack.gzip(from, to, function(error) {
var name = path.basename(files.from);
if (error)
@ -332,7 +335,7 @@
from = files.from;
to = files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']);
flop.copy(from, to, 'unzip', function(error) {
pack.gunzip(from, to, function(error) {
var name = path.basename(files.from);
if (error)

View file

@ -8,8 +8,11 @@
CloudFunc = main.cloudfunc,
DIR = '../../../',
DIR_SERVER = DIR + 'server/',
flop = require(DIR_SERVER + 'flop'),
Util = require(DIR + 'util'),
flop = require(DIR_SERVER + 'flop'),
pack = require(DIR_SERVER + 'pack'),
pipe = require(DIR_SERVER + 'pipe');
exports.onPut = onPut;
@ -36,7 +39,7 @@
break;
default:
flop.write(name, readStream, 'unzip', function(error) {
pack.gunzip(readStream, path, function(error) {
var msg;
if (!error)