mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
improved sendFile function
This commit is contained in:
parent
61e138130b
commit
9ef0d134d1
1 changed files with 14 additions and 27 deletions
|
|
@ -184,36 +184,23 @@
|
|||
|
||||
lEnc = lReq.headers['accept-encoding'] || '',
|
||||
lGzip = lEnc.match(/\bgzip\b/),
|
||||
lReadStream,
|
||||
|
||||
lReadStream = fs.createReadStream(lName, {
|
||||
'bufferSize': 4 * 1024
|
||||
});
|
||||
|
||||
lSendError = function(pError){
|
||||
var lJson = JSON.stringify({
|
||||
error: pError
|
||||
});
|
||||
|
||||
lRes.writeHead(ERROR, 'OK');
|
||||
lRes.end(lJson);
|
||||
};
|
||||
|
||||
main.fs.exists(lName, function(pExist){
|
||||
lRet = pExist;
|
||||
if(pExist)
|
||||
Util.tryCatch(function(){
|
||||
lReadStream = fs.createReadStream(lName, {
|
||||
'bufferSize': 4 * 1024
|
||||
});
|
||||
lRes.writeHead(OK, generateHeaders(lName, lGzip) );
|
||||
|
||||
if (lGzip)
|
||||
lReadStream = lReadStream.pipe( zlib.createGzip() );
|
||||
|
||||
lReadStream.pipe(lRes);
|
||||
}, lSendError);
|
||||
|
||||
else
|
||||
lSendError('File not Found');
|
||||
lReadStream.on('error', function(pError){
|
||||
lRes.writeHead(ERROR, 'OK');
|
||||
lRes.end(String(pError));
|
||||
});
|
||||
|
||||
lRes.writeHead(OK, generateHeaders(lName, lGzip) );
|
||||
|
||||
if (lGzip)
|
||||
lReadStream = lReadStream.pipe( zlib.createGzip() );
|
||||
|
||||
lReadStream.pipe(lRes);
|
||||
|
||||
return lRet;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue