(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', PATH_TMPL = HTMLDIR + 'path.html', LINK_TMPL = HTMLDIR + 'link.html', FileTemplate, 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(pData) { var lPath, lReplace, lKeysPanel, lData = pData.data, lAdditional = pData.additional; if (!Config.appCache) lData = Util.removeStr(lData, [ /* min */ ' manifest=/cloudcmd.appcache', /* normal */ ' manifest="/cloudcmd.appcache"' ]); if (!Config.showKeysPanel) { lKeysPanel = '
' + p.name); pParams.name = main.HTMLDIR + p.name + '.html'; lRet = main.sendFile( pParams ); } else if ( Util.isContainStrAtBegin(p.name, FS) || Util.strCmp( p.name, '/') ) lRet = sendContent( pParams ); else lRet = false; } return lRet; } 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(pError, pStat) { if (!pError) if ( pStat.isDirectory() ) processContent(pParams); else main.sendFile( pParams ); else main.sendError(pParams, pError); }); } return lRet; } function processContent(pParams) { var p, lRet = main.checkParams(pParams); if (lRet) { p = pParams; main.commander.getDirContent(p.name, function(pError, pJSON) { var lQuery, isJSON; if (pError) main.sendError(pParams, pError); else { lQuery = main.getQuery(p.request); isJSON = Util.isContainStr(lQuery, 'json'); if (!isJSON) readIndex(pJSON, pParams); else { p.data = Util.stringifyJSON(pJSON); p.name +='.json'; main.sendResponse(p, 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 lPanel, lList, config = main.config, minify = config.minify; if (error) main.sendError(p, error); else { p.name = INDEX_PATH, lPanel = CloudFunc.buildFromJSON(pJSON, FileTemplate, PathTemplate, LinkTemplate), lList = '
' + lPanel + '
' + ''; main.sendResponse(p, indexProcessing({ additional : lList, 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; } })();