diff --git a/lib/server/rest.js b/lib/server/rest.js index 57b0ea96..d7ced0a8 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -261,7 +261,7 @@ * @param pParams {command, method, body, requrest, response} */ function onPUT(params) { - var p, cmd, files, name, json, config, + var p, cmd, files, name, json, config, data, ret = main.checkParams(params, ['body']); if (ret) { @@ -283,10 +283,15 @@ break; case 'mv': - if (!Util.checkObjTrue(files, ['from', 'names', 'to']) ) + if (!Util.checkObjTrue(files, ['from', 'to']) ) sendError(params, p.data); else { - files.namesAll = Util.slice(files.names); + + if (files.names) + data = Util.slice(files.names); + else + data = files; + copyFiles(files, function(path, callback) { fse.delete(path, function(error) { @@ -301,7 +306,7 @@ if (error) sendError(params, error); else - sendMsg(params, 'move', files.namesAll); + sendMsg(params, 'move', data); }); } @@ -396,22 +401,31 @@ } function copyFiles(files, callbackProcess, callback) { - var names = files.names, - - from = files.from, - to = files.to, - - isFunc = Util.isFunction(callbackProcess), + var names = files.names, + isFunc = Util.isFunction(callbackProcess), + processFunc = names ? fse.copy : fs.rename, copy = function() { - var isLast = !names.length, + var isLast, name, process, + from = files.from, + to = files.to; + + if (names) { + isLast = !names.length, name = names.shift(), - process = Util.retExec(callbackProcess, from + name); + from += name; + to += name; + } else { + isLast = false; + names = []; + } + + process = Util.retExec(callbackProcess, from); if (isLast) Util.exec(callback, null); else - fse.copy(from + name, to + name, function(error) { + processFunc(from, to, function(error) { if (error) Util.exec(callback, error); else @@ -422,9 +436,18 @@ copy(); } - function sendMsg(pParams, pMsg, pName, pStatus) { - var msg = CloudFunc.formatMsg(pMsg, pName, pStatus); - sendResponse(pParams, msg); + function sendMsg(sendParam, msgParam, dataParam, status) { + var msg, data, + isObj = Util.isObject(dataParam); + + if (isObj) + data = Util.stringifyJSON(dataParam); + else + data = dataParam; + + msg = CloudFunc.formatMsg(msgParam, data, status); + + sendResponse(sendParam, msg); } })();