diff --git a/lib/server/ischanged.js b/lib/server/ischanged.js index b9905f40..7da5abcc 100644 --- a/lib/server/ischanged.js +++ b/lib/server/ischanged.js @@ -7,6 +7,7 @@ fs = main.fs, path = main.path, Util = main.util, + time = main.time, CHANGESNAME = JSONDIR + 'changes', CHANGES_JSON = CHANGESNAME + '.json', @@ -27,25 +28,22 @@ } } - fs.stat(pFileName, function(pError, pStat) { - var lTimeChanged, lFileTime; + time.get(pFileName, function(error, fileTime) { + var timeChanged; - if (!pError) { - lFileTime = pStat.mtime.getTime(); - - if (lReadedTime !== lFileTime) - lTimeChanged = Times[i] = { - name: pFileName, - time: lFileTime - }; - } - else - Util.log(pError); + if (error) + Util.log(error); + else if (lReadedTime !== fileTime) + timeChanged = Times[i] = { + name: pFileName, + time: fileTime + }; + - if (lTimeChanged) + if (timeChanged) writeFile(CHANGES_JSON, Util.stringifyJSON(Times)); - Util.exec(pCallBack, lTimeChanged); + Util.exec(pCallBack, timeChanged); }); }; diff --git a/lib/server/main.js b/lib/server/main.js index b6b51ec9..6557e5d9 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -111,6 +111,7 @@ exports.hash = srvrequire('hash'), diffPatch = librequire('diff/diff-match-patch').diff_match_patch, exports.diff = new (librequire('diff').DiffProto)(diffPatch), + exports.time = srvrequire('time'); exports.rest = srvrequire('rest').api, exports.update = srvrequire('update'), exports.ischanged = srvrequire('ischanged'); diff --git a/lib/server/rest.js b/lib/server/rest.js index e464e564..5d08a760 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -22,6 +22,7 @@ CloudFunc = main.cloudfunc, dir = main.dir, diff = main.diff, + time = main.time, JSONDIR = main.JSONDIR, OK = 200, sendError = main.sendError, @@ -116,6 +117,14 @@ sendResponse(p, lSize); }); }); + else if (Util.strCmp(lQuery, 'time')) + time.get(p.name, function(error, time) { + checkSendError(error, p, function() { + var timeStr = time.toString(); + + sendResponse(p, timeStr); + }); + }); else if (Util.strCmp(lQuery, 'hash')) { hash = Hash.create(); diff --git a/lib/server/time.js b/lib/server/time.js new file mode 100644 index 00000000..0aaf5ea3 --- /dev/null +++ b/lib/server/time.js @@ -0,0 +1,27 @@ +(function (object) { + 'use strict'; + + if(!global.cloudcmd) + return console.log( + '# dir.js' + '\n' + + '# -----------' + '\n' + + '# Module is part of Cloud Commander,' + '\n' + + '# used for getting file change time.' + '\n' + + '# http://cloudcmd.io' + '\n'); + + var main = global.cloudcmd.main, + fs = require('fs'), + Util = main.util; + + object.get = function(filename, callback) { + fs.stat(filename, function(error, stat) { + var time; + + if (!error) + time = stat.mtime.getTime(); + + Util.exec(callback, error, time); + }); + }; + +})(this);