diff --git a/lib/server/rest.js b/lib/server/rest.js index f413292b..18e0fc55 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -241,16 +241,16 @@ * * @param pParams {command, method, body, requrest, response} */ - function onPUT(pParams) { - var name, json, config, names, to, from, copyFiles, - ret = main.checkParams(pParams, ['body']); + function onPUT(params) { + var p, cmd, files, name, json, config, copyFiles, + ret = main.checkParams(params, ['body']); if (ret) { - var p = pParams, - lCmd = p.command, - lFiles = Util.parseJSON(p.body); + p = params, + cmd = p.command, + files = Util.parseJSON(p.body); - switch(lCmd) { + switch(cmd) { case 'auth': main.auth(p.body, function(error, token) { if (error) @@ -264,37 +264,38 @@ break; case 'mv': - if (!Util.checkObjTrue(lFiles, ['from', 'to']) ) - sendError(pParams, p.data); + if (!Util.checkObjTrue(files, ['from', 'to']) ) + sendError(params, p.data); else - fs.rename(lFiles.from, lFiles.to, function(pError) { - checkSendError(pError, pParams, function() { - sendResponse(pParams); - }); + fs.rename(files.from, files.to, function(error) { + if (error) + sendError(params, error); + else + sendResponse(params); }); break; case 'cp': - if (!Util.checkObjTrue(lFiles, ['from', 'names', 'to'])) - sendError(pParams, p.data); + if (!Util.checkObjTrue(files, ['from', 'names', 'to'])) + sendError(params, p.data); else { - lFiles.namesAll = Util.slice(lFiles.names); + files.namesAll = Util.slice(files.names); copyFiles = function(files) { var names = files.names, namesAll = files.namesAll, - name = names.shift(); + name = names.shift(), from = files.from, - to = files.to, + to = files.to; fse.copy(from + name, to + name, function(error) { var length = names.length; if (error) - sendError(pParams, error); + sendError(params, error); else if (!length) - sendMsg(pParams, 'copy', namesAll); + sendMsg(params, 'copy', namesAll); else copyFiles({ from : from, @@ -305,72 +306,77 @@ }); }; - copyFiles(lFiles); + copyFiles(files); } break; case 'zip': - if (!Util.checkObjTrue(lFiles, ['from'])) - sendError(pParams, p.data); + if (!Util.checkObjTrue(files, ['from'])) + sendError(params, p.data); else pipe.create({ - from : lFiles.from, - to : lFiles.to || lFiles.from + '.zip', + from : files.from, + to : files.to || files.from + '.zip', gzip : true, - callback : function(pError) { - checkSendError(pError, pParams, function() { - var lName = path.basename(lFiles.from); - sendMsg(pParams, 'zip', lName); - }); + callback : function(error) { + var name = path.basename(files.from); + + if (error) + sendError(params, error); + else + sendMsg(params, 'zip', name); } }); break; case 'unzip': - if (!Util.checkObjTrue(lFiles, ['from'])) - sendError(pParams, p.data); + if (!Util.checkObjTrue(files, ['from'])) + sendError(params, p.data); else pipe.create({ - from : lFiles.from, - to : lFiles.to || Util.removeStrOneTime(lFiles.from, ['.zip', '.gzip']), + from : files.from, + to : files.to || Util.removeStrOneTime(files.from, ['.zip', '.gzip']), gunzip : true, callback : function(error) { - checkSendError(error, pParams, function() { - var name = path.basename(lFiles.from); - sendMsg(pParams, 'zip', name); - }); + var name = path.basename(files.from); + + if (error) + sendError(params, error); + else + sendMsg(params, 'unzip', name); } }); break; case 'config': - var passwd = lFiles && lFiles.password, + var passwd = files && files.password, sha = crypto.createHash('sha1'); config = main.config; if (passwd) { sha.update(passwd); passwd = sha.digest('hex'); - lFiles.password = passwd; + files.password = passwd; } - for (name in lFiles) - config[name] = lFiles[name]; + for (name in files) + config[name] = files[name]; json = Util.stringifyJSON(config) + '\n'; fs.writeFile(JSONDIR + 'config.json', json, function(error) { - checkSendError(error, pParams, function() { - sendMsg(pParams, 'config', name); - }); + if (error) + sendError(params, error); + else + sendMsg(params, 'config', name); }); break; default: - send(pParams); + send(params); break; } }