mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-20 01:47:35 +00:00
feature(pipe) create: params -> read, write, options, callback
This commit is contained in:
parent
a471f7dbaf
commit
f11e44e792
6 changed files with 132 additions and 130 deletions
|
|
@ -41,7 +41,11 @@
|
|||
|
||||
function readPipe(params) {
|
||||
var name, names, lenght,
|
||||
p = params;
|
||||
p = params,
|
||||
options = {
|
||||
gzip : p.gzip,
|
||||
notEnd : true
|
||||
};
|
||||
|
||||
if (p.names) {
|
||||
names = p.names.slice();
|
||||
|
|
@ -54,23 +58,17 @@
|
|||
} else {
|
||||
name = names.shift();
|
||||
|
||||
pipe.create({
|
||||
from : name,
|
||||
write : p.write,
|
||||
gzip : p.gzip,
|
||||
notEnd : true,
|
||||
callback: function(error) {
|
||||
if (error)
|
||||
Util.exec(p.callback, error);
|
||||
else
|
||||
readPipe({
|
||||
dir : p.dir,
|
||||
names : names,
|
||||
write : p.write,
|
||||
gzip : p.gzip,
|
||||
callback : p.callback
|
||||
});
|
||||
}
|
||||
pipe.create(name, p.write, options, function(error) {
|
||||
if (error)
|
||||
Util.exec(p.callback, error);
|
||||
else
|
||||
readPipe({
|
||||
dir : p.dir,
|
||||
names : names,
|
||||
write : p.write,
|
||||
gzip : p.gzip,
|
||||
callback : p.callback
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -286,7 +286,8 @@
|
|||
isGzip = isGZIP(p.request) && p.gzip;
|
||||
|
||||
fs.lstat(p.name, function(error, stat) {
|
||||
var time, length, range;
|
||||
var time, length, range,
|
||||
options = {};
|
||||
|
||||
if (error) {
|
||||
sendError(params, error);
|
||||
|
|
@ -308,15 +309,14 @@
|
|||
|
||||
mainSetHeader(params);
|
||||
|
||||
pipe.create({
|
||||
from : p.name,
|
||||
write : p.response,
|
||||
options = {
|
||||
gzip : isGzip && !range,
|
||||
range : range,
|
||||
callback: function(error) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
}
|
||||
range : range
|
||||
};
|
||||
|
||||
pipe.create(p.name, p.response, options, function(error) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -21,53 +21,62 @@
|
|||
* @param params
|
||||
* @param callback
|
||||
*/
|
||||
function create(params) {
|
||||
var gzip, read, write, isFsWrite,
|
||||
p = params,
|
||||
func = p.callback,
|
||||
options = {
|
||||
function create(read, write, options, callback) {
|
||||
var gzip, isFsWrite, func,
|
||||
isStrRead = Util.isString(read),
|
||||
isStrWrite = Util.isString(write),
|
||||
isFunc = Util.isFunction(options),
|
||||
o = {},
|
||||
optionsRead = {
|
||||
bufferSize: 4 * 1024
|
||||
};
|
||||
|
||||
if (p) {
|
||||
if (p.range)
|
||||
options = {
|
||||
start : p.range.start,
|
||||
end : p.range.end,
|
||||
};
|
||||
|
||||
read = p.read || fs.createReadStream(p.from, options);
|
||||
|
||||
if (p.write)
|
||||
write = p.write;
|
||||
else {
|
||||
write = fs.createWriteStream(p.to);
|
||||
isFsWrite = true;
|
||||
}
|
||||
|
||||
if (p.gzip || p.gunzip) {
|
||||
if (p.gzip)
|
||||
gzip = zlib.createGzip();
|
||||
else
|
||||
gzip = zlib.createGunzip();
|
||||
|
||||
on('error', read, func);
|
||||
read = read.pipe(gzip);
|
||||
}
|
||||
|
||||
on('error', write, func);
|
||||
on('error', read, func);
|
||||
|
||||
Util.exec.if(!isFsWrite, function() {
|
||||
read.pipe(write, {
|
||||
end: !p.notEnd
|
||||
});
|
||||
|
||||
on('end', read, func);
|
||||
}, function(callback) {
|
||||
on('open', write, callback);
|
||||
Util.checkArgs(arguments, ['read', 'write', 'options']);
|
||||
|
||||
if (isFunc)
|
||||
callback = options;
|
||||
else
|
||||
o = options;
|
||||
|
||||
func = Util.exec.ret(callback);
|
||||
|
||||
if (options.range)
|
||||
Util.extend(optionsRead, {
|
||||
start : o.range.start,
|
||||
end : o.range.end,
|
||||
});
|
||||
|
||||
|
||||
if (isStrRead)
|
||||
read = fs.createReadStream(read, optionsRead);
|
||||
|
||||
if (isStrWrite) {
|
||||
write = fs.createWriteStream(write);
|
||||
isFsWrite = true;
|
||||
}
|
||||
|
||||
if (o.gzip || o.gunzip) {
|
||||
if (o.gzip)
|
||||
gzip = zlib.createGzip();
|
||||
else
|
||||
gzip = zlib.createGunzip();
|
||||
|
||||
on('error', read, func);
|
||||
read = read.pipe(gzip);
|
||||
}
|
||||
|
||||
on('error', write, func);
|
||||
on('error', read, func);
|
||||
|
||||
Util.exec.if(!isFsWrite, function() {
|
||||
read.pipe(write, {
|
||||
end: !o.notEnd
|
||||
});
|
||||
|
||||
on('end', read, func);
|
||||
}, function(callback) {
|
||||
on('open', write, callback);
|
||||
});
|
||||
}
|
||||
|
||||
function on(event, emitter, callback) {
|
||||
|
|
|
|||
|
|
@ -40,13 +40,8 @@
|
|||
NOT_LOG = true,
|
||||
|
||||
fse = {
|
||||
copy : ncp || function(from, to, callback) {
|
||||
pipe.create({
|
||||
from : from,
|
||||
to : to,
|
||||
callback : callback
|
||||
});
|
||||
},
|
||||
copy : ncp || pipe.create,
|
||||
|
||||
delete : rimraf || function(path, callback) {
|
||||
dir.isDir(path, function(error, isDir) {
|
||||
if (error)
|
||||
|
|
@ -278,7 +273,7 @@
|
|||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onPUT(params) {
|
||||
var p, cmd, files, name, json, config, data,
|
||||
var p, cmd, files, name, json, config, data, read, write, options,
|
||||
ret = main.checkParams(params, ['body']);
|
||||
|
||||
if (ret) {
|
||||
|
|
@ -344,42 +339,46 @@
|
|||
break;
|
||||
|
||||
case 'zip':
|
||||
if (!Util.checkObjTrue(files, ['from']))
|
||||
if (!files.from) {
|
||||
sendError(params, p.data);
|
||||
else
|
||||
pipe.create({
|
||||
from : files.from,
|
||||
to : files.to || files.from + '.zip',
|
||||
gzip : true,
|
||||
callback : function(error) {
|
||||
var name = path.basename(files.from);
|
||||
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
sendMsg(params, 'zip', name);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
read = files.from,
|
||||
write = files.to || files.from + '.zip';
|
||||
|
||||
options = {
|
||||
gzip: true
|
||||
};
|
||||
|
||||
pipe.create(read, write, options, function(error) {
|
||||
var name = path.basename(files.from);
|
||||
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
sendMsg(params, 'zip', name);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'unzip':
|
||||
if (!Util.checkObjTrue(files, ['from']))
|
||||
if (!files.from) {
|
||||
sendError(params, p.data);
|
||||
else
|
||||
pipe.create({
|
||||
from : files.from,
|
||||
to : files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']),
|
||||
gunzip : true,
|
||||
callback : function(error) {
|
||||
var name = path.basename(files.from);
|
||||
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
sendMsg(params, 'unzip', name);
|
||||
}
|
||||
} else {
|
||||
read = files.from;
|
||||
write = files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']);
|
||||
options = {
|
||||
gunzip: true
|
||||
},
|
||||
|
||||
pipe.create(read, write, options, function(error) {
|
||||
var name = path.basename(read);
|
||||
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
sendMsg(params, 'unzip', name);
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
|
|
|||
|
|
@ -47,17 +47,13 @@
|
|||
error = CloudFunc.formatMsg('hash', error, 'error');
|
||||
func(error);
|
||||
} else
|
||||
pipe.create({
|
||||
from : name,
|
||||
write : hash,
|
||||
callback : function (error) {
|
||||
var hex;
|
||||
|
||||
if (!error)
|
||||
hex = hash.get();
|
||||
|
||||
func(error, hex);
|
||||
}
|
||||
pipe.create(name, hash, function (error) {
|
||||
var hex;
|
||||
|
||||
if (!error)
|
||||
hex = hash.get();
|
||||
|
||||
func(error, hex);
|
||||
});
|
||||
break;
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@
|
|||
exports.onPut = onPut;
|
||||
|
||||
function onPut(name, query, readStream, callback) {
|
||||
var func = Util.exec.ret(callback),
|
||||
var options,
|
||||
func = Util.exec.ret(callback),
|
||||
baseName = path.basename(name);
|
||||
|
||||
switch(query) {
|
||||
|
|
@ -32,18 +33,17 @@
|
|||
break;
|
||||
|
||||
default:
|
||||
pipe.create({
|
||||
read : readStream,
|
||||
gunzip : query === 'unzip',
|
||||
to : name,
|
||||
callback : function(error) {
|
||||
var msg;
|
||||
|
||||
if (!error)
|
||||
msg = CloudFunc.formatMsg('save', baseName);
|
||||
|
||||
func(error, msg);
|
||||
}
|
||||
options = {
|
||||
gunzip : query === 'unzip'
|
||||
};
|
||||
|
||||
pipe.create(readStream, name, function(error) {
|
||||
var msg;
|
||||
|
||||
if (!error)
|
||||
msg = CloudFunc.formatMsg('save', baseName);
|
||||
|
||||
func(error, msg);
|
||||
});
|
||||
break;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue