diff --git a/cloudcmd.js b/cloudcmd.js index fb14b00e..ce133bd9 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -17,6 +17,7 @@ AppCache = main.appcache, Util = main.util, update = main.update, + dir = main.dir, server = main.librequire('server'), Minify = main.minify, @@ -250,9 +251,8 @@ } function getContent(name, callback) { - fs.stat(name, function(error, stat) { + dir.isDir(name, function(error, isDir) { var getDirContent = main.commander.getDirContent, - isDir = stat && stat.isDirectory(), func = Util.retExec(callback); if (!error && isDir) diff --git a/lib/server/commander.js b/lib/server/commander.js index 72a03af9..92e05eb3 100644 --- a/lib/server/commander.js +++ b/lib/server/commander.js @@ -20,17 +20,15 @@ checkParams = main.checkCallBackParams; exports.getDirContent = function(path, callback) { - var ret = Util.isString(path); + var isString = Util.isString(path); - if (!ret) + if (!isString) Util.exec(callback, "First parameter should be a string"); else fs.readdir(path, readDir.bind(null, { callback : callback, path : path })); - - return ret; }; diff --git a/lib/server/dir.js b/lib/server/dir.js index db7a63b1..1521559a 100644 --- a/lib/server/dir.js +++ b/lib/server/dir.js @@ -19,6 +19,19 @@ Util = main.util, path = main.path; + exports.isDir = function(name, callback) { + name += ''; + + fs.stat(name, function(error, stat) { + var isDir; + + if (!error) + isDir = stat.isDirectory(); + + Util.exec(callback, error, isDir); + }); + }; + exports.getSize = function(dir, callback) { var total = 0; diff --git a/lib/server/rest.js b/lib/server/rest.js index 565c6891..4ab1641e 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -141,7 +141,7 @@ else { p.name += '.json'; str = Util.stringifyJSON(data); - sendResponse(p, str); + sendResponse(p, str, true); } }); @@ -246,7 +246,7 @@ } function onDelete(name, files, query, callback) { - var i, n, onStat, + var i, n, onStat, isDirFunc, assync = 0, rmFile = fs.unlink.bind(fs), rmDir = fse.remove.bind(fse), @@ -265,17 +265,16 @@ n = files && files.length, dir = name; - onStat = function(name, error, stat) { + onIsDir = function(name, error, isDir) { var log = Util.log.bind(Util); ++assync; if (error) func(error); else { - if (stat.isDirectory()) + if (isDir) rmDir(name, log); - - else if (stat.isFile()) + else rmFile(name, log); if (assync === n) @@ -284,9 +283,11 @@ }; for (i = 0; i < n; i ++) { - name = dir + files[i]; + name = dir + files[i]; + isDirFunc = Util.bind(onIsDir, name); + + dir.isDir(name, isDirFunc); Util.log(name); - fs.stat(name, onStat.bind(null, name)); } break; } @@ -340,9 +341,8 @@ break; default: - fs.stat(name, function(error, stat) { - var getDirContent = main.commander.getDirContent, - isDir = stat && stat.isDirectory(); + dir.isDir(name, function(error, isDir) { + var getDirContent = main.commander.getDirContent; if (isDir && !error) getDirContent(name, func); @@ -398,8 +398,6 @@ lCmd = p.command, lFiles = Util.parseJSON(p.body); - console.log(lFiles); - switch(lCmd) { case 'auth': main.auth(p.body, function(pTocken) {