(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,
CONFIG_PATH = JSONDIR + 'config.json',
KEY = DIR + 'ssl/ssl.key',
CERT = DIR + 'ssl/ssl.crt',
HTML_FS_DIR = HTMLDIR + 'fs/',
INDEX_PATH = HTML_FS_DIR + 'index.html',
FILE_TMPL = HTML_FS_DIR + 'file.html',
PANEL_TMPL = HTML_FS_DIR + 'panel.html',
PATH_TMPL = HTML_FS_DIR + 'path.html',
LINK_TMPL = HTML_FS_DIR + '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) {
query = main.getQuery(params.request),
sendContent(name, query, function(name, error, data, isFile) {
if (error)
main.sendError(params, error);
else if (isFile) {
params.name = name;
main.sendFile(params);
} else {
params.name = name;
main.sendResponse(params, data, true);
}
});
} else {
ret = false;
Util.exec(callback);
}
}
return ret;
}
function sendContent(name, query, callback) {
name = Util.removeStrOneTime(name, CloudFunc.FS) || main.SLASH;
fs.stat(name, function(error, stat) {
var func = Util.retExec(callback, name);
if (error)
func(error);
else
if (!stat.isDirectory())
func(error, null, true);
else {
processContent(name, query, callback);
}
});
}
function processContent(name, query, callback) {
main.commander.getDirContent(name, function(error, json) {
var data, name,
isJSON = Util.isContainStr(query, 'json');
if (!isJSON && !error)
readIndex(json, Util.retExec(callback, INDEX_PATH));
else {
if (!error) {
data = Util.stringifyJSON(json);
name +='.json';
}
Util.exec(callback, name, error, data);
}
});
}
function readIndex(json, callback) {
Util.ifExec(!Minify, function(params) {
var name = params && params.name;
fs.readFile(name || INDEX_PATH, 'utf8', function(error, template) {
var panel, data,
config = main.config,
minify = config.minify;
if (!error) {
panel = CloudFunc.buildFromJSON(json, FileTemplate, PathTemplate, LinkTemplate),
data = indexProcessing({
panel : panel,
data : template,
});
}
Util.exec(callback, error, data);
});
}, 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;
}
})();