mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-24 03:05:41 +00:00
refactor(rest) sendError -> callback
This commit is contained in:
parent
a7c0c78885
commit
0fe62b2a67
1 changed files with 51 additions and 39 deletions
|
|
@ -51,7 +51,11 @@
|
|||
* @pParams {request, responce}
|
||||
*/
|
||||
exports.api = function(request, response, callback) {
|
||||
var apiURL, name, ret;
|
||||
var apiURL, name, ret,
|
||||
params = {
|
||||
request : request,
|
||||
response : response,
|
||||
};
|
||||
|
||||
if (request && response) {
|
||||
apiURL = CloudFunc.apiURL;
|
||||
|
|
@ -59,11 +63,10 @@
|
|||
ret = Util.isContainStr(name, apiURL);
|
||||
|
||||
if (ret) {
|
||||
name = Util.rmStrOnce(name, apiURL) || '/';
|
||||
sendData({
|
||||
request : request,
|
||||
response : response,
|
||||
name : name
|
||||
params.name = Util.rmStrOnce(name, apiURL) || '/';
|
||||
|
||||
sendData(params, function(error) {
|
||||
sendError(error, params);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -93,7 +96,7 @@
|
|||
*
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function sendData(params) {
|
||||
function sendData(params, callback) {
|
||||
var p, isFS, isMD,
|
||||
ret = main.checkParams(params);
|
||||
|
||||
|
|
@ -103,11 +106,11 @@
|
|||
isMD = Util.isContainStrAtBegin(p.name, '/markdown');
|
||||
|
||||
if (isFS)
|
||||
onFS(params);
|
||||
onFS(params, callback);
|
||||
else if (isMD)
|
||||
markdown.operate(p.name, p.request, function(error, data) {
|
||||
if (error)
|
||||
sendError(error, p);
|
||||
callback(error);
|
||||
else
|
||||
sendResponse(p, data, NOT_LOG);
|
||||
});
|
||||
|
|
@ -117,16 +120,16 @@
|
|||
|
||||
switch(p.request.method) {
|
||||
case 'GET':
|
||||
ret = onGET(params);
|
||||
ret = onGET(params, callback);
|
||||
break;
|
||||
|
||||
case 'PUT':
|
||||
pipe.getBody(p.request, function(error, body) {
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
else {
|
||||
if (error) {
|
||||
callback(error);
|
||||
} else {
|
||||
p.body = body;
|
||||
onPUT(params);
|
||||
onPUT(params, callback);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
|
@ -136,7 +139,7 @@
|
|||
return ret;
|
||||
}
|
||||
|
||||
function onFS(params) {
|
||||
function onFS(params, callback) {
|
||||
var p, query, path,
|
||||
ret = main.checkParams(params);
|
||||
|
||||
|
|
@ -162,7 +165,7 @@
|
|||
if (error.code === 'ENOTDIR')
|
||||
main.sendFile(params);
|
||||
else
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
} else {
|
||||
data.path = format.addSlashToEnd(p.name);
|
||||
params.name += '.json';
|
||||
|
|
@ -182,7 +185,7 @@
|
|||
case 'PUT':
|
||||
onFSPut(query, path, p.request, function(error, msg) {
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
else
|
||||
sendResponse(params, msg);
|
||||
});
|
||||
|
|
@ -193,7 +196,7 @@
|
|||
var files;
|
||||
|
||||
if (error)
|
||||
sendError(error, p);
|
||||
callback(error);
|
||||
else {
|
||||
files = Util.parseJSON(body);
|
||||
|
||||
|
|
@ -201,7 +204,7 @@
|
|||
var names;
|
||||
|
||||
if (error) {
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
} else {
|
||||
if (files && files.length)
|
||||
names = files;
|
||||
|
|
@ -225,10 +228,10 @@
|
|||
*
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onGET(pParams) {
|
||||
var p, cmd, json, ret = main.checkParams(pParams);
|
||||
function onGET(params, callback) {
|
||||
var p, cmd, json, ret = main.checkParams(params);
|
||||
if (ret) {
|
||||
p = pParams,
|
||||
p = params,
|
||||
cmd = p.command;
|
||||
|
||||
switch(cmd) {
|
||||
|
|
@ -253,7 +256,7 @@
|
|||
message: 'Error: command not found!'
|
||||
};
|
||||
|
||||
sendError(json, p);
|
||||
callback(json);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -266,8 +269,8 @@
|
|||
*
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onPUT(params) {
|
||||
var p, cmd, files, name, json, config, data, from, to,
|
||||
function onPUT(params, callback) {
|
||||
var p, cmd, files, name, json, config, data, from, to, error,
|
||||
ret = main.checkParams(params, ['body']);
|
||||
|
||||
if (ret) {
|
||||
|
|
@ -279,7 +282,7 @@
|
|||
case 'auth':
|
||||
main.auth(p.body, function(error, token) {
|
||||
if (error)
|
||||
sendError(error, p);
|
||||
callback(error);
|
||||
else
|
||||
send({
|
||||
response: p.response,
|
||||
|
|
@ -290,11 +293,16 @@
|
|||
|
||||
case 'mv':
|
||||
if (!files.from || !files.to) {
|
||||
sendError(p.data, params);
|
||||
callback(p.data);
|
||||
|
||||
} else if (isRootWin32(files.to)) {
|
||||
sendError(getWin32RootMsg('to'), params);
|
||||
error = getWin32RootMsg('to');
|
||||
callback(error);
|
||||
|
||||
} else if (isRootWin32(files.from)) {
|
||||
sendError(getWin32RootMsg('from'), params);
|
||||
error = getWin32RootMsg('from');
|
||||
callback(error);
|
||||
|
||||
} else {
|
||||
files.from = mellow.convertPath(files.from);
|
||||
files.to = mellow.convertPath(files.to);
|
||||
|
|
@ -306,7 +314,7 @@
|
|||
|
||||
copyFiles(files, flop.move, function(error) {
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
else
|
||||
sendMsg(params, 'move', data);
|
||||
});
|
||||
|
|
@ -316,11 +324,15 @@
|
|||
|
||||
case 'cp':
|
||||
if (!files.from || !files.names || !files.to) {
|
||||
sendError(p.data, params);
|
||||
callback(p.data);
|
||||
|
||||
} else if (isRootWin32(files.to)) {
|
||||
sendError(getWin32RootMsg('to'), params);
|
||||
error = getWin32RootMsg('to');
|
||||
callback(error);
|
||||
|
||||
} else if (isRootWin32(files.from)) {
|
||||
sendError(getWin32RootMsg('from'), params);
|
||||
error = getWin32RootMsg('from');
|
||||
callback(error);
|
||||
} else {
|
||||
files.from = mellow.convertPath(files.from);
|
||||
files.to = mellow.convertPath(files.to);
|
||||
|
|
@ -328,7 +340,7 @@
|
|||
files.namesAll = Util.slice(files.names);
|
||||
copyFiles(files, flop.copy, function(error) {
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
else
|
||||
sendMsg(params, 'copy', files.namesAll);
|
||||
});
|
||||
|
|
@ -337,7 +349,7 @@
|
|||
|
||||
case 'zip':
|
||||
if (!files.from) {
|
||||
sendError(p.data, params);
|
||||
callback(p.data);
|
||||
} else {
|
||||
from = mellow.convertPath(files.from);
|
||||
|
||||
|
|
@ -350,7 +362,7 @@
|
|||
var name = path.basename(files.from);
|
||||
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
else
|
||||
sendMsg(params, 'zip', name);
|
||||
});
|
||||
|
|
@ -359,7 +371,7 @@
|
|||
|
||||
case 'unzip':
|
||||
if (!files.from) {
|
||||
sendError(p.data, params);
|
||||
callback(p.data);
|
||||
} else {
|
||||
from = mellow.convertPath(files.from);
|
||||
|
||||
|
|
@ -372,7 +384,7 @@
|
|||
var name = path.basename(files.from);
|
||||
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
else
|
||||
sendMsg(params, 'unzip', name);
|
||||
});
|
||||
|
|
@ -398,7 +410,7 @@
|
|||
|
||||
fs.writeFile(JSONDIR + 'config.json', json, function(error) {
|
||||
if (error)
|
||||
sendError(error, params);
|
||||
callback(error);
|
||||
else
|
||||
sendMsg(params, 'config', name);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue