mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
var DIR = '../',
|
|
DIR_SERVER = DIR + 'server/',
|
|
main = require('main'),
|
|
files = require(DIR_SERVER + 'files'),
|
|
Util = require(DIR_SERVER + 'util'),
|
|
CloudFunc = require(DIR + 'cloudfunc'),
|
|
zlib = require('zlib');
|
|
|
|
module.exports = join;
|
|
|
|
function join(before, req, res, callback) {
|
|
var names,
|
|
exec = Util.exec,
|
|
readFunc = exec.with(readPipe, req, res),
|
|
path = main.getPathName(req),
|
|
isJoin = CloudFunc.isJoinURL(path);
|
|
|
|
if (!isJoin) {
|
|
exec(callback);
|
|
} else {
|
|
names = CloudFunc.getJoinArray(path);
|
|
|
|
exec.if(!before, readFunc, function(callback) {
|
|
before(names, callback);
|
|
});
|
|
}
|
|
|
|
return isJoin;
|
|
}
|
|
|
|
function readPipe(req, res, names) {
|
|
var stream,
|
|
path = main.getPathName(req),
|
|
gzip = zlib.createGzip(),
|
|
isGzip = main.isGZIP(req);
|
|
|
|
main.mainSetHeader({
|
|
name : names[0],
|
|
cache : true,
|
|
gzip : isGzip,
|
|
request : req,
|
|
response : res
|
|
});
|
|
|
|
stream = isGzip ? gzip : res;
|
|
|
|
files.readPipe({
|
|
names : names,
|
|
write : stream,
|
|
callback : function(error) {
|
|
var errorStr;
|
|
|
|
if (error)
|
|
if (!res.headersSent)
|
|
main.sendError({
|
|
request : req,
|
|
response : res,
|
|
name : path
|
|
}, error);
|
|
else {
|
|
Util.log(error);
|
|
errorStr = error.toString();
|
|
stream.end(errorStr);
|
|
}
|
|
}
|
|
});
|
|
|
|
/*
|
|
* pipe should be setted up after
|
|
* readPipe called with stream param
|
|
*/
|
|
if (isGzip)
|
|
gzip.pipe(res);
|
|
}
|
|
})();
|