mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
feature(files) add pipeFiles from pipe
This commit is contained in:
parent
ba880af169
commit
41cd8ad96c
7 changed files with 82 additions and 85 deletions
|
|
@ -1,14 +1,17 @@
|
|||
(function(object) {
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
var fs = require('fs'),
|
||||
zlib = require('zlib'),
|
||||
|
||||
pipe = require('./pipe'),
|
||||
Util = require('../util');
|
||||
Util = require('../util'),
|
||||
type = Util.type;
|
||||
|
||||
object.read = function(files, options, callback) {
|
||||
module.exports.read = function(files, options, callback) {
|
||||
var done = [],
|
||||
isDone = false,
|
||||
noOptions = Util.type.function(options),
|
||||
noOptions = type.function(options),
|
||||
readFiles = {},
|
||||
doneFunc = function (name, error, data) {
|
||||
done.pop();
|
||||
|
|
@ -37,9 +40,7 @@
|
|||
});
|
||||
};
|
||||
|
||||
object.readPipe = readPipe;
|
||||
|
||||
function readPipe(names, write, options, callback) {
|
||||
module.exports.readPipe = function readPipe(names, write, options, callback) {
|
||||
var name, lenght;
|
||||
|
||||
if (!callback) {
|
||||
|
|
@ -62,13 +63,70 @@
|
|||
} else {
|
||||
name = names.shift();
|
||||
|
||||
pipe(name, write, options, function(error) {
|
||||
pipeFiles(name, write, options, function(error) {
|
||||
if (error)
|
||||
callback(error);
|
||||
else
|
||||
readPipe(names, write, options, callback);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* create pipe
|
||||
*
|
||||
* @param read - readable stream
|
||||
* @param write - writable stream
|
||||
*
|
||||
* @param options {
|
||||
* gzip
|
||||
* ungzip
|
||||
* notEnd
|
||||
* }
|
||||
*
|
||||
* @param callback - function(error) {}
|
||||
*/
|
||||
module.exports.pipe = pipeFiles;
|
||||
|
||||
function pipeFiles(read, write, options, callback) {
|
||||
var gzip,
|
||||
isStrRead = type.string(read),
|
||||
isStrWrite = type.string(write),
|
||||
isFunc = type.function(options),
|
||||
o = {},
|
||||
optionsRead = {
|
||||
bufferSize: 4 * 1024
|
||||
};
|
||||
|
||||
Util.checkArgs(arguments, ['read', 'write', 'callback']);
|
||||
|
||||
if (isFunc)
|
||||
callback = options;
|
||||
else
|
||||
o = options;
|
||||
|
||||
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);
|
||||
|
||||
if (o.gzip || o.gunzip) {
|
||||
if (o.gzip)
|
||||
gzip = zlib.createGzip();
|
||||
else
|
||||
gzip = zlib.createGunzip();
|
||||
|
||||
read = read.pipe(gzip);
|
||||
}
|
||||
|
||||
pipe([read, write], options, callback);
|
||||
}
|
||||
|
||||
})(this);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
size = require(DIR + 'size'),
|
||||
readify = require(DIR + 'readify'),
|
||||
time = require(DIR + 'timem'),
|
||||
pipe = require(DIR + 'pipe'),
|
||||
files = require(DIR + 'files'),
|
||||
tryRequire = require(DIR + 'tryRequire'),
|
||||
|
||||
ncp = tryRequire('ncp'),
|
||||
|
|
@ -101,7 +101,7 @@
|
|||
stopOnError: true
|
||||
}, callback);
|
||||
else
|
||||
pipe(from, to, callback);
|
||||
files.pipe(from, to, callback);
|
||||
});
|
||||
};
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
fstream = tryRequire('fstream'),
|
||||
|
||||
Util = require(DIR_LIB + 'util'),
|
||||
files = require(DIR + 'files'),
|
||||
pipe = require(DIR + 'pipe');
|
||||
|
||||
exports.pack = function(from, to, callback) {
|
||||
|
|
@ -34,7 +35,7 @@
|
|||
};
|
||||
|
||||
if (!is || !fstream || !tar) {
|
||||
pipe(from, to, options, callback);
|
||||
files.pipe(from, to, options, callback);
|
||||
} else {
|
||||
streamDir = fstream.Reader(optionsDir);
|
||||
streamTar = tar.Pack(optionsTar);
|
||||
|
|
@ -49,7 +50,7 @@
|
|||
streamFile = fs.createWriteStream(to);
|
||||
}
|
||||
|
||||
pipe.all([
|
||||
pipe([
|
||||
streamDir,
|
||||
streamTar,
|
||||
streamZip,
|
||||
|
|
@ -75,7 +76,7 @@
|
|||
write = to;
|
||||
}
|
||||
|
||||
pipe(from, write, options, callback);
|
||||
files.pipe(from, write, options, callback);
|
||||
};
|
||||
|
||||
function isDir(name, callback) {
|
||||
|
|
|
|||
|
|
@ -2,70 +2,10 @@
|
|||
'use strict';
|
||||
|
||||
var fs = require('fs'),
|
||||
zlib = require('zlib'),
|
||||
|
||||
Util = require('../util'),
|
||||
type = Util.type;
|
||||
Util = require('../util');
|
||||
|
||||
module.exports = create;
|
||||
module.exports = all;
|
||||
module.exports.getBody = getBody;
|
||||
module.exports.all = all;
|
||||
|
||||
/**
|
||||
* create pipe
|
||||
*
|
||||
* @param read - readable stream
|
||||
* @param write - writable stream
|
||||
*
|
||||
* @param options {
|
||||
* gzip
|
||||
* ungzip
|
||||
* notEnd
|
||||
* }
|
||||
*
|
||||
* @param callback - function(error) {}
|
||||
*/
|
||||
function create(read, write, options, callback) {
|
||||
var gzip,
|
||||
isStrRead = type.string(read),
|
||||
isStrWrite = type.string(write),
|
||||
isFunc = type.function(options),
|
||||
o = {},
|
||||
optionsRead = {
|
||||
bufferSize: 4 * 1024
|
||||
};
|
||||
|
||||
Util.checkArgs(arguments, ['read', 'write', 'callback']);
|
||||
|
||||
if (isFunc)
|
||||
callback = options;
|
||||
else
|
||||
o = options;
|
||||
|
||||
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);
|
||||
|
||||
if (o.gzip || o.gunzip) {
|
||||
if (o.gzip)
|
||||
gzip = zlib.createGzip();
|
||||
else
|
||||
gzip = zlib.createGunzip();
|
||||
|
||||
on('error', read, callback);
|
||||
read = read.pipe(gzip);
|
||||
}
|
||||
|
||||
pipe([read, write], options, callback);
|
||||
}
|
||||
|
||||
function on(event, emitter, callback) {
|
||||
var isSet,
|
||||
|
|
@ -80,7 +20,7 @@
|
|||
emitter.on(event, callback);
|
||||
}
|
||||
|
||||
function all(streams, callback) {
|
||||
function all(streams, options, callback) {
|
||||
var n, write, isFSWrite;
|
||||
|
||||
Util.checkArgs(arguments, ['streams', 'callback']);
|
||||
|
|
@ -90,7 +30,7 @@
|
|||
isFSWrite = write instanceof fs.WriteStream;
|
||||
|
||||
Util.exec.if(!isFSWrite, function() {
|
||||
pipe(streams, callback);
|
||||
pipe(streams, options, callback);
|
||||
}, function(callback) {
|
||||
write.on('open', callback);
|
||||
});
|
||||
|
|
@ -100,8 +40,6 @@
|
|||
var main,
|
||||
read = streams[0];
|
||||
|
||||
Util.checkArgs(arguments, ['streams', 'callback']);
|
||||
|
||||
if (!callback)
|
||||
callback = options;
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
DIR_JSON = __dirname + '/../../json/',
|
||||
|
||||
Util = require('../util'),
|
||||
pipe = require('./pipe'),
|
||||
files = require('./files'),
|
||||
ext = require(DIR_JSON + 'ext'),
|
||||
|
||||
OK = 200,
|
||||
|
|
@ -227,7 +227,7 @@
|
|||
range : range
|
||||
};
|
||||
|
||||
pipe(p.name, p.response, options, function(error) {
|
||||
files.pipe(p.name, p.response, options, function(error) {
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
var path = require('path'),
|
||||
DIR = path.resolve(__dirname + '/../../../') + '/',
|
||||
DIR_SERVER = DIR + 'server/',
|
||||
pipe = require(DIR_SERVER + 'pipe'),
|
||||
files = require(DIR_SERVER + 'files'),
|
||||
flop = require(DIR_SERVER + 'flop'),
|
||||
hash = require(DIR_SERVER + 'hash'),
|
||||
mellow = require(DIR_SERVER + 'mellow'),
|
||||
|
|
@ -45,7 +45,7 @@
|
|||
error = 'hash: not suported, try update node';
|
||||
callback(Error(error));
|
||||
} else
|
||||
pipe(name, hashStream, function (error) {
|
||||
files.pipe(name, hashStream, function (error) {
|
||||
var hex;
|
||||
|
||||
if (!error)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
flop = require(DIR_SERVER + 'flop'),
|
||||
packer = require(DIR_SERVER + 'packer'),
|
||||
pipe = require(DIR_SERVER + 'pipe'),
|
||||
files = require(DIR_SERVER + 'files'),
|
||||
patch = require('./patch');
|
||||
|
||||
module.exports = function(query, name, readStream, callback) {
|
||||
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
switch(query) {
|
||||
default:
|
||||
pipe(readStream, name, onSave);
|
||||
files.pipe(readStream, name, onSave);
|
||||
break;
|
||||
|
||||
case 'dir':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue