mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
feature(stream) putFile -> stream.createPipe
This commit is contained in:
parent
ad34db0221
commit
981ed59201
3 changed files with 84 additions and 58 deletions
|
|
@ -100,6 +100,7 @@
|
|||
exports.VOLUMES = getVolumes(),
|
||||
|
||||
/* Additional Modules */
|
||||
exports.stream = stream = srvrequire('stream'),
|
||||
exports.socket = srvrequire('socket'),
|
||||
exports.auth = srvrequire('auth').auth,
|
||||
exports.appcache = srvrequire('appcache'),
|
||||
|
|
@ -220,21 +221,12 @@
|
|||
* @param pGzip - данные сжаты gzip'ом
|
||||
*/
|
||||
function sendFile(pParams){
|
||||
var lZipStream, lRet = checkParams(pParams);
|
||||
|
||||
if(lRet){
|
||||
var lRet = checkParams(pParams);
|
||||
console.log('####')
|
||||
if (lRet) {
|
||||
var p = pParams,
|
||||
lGzip = isGZIP(p.request) && p.gzip,
|
||||
|
||||
lReadStream = fs.createReadStream(p.name, {
|
||||
'bufferSize': 4 * 1024
|
||||
});
|
||||
|
||||
lReadStream.on('error', function(pError){
|
||||
p.response.writeHead(FILE_NOT_FOUND, 'OK');
|
||||
p.response.end(String(pError));
|
||||
});
|
||||
|
||||
lGzip = isGZIP(p.request) && p.gzip;
|
||||
|
||||
p.response.writeHead(OK, generateHeaders({
|
||||
name : p.name,
|
||||
cache : p.cache,
|
||||
|
|
@ -242,14 +234,19 @@
|
|||
query : getQuery(p.request)
|
||||
}) );
|
||||
|
||||
if (lGzip && !p.gziped)
|
||||
lZipStream = lReadStream.pipe( zlib.createGzip() );
|
||||
else
|
||||
lZipStream = lReadStream;
|
||||
|
||||
lZipStream.pipe(p.response);
|
||||
|
||||
lRet = true;
|
||||
stream.createPipe({
|
||||
from: p.name,
|
||||
write: p.response,
|
||||
zip : lGzip && !p.gziped,
|
||||
callback: function(pError) {
|
||||
console.log('****')
|
||||
var lError = pError && pError.toString();
|
||||
if (pError) {
|
||||
p.response.writeHead(FILE_NOT_FOUND, 'OK');
|
||||
p.response.end(lError);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return lRet;
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@
|
|||
fs = main.fs,
|
||||
path = main.path,
|
||||
Util = main.util,
|
||||
stream = main.stream,
|
||||
CloudFunc = main.cloudfunc,
|
||||
zlib = main.zlib,
|
||||
dir = main.dir,
|
||||
OK = 200,
|
||||
Header = main.generateHeaders({
|
||||
|
|
@ -145,7 +145,7 @@
|
|||
});
|
||||
|
||||
else
|
||||
putFile({
|
||||
stream.createPipe({
|
||||
read : p.request,
|
||||
to : p.name,
|
||||
callback : function(pError) {
|
||||
|
|
@ -319,7 +319,7 @@
|
|||
|
||||
case 'cp':
|
||||
if (Util.checkObjTrue(lFiles, ['from', 'to']))
|
||||
putFile({
|
||||
stream.createPipe({
|
||||
from : lFiles.from,
|
||||
to : lFiles.to,
|
||||
callback : function(pError) {
|
||||
|
|
@ -334,8 +334,8 @@
|
|||
break;
|
||||
|
||||
case 'zip':
|
||||
if (Util.checkObjTrue(lFiles, ['from', 'to']))
|
||||
putFile({
|
||||
if (Util.checkObjTrue(lFiles, ['from']))
|
||||
stream.createPipe({
|
||||
from : lFiles.from,
|
||||
to : lFiles.to || lFiles.from + '.zip',
|
||||
zip : true,
|
||||
|
|
@ -381,37 +381,6 @@
|
|||
});
|
||||
}
|
||||
|
||||
function putFile(pParams) {
|
||||
var lZlib, lError, lMsg, lRead, lWrite,
|
||||
p = pParams;
|
||||
|
||||
if (p) {
|
||||
lRead = p.read || fs.createReadStream(p.from);
|
||||
lWrite = p.write || fs.createWriteStream(p.to);
|
||||
|
||||
lError = function(pError) {
|
||||
Util.exec(p.callback, pError);
|
||||
};
|
||||
|
||||
if (p.zip) {
|
||||
lZlib = zlib.createGzip();
|
||||
lRead.on('error', lError);
|
||||
lRead = lRead.pipe(lZlib);
|
||||
}
|
||||
|
||||
lWrite.on('error', lError);
|
||||
lRead.on('error', lError);
|
||||
|
||||
lWrite.on('open', function() {
|
||||
lRead.pipe(lWrite);
|
||||
|
||||
lRead.on('end', function() {
|
||||
Util.exec(p.callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function sendMsg(pParams, pMsg, pName) {
|
||||
main.sendResponse(pParams, pMsg + ': ok("' + pName + '")');
|
||||
}
|
||||
|
|
|
|||
60
lib/server/stream.js
Normal file
60
lib/server/stream.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (!global.cloudcmd)
|
||||
return console.log(
|
||||
'# stream.js' + '\n' +
|
||||
'# -----------' + '\n' +
|
||||
'# Module is part of Cloud Commander,' + '\n' +
|
||||
'# used for work with stream.' + '\n' +
|
||||
'# If you wont to see at work call' + '\n' +
|
||||
'# stream.createPipe' + '\n' +
|
||||
'# http://coderaiser.github.com/cloudcmd' + '\n');
|
||||
|
||||
var main = global.cloudcmd.main,
|
||||
fs = main.fs,
|
||||
Util = main.util,
|
||||
zlib = main.zlib;
|
||||
|
||||
exports.createPipe = function(pParams) {
|
||||
var lZlib, lError, lMsg, lRead, lWrite, lIsFsWrite,
|
||||
p = pParams;
|
||||
|
||||
if (p) {
|
||||
lRead = p.read || fs.createReadStream(p.from, {
|
||||
bufferSize: 4 * 1024
|
||||
});
|
||||
|
||||
if (p.write)
|
||||
lWrite = p.write;
|
||||
else {
|
||||
lWrite = fs.createWriteStream(p.to);
|
||||
lIsFsWrite = true;
|
||||
}
|
||||
|
||||
lError = function(pError) {
|
||||
Util.exec(p.callback, pError);
|
||||
};
|
||||
|
||||
if (p.zip) {
|
||||
lZlib = zlib.createGzip();
|
||||
lRead.on('error', lError);
|
||||
lRead = lRead.pipe(lZlib);
|
||||
}
|
||||
|
||||
lWrite.on('error', lError);
|
||||
lRead.on('error', lError);
|
||||
|
||||
if (lIsFsWrite)
|
||||
lWrite.on('open', function() {
|
||||
lRead.pipe(lWrite);
|
||||
lRead.on('end', Util.retExec(p.callback));
|
||||
});
|
||||
else {
|
||||
lRead.pipe(lWrite);
|
||||
lRead.on('end', Util.retExec(p.callback));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue