mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-25 17:04:16 +00:00
improved reading file with streams
This commit is contained in:
parent
c3cfbce39f
commit
e24042e672
5 changed files with 82 additions and 53 deletions
|
|
@ -6,6 +6,7 @@
|
|||
SRVDIR,
|
||||
Util,
|
||||
|
||||
OK = 200,
|
||||
Extensions = {
|
||||
'.css' : 'text/css',
|
||||
'.js' : 'text/javascript',
|
||||
|
|
@ -28,6 +29,7 @@
|
|||
|
||||
/* Functions */
|
||||
exports.generateHeaders = generateHeaders,
|
||||
exports.sendFile = sendFile,
|
||||
exports.require = mrequire,
|
||||
exports.librequire = librequire,
|
||||
exports.srvrequire = srvrequire,
|
||||
|
|
@ -75,7 +77,7 @@
|
|||
* second initializing after all modules load, so global var is
|
||||
* totally filled of all information that should know all modules
|
||||
*/
|
||||
global.cloudcmd.main = exports;
|
||||
var main = global.cloudcmd.main = exports;
|
||||
|
||||
/**
|
||||
* function do safe require of needed module
|
||||
|
|
@ -152,5 +154,33 @@
|
|||
|
||||
return lRet;
|
||||
}
|
||||
|
||||
/**
|
||||
* send file to client thru pipe
|
||||
* and gzip it if client support
|
||||
*
|
||||
* @param pName - имя файла
|
||||
* @param pGzip - данные сжаты gzip'ом
|
||||
*/
|
||||
function sendFile(pParams){
|
||||
var lName = pParams.name,
|
||||
lReq = pParams.request,
|
||||
lRes = pParams.response,
|
||||
|
||||
lEnc = lReq.headers['accept-encoding'] || '',
|
||||
lGzip = lEnc.match(/\bgzip\b/),
|
||||
|
||||
lReadStream = main.fs.createReadStream(lName, {
|
||||
'bufferSize': 4 * 1024
|
||||
});
|
||||
|
||||
lRes.writeHead(OK, generateHeaders(lName, true) );
|
||||
|
||||
|
||||
if (lGzip)
|
||||
lReadStream = lReadStream.pipe( main.zlib.createGzip() );
|
||||
|
||||
lReadStream.pipe(lRes);
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@
|
|||
command : lCommand,
|
||||
method : lMethod,
|
||||
body : pBody,
|
||||
request : lReq,
|
||||
response : lRes
|
||||
});
|
||||
|
||||
|
|
@ -57,7 +58,7 @@
|
|||
/**
|
||||
* getting data on method and command
|
||||
*
|
||||
* @param pParams {command, method, body, response}
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function getData(pParams){
|
||||
var lResult,
|
||||
|
|
@ -85,7 +86,7 @@
|
|||
/**
|
||||
* process data on GET request
|
||||
*
|
||||
* @param pParams {command, method, body, response}
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onGET(pParams){
|
||||
var lResult = {error: 'command not found'},
|
||||
|
|
@ -114,7 +115,7 @@
|
|||
/**
|
||||
* process data on PUT request
|
||||
*
|
||||
* @param pParams {command, method, body, response}
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onPUT(pParams){
|
||||
var lResult = {error: 'command not found'},
|
||||
|
|
@ -128,23 +129,16 @@
|
|||
send(lRes, pTocken);
|
||||
});
|
||||
lResult = false;
|
||||
|
||||
break;
|
||||
|
||||
/* Example:
|
||||
* read=[lib/dom.js, lib/cloudfunc.js, client.js]
|
||||
*/
|
||||
case 'read':
|
||||
console.log(lBody);
|
||||
var lFiles = lBody;
|
||||
|
||||
|
||||
if( Util.isString(lFiles) ){
|
||||
lRes.writeHead(OK, main.generateHeaders(lFiles, true) );
|
||||
|
||||
fs.createReadStream(lFiles, {
|
||||
'bufferSize': 4 * 1024
|
||||
})
|
||||
.pipe( zlib.createGzip() )
|
||||
.pipe(lRes);
|
||||
pParams.name = lFiles;
|
||||
main.sendFile(pParams);
|
||||
|
||||
lResult = null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue