From 84e14ce2ddfa56ab7f23946b801447e6277fac77 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 8 Feb 2013 09:22:03 -0500 Subject: [PATCH] refactored --- lib/server/commander.js | 98 +++++++++++++++++++---------------------- lib/server/main.js | 12 ++--- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/lib/server/commander.js b/lib/server/commander.js index f32d9931..44ec8a85 100644 --- a/lib/server/commander.js +++ b/lib/server/commander.js @@ -79,64 +79,58 @@ * @param pFiles */ function readDir(pParams){ - var lRet, - lError, lFiles, lHTTP, lDirPath, - lReq, lRes; - - if(pParams){ - lError = pParams.error; - lFiles = pParams.data; - lHTTP = pParams.params; - - if(lHTTP){ - lReq = lHTTP.request, - lRes = lHTTP.response; - lRet = true; - lDirPath = getDirPath(lReq); - } - } - - if(lRet && !lError && lFiles){ - lFiles = lFiles.sort(); - - /* Получаем информацию о файлах */ - var n = lFiles.length, - lStats = {}, - lFilesData = { - files : lFiles, - stats : lStats, - request : lReq, - response : lRes - }, + var lRet = Util.checkObj(pParams, + ['error', 'data', 'params']) && - lFill = function(){ - fillJSON(lFilesData); - }; - - if(n){ - for(var i = 0; i < n; i++){ - var lName = lDirPath + lFiles[i], - lParams = { - callback : lFill, - count : n, - name : lFiles[i], - stats : lStats, - }; + Util.checkObjTrue(pParams.params, + [REQUEST, RESPONSE]); + if(lRet){ + var p = pParams, + c = p.params, + lDirPath = getDirPath(c.request); + + if(!p.error && p.data){ + p.data = p.data.sort(); + + /* Получаем информацию о файлах */ + var n = p.data.length, + lStats = {}, + lFilesData = { + files : p.data, + stats : lStats, + request : c.request, + response : c.response + }, - fs.stat( lName, Util.call(getFilesStat, lParams) ); + lFill = function(){ + fillJSON(lFilesData); + }; + + if(n){ + for(var i = 0; i < n; i++){ + var lName = lDirPath + p.data[i], + lParams = { + callback : lFill, + count : n, + name : p.data[i], + stats : lStats, + }; + + fs.stat( lName, Util.call(getFilesStat, lParams) ); + } } + else + fillJSON(lFilesData); } else - fillJSON(lFilesData); + sendResponse({ + data : p.Error.toString(), + name : lDirPath, + request : c.request, + response: c.response, + status : FILE_NOT_FOUND + }); } - else - sendResponse({ - data : lError.toString(), - name : lDirPath, - request : lReq, - response: lRes, - status : FILE_NOT_FOUND - }); } /** diff --git a/lib/server/main.js b/lib/server/main.js index 24ea68f1..8d3cf430 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -150,6 +150,9 @@ lType = ext[lExt] || 'text/plain'; + /* if type of file any, but img - + * then we shoud specify charset + */ if( !Util.isContainStr(lType, 'img') ) lContentEncoding = '; charset=UTF-8'; @@ -159,16 +162,13 @@ if(!lCacheControl) lCacheControl = 31337 * 21; - + lRet = { - /* if type of file any, but img - - * then we shoud specify charset - */ 'Content-Type': lType + lContentEncoding, 'cache-control': 'max-age=' + lCacheControl, - 'last-modified': new Date().toString(), + 'last-modified': new Date().toString(), /* https://developers.google.com/speed/docs/best-practices - /caching?hl=ru#LeverageProxyCaching */ + /caching?hl=ru#LeverageProxyCaching */ 'Vary': 'Accept-Encoding' };