feature(flop) add write

This commit is contained in:
coderaiser 2014-06-19 09:58:08 -04:00
parent 63cf7b8387
commit 9a6626aca6
4 changed files with 65 additions and 27 deletions

View file

@ -96,8 +96,32 @@
}
};
exports.write = function() {
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 = '';
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) {
@ -127,8 +151,35 @@
});
};
exports.cp = function(path, callback) {
(ncp || pipe.create)(path, callback);
exports.cp = function(from, to, type, callback) {
var options;
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, options, callback);
break;
case 'unzip':
options = {
gunzip : true
};
pipe.create(from, to, options, callback);
break;
}
};
function tryRequire(name) {

View file

@ -251,7 +251,7 @@
* @param pParams {command, method, body, requrest, response}
*/
function onPUT(params) {
var p, cmd, files, name, json, config, data, read, write, options,
var p, cmd, files, name, json, config, data, from, to, options,
ret = main.checkParams(params, ['body']);
if (ret) {
@ -320,14 +320,10 @@
if (!files.from) {
sendError(params, p.data);
} else {
read = files.from,
write = files.to || files.from + '.zip';
from = files.from;
to = files.to || from + '.zip';
options = {
gzip: true
};
pipe.create(read, write, options, function(error) {
flop.cp(from, to, 'zip', function(error) {
var name = path.basename(files.from);
if (error)
@ -342,14 +338,11 @@
if (!files.from) {
sendError(params, p.data);
} else {
read = files.from;
write = files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']);
options = {
gunzip: true
},
from = files.from;
to = files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']);
pipe.create(read, write, options, function(error) {
var name = path.basename(read);
flop.cp(from, to, 'unzip', function(error) {
var name = path.basename(files.from);
if (error)
sendError(params, error);

View file

@ -1,8 +1,7 @@
(function(){
'use strict';
var
DIR = '../../../',
var DIR = '../../../',
DIR_SERVER = DIR + 'server/',
Util = require(DIR + 'util'),
flop = require(DIR_SERVER + 'flop');

View file

@ -15,8 +15,7 @@
exports.onPut = onPut;
function onPut(name, query, readStream, callback) {
var options,
func = Util.exec.ret(callback),
var func = Util.exec.ret(callback),
baseName = path.basename(name);
switch(query) {
@ -33,11 +32,7 @@
break;
default:
options = {
gunzip : query === 'unzip'
};
pipe.create(readStream, name, options, function(error) {
flop.write(name, readStream, 'unzip', function(error) {
var msg;
if (!error)