(function() {
'use strict';
var DIR = __dirname + '/',
main = require(DIR + 'lib/server/main'),
LIBDIR = main.LIBDIR,
SRVDIR = main.SRVDIR,
CLIENTDIR = LIBDIR + 'client',
HTMLDIR = main.HTMLDIR,
JSONDIR = main.JSONDIR,
path = main.path,
fs = main.fs,
files = main.files,
CloudFunc = main.cloudfunc,
AppCache = main.appcache,
Util = main.util,
update = main.update,
server = main.librequire('server'),
Minify = main.minify,
Config = main.config,
INDEX_PATH = HTMLDIR + 'index.html',
CONFIG_PATH = JSONDIR + 'config.json',
KEY = DIR + 'ssl/ssl.key',
CERT = DIR + 'ssl/ssl.crt',
FILE_TMPL = HTMLDIR + 'file.html',
PANEL_TMPL = HTMLDIR + 'panel.html',
PATH_TMPL = HTMLDIR + 'path.html',
LINK_TMPL = HTMLDIR + 'link.html',
FileTemplate, PanelTemplate, PathTemplate, LinkTemplate,
FS = CloudFunc.FS;
/* reinit main dir os if we on Win32 should be backslashes */
DIR = main.DIR;
readConfig(init);
/**
* additional processing of index file
*/
function indexProcessing(options) {
var keysPanel, left, right,
LEFT = CloudFunc.PANEL_LEFT,
RIGHT = CloudFunc.PANEL_RIGHT,
data = options.data,
panel = options.panel;
if (!Config.appCache)
data = Util.removeStr(data, [
/* min */
' manifest=/cloudcmd.appcache',
/* normal */
' manifest="/cloudcmd.appcache"'
]);
if (!Config.showKeysPanel) {
lKeysPanel = '
' + name);
params.name = main.HTMLDIR + name + '.html';
main.sendFile(params);
} else if (isFS)
sendContent(params);
else {
ret = false;
Util.exec(callback);
}
}
return ret;
}
function sendContent(pParams) {
var p, lRet = main.checkParams(pParams);
if (lRet) {
p = pParams;
p.name = Util.removeStrOneTime(p.name, CloudFunc.FS) || main.SLASH;
fs.stat(p.name, function(error, stat) {
if (error)
main.sendError(pParams, error);
else
if (stat.isDirectory())
processContent(pParams);
else
main.sendFile(pParams);
});
}
return lRet;
}
function processContent(params) {
var p = params,
ret = main.checkParams(params);
if (ret)
main.commander.getDirContent(p.name, function(error, json) {
var query, isJSON;
if (error)
main.sendError(params, error);
else {
query = main.getQuery(p.request);
isJSON = Util.isContainStr(query, 'json');
if (!isJSON)
readIndex(json, params);
else {
p.data = Util.stringifyJSON(json);
p.name +='.json';
main.sendResponse(params, null, true);
}
}
});
}
function readIndex(pJSON, params) {
var p = params;
Util.ifExec(!Minify, function(params) {
var name = params && params.name;
fs.readFile(name || INDEX_PATH, 'utf8', function(error, template) {
var panel,
config = main.config,
minify = config.minify;
if (error)
main.sendError(p, error);
else {
p.name = INDEX_PATH,
panel = CloudFunc.buildFromJSON(pJSON, FileTemplate, PathTemplate, LinkTemplate),
main.sendResponse(p, indexProcessing({
panel : panel,
data : template,
}), true);
}
});
}, function(callback) {
Minify.optimize(INDEX_PATH, {
callback : callback,
returnName : true
});
});
}
/* function sets stdout to file log.txt */
function writeLogsToFile() {
var stdout = process.stdout,
writeFile = fs.createWriteStream('log.txt'),
write = writeFile.write.bind(writeFile);
stdout.write = write;
}
})();