From 981ed592019dbefa048b3e8ebe18dd3e23f9e166 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 20 Aug 2013 12:17:21 +0000 Subject: [PATCH] feature(stream) putFile -> stream.createPipe --- lib/server/main.js | 41 ++++++++++++++---------------- lib/server/rest.js | 41 ++++-------------------------- lib/server/stream.js | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 58 deletions(-) create mode 100644 lib/server/stream.js diff --git a/lib/server/main.js b/lib/server/main.js index 855b52e3..8b924109 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -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; diff --git a/lib/server/rest.js b/lib/server/rest.js index 05b6602b..3a50eef7 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -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 + '")'); } diff --git a/lib/server/stream.js b/lib/server/stream.js new file mode 100644 index 00000000..3fa9596e --- /dev/null +++ b/lib/server/stream.js @@ -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)); + } + } + }; + +})();