mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-24 03:05:41 +00:00
refactor(rest) onPUT: params -> name, body
This commit is contained in:
parent
1159d1f4c8
commit
391db5f83f
1 changed files with 142 additions and 143 deletions
|
|
@ -89,7 +89,7 @@
|
|||
/**
|
||||
* getting data on method and command
|
||||
*
|
||||
* @param params {command, method, body, requrest, response}
|
||||
* @param params {method, body, requrest, response}
|
||||
*/
|
||||
function sendData(params, callback) {
|
||||
var p, isFS, isMD,
|
||||
|
|
@ -107,9 +107,6 @@
|
|||
callback(error, {notLog: true}, data);
|
||||
});
|
||||
else {
|
||||
if (p.name[0] === '/')
|
||||
p.command = Util.rmStrOnce(p.name, '/');
|
||||
|
||||
switch(p.request.method) {
|
||||
case 'GET':
|
||||
ret = onGET(params, callback);
|
||||
|
|
@ -117,12 +114,10 @@
|
|||
|
||||
case 'PUT':
|
||||
pipe.getBody(p.request, function(error, body) {
|
||||
if (error) {
|
||||
if (error)
|
||||
callback(error);
|
||||
} else {
|
||||
p.body = body;
|
||||
onPUT(params, callback);
|
||||
}
|
||||
else
|
||||
onPUT(p.name, body, callback);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
|
@ -160,8 +155,12 @@
|
|||
} else {
|
||||
if (!error) {
|
||||
data.path = format.addSlashToEnd(p.name);
|
||||
options.name += '.json';
|
||||
options.query = query;
|
||||
|
||||
options = {
|
||||
name : p.name + '.json',
|
||||
query : query,
|
||||
notLog : true
|
||||
};
|
||||
|
||||
if (isStr)
|
||||
str = data;
|
||||
|
|
@ -214,13 +213,15 @@
|
|||
/**
|
||||
* process data on GET request
|
||||
*
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
* @param pParams {method, body, requrest, response}
|
||||
*/
|
||||
function onGET(params, callback) {
|
||||
var p, cmd, json, ret = main.checkParams(params);
|
||||
if (ret) {
|
||||
p = params,
|
||||
cmd = p.command;
|
||||
p = params;
|
||||
|
||||
if (p.name[0] === '/')
|
||||
cmd = p.name.replace('/', '');
|
||||
|
||||
switch(cmd) {
|
||||
case '':
|
||||
|
|
@ -258,149 +259,147 @@
|
|||
*
|
||||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onPUT(params, callback) {
|
||||
var p, cmd, files, name, json, config, data, from, to, error,
|
||||
ret = main.checkParams(params, ['body']);
|
||||
function onPUT(name, body, callback) {
|
||||
var cmd, files, json, config, data, from, to, error;
|
||||
|
||||
if (ret) {
|
||||
p = params,
|
||||
cmd = p.command,
|
||||
files = Util.parseJSON(p.body);
|
||||
Util.checkArgs(arguments, ['name', 'body', 'callback']);
|
||||
|
||||
if (name[0] === '/')
|
||||
cmd = name.replace('/', '');
|
||||
|
||||
files = Util.parseJSON(body);
|
||||
|
||||
switch(cmd) {
|
||||
case 'auth':
|
||||
main.auth(body, function(error, token) {
|
||||
callback(error, Util.stringifyJSON({
|
||||
data: token
|
||||
}));
|
||||
});
|
||||
break;
|
||||
|
||||
case 'mv':
|
||||
if (!files.from || !files.to) {
|
||||
callback(body);
|
||||
|
||||
switch(cmd) {
|
||||
case 'auth':
|
||||
main.auth(p.body, function(error, token) {
|
||||
callback(error, Util.stringifyJSON({
|
||||
data: token
|
||||
}));
|
||||
} else if (isRootWin32(files.to)) {
|
||||
error = getWin32RootMsg('to');
|
||||
callback(error);
|
||||
|
||||
} else if (isRootWin32(files.from)) {
|
||||
error = getWin32RootMsg('from');
|
||||
callback(error);
|
||||
|
||||
} else {
|
||||
files.from = mellow.convertPath(files.from);
|
||||
files.to = mellow.convertPath(files.to);
|
||||
|
||||
if (files.names)
|
||||
data = Util.slice(files.names);
|
||||
else
|
||||
data = files;
|
||||
|
||||
copyFiles(files, flop.move, function(error) {
|
||||
var msg = formatMsg('move', data);
|
||||
|
||||
callback(error, msg);
|
||||
});
|
||||
break;
|
||||
|
||||
case 'mv':
|
||||
if (!files.from || !files.to) {
|
||||
callback(p.data);
|
||||
|
||||
} else if (isRootWin32(files.to)) {
|
||||
error = getWin32RootMsg('to');
|
||||
callback(error);
|
||||
|
||||
} else if (isRootWin32(files.from)) {
|
||||
error = getWin32RootMsg('from');
|
||||
callback(error);
|
||||
|
||||
} else {
|
||||
files.from = mellow.convertPath(files.from);
|
||||
files.to = mellow.convertPath(files.to);
|
||||
|
||||
if (files.names)
|
||||
data = Util.slice(files.names);
|
||||
else
|
||||
data = files;
|
||||
|
||||
copyFiles(files, flop.move, function(error) {
|
||||
var msg = formatMsg('move', data);
|
||||
|
||||
callback(error, msg);
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'cp':
|
||||
if (!files.from || !files.names || !files.to) {
|
||||
callback(p.data);
|
||||
|
||||
} else if (isRootWin32(files.to)) {
|
||||
error = getWin32RootMsg('to');
|
||||
callback(error);
|
||||
|
||||
} else if (isRootWin32(files.from)) {
|
||||
error = getWin32RootMsg('from');
|
||||
callback(error);
|
||||
} else {
|
||||
files.from = mellow.convertPath(files.from);
|
||||
files.to = mellow.convertPath(files.to);
|
||||
|
||||
files.namesAll = Util.slice(files.names);
|
||||
copyFiles(files, flop.copy, function(error) {
|
||||
var msg = formatMsg('copy', files.namesAll);
|
||||
|
||||
callback(error, msg);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'zip':
|
||||
if (!files.from) {
|
||||
callback(p.data);
|
||||
} else {
|
||||
from = mellow.convertPath(files.from);
|
||||
|
||||
if (files.to)
|
||||
to = mellow.convertPath(files.to);
|
||||
else
|
||||
to = from + '.zip';
|
||||
|
||||
pack.gzip(from, to, function(error) {
|
||||
var name = path.basename(files.from),
|
||||
msg = formatMsg('zip', name);
|
||||
|
||||
callback(error, msg);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'unzip':
|
||||
if (!files.from) {
|
||||
callback(p.data);
|
||||
} else {
|
||||
from = mellow.convertPath(files.from);
|
||||
|
||||
if (files.to)
|
||||
to = mellow.convertPath(files.to);
|
||||
else
|
||||
to = Util.rmStrOnce(files.from, ['.zip', '.gzip']);
|
||||
|
||||
pack.gunzip(from, to, function(error) {
|
||||
var name = path.basename(files.from),
|
||||
data = formatMsg('unzip', name);
|
||||
|
||||
callback(error, data);
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
|
||||
case 'cp':
|
||||
if (!files.from || !files.names || !files.to) {
|
||||
callback(body);
|
||||
|
||||
case 'config':
|
||||
var passwd = files && files.password,
|
||||
sha = crypto.createHash('sha1');
|
||||
config = main.config;
|
||||
} else if (isRootWin32(files.to)) {
|
||||
error = getWin32RootMsg('to');
|
||||
callback(error);
|
||||
|
||||
} else if (isRootWin32(files.from)) {
|
||||
error = getWin32RootMsg('from');
|
||||
callback(error);
|
||||
} else {
|
||||
files.from = mellow.convertPath(files.from);
|
||||
files.to = mellow.convertPath(files.to);
|
||||
|
||||
if (passwd) {
|
||||
sha.update(passwd);
|
||||
passwd = sha.digest('hex');
|
||||
files.password = passwd;
|
||||
}
|
||||
files.namesAll = Util.slice(files.names);
|
||||
copyFiles(files, flop.copy, function(error) {
|
||||
var msg = formatMsg('copy', files.namesAll);
|
||||
|
||||
callback(error, msg);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'zip':
|
||||
if (!files.from) {
|
||||
callback(body);
|
||||
} else {
|
||||
from = mellow.convertPath(files.from);
|
||||
|
||||
for (name in files)
|
||||
config[name] = files[name];
|
||||
if (files.to)
|
||||
to = mellow.convertPath(files.to);
|
||||
else
|
||||
to = from + '.zip';
|
||||
|
||||
json = Util.stringifyJSON(config) + '\n';
|
||||
pack.gzip(from, to, function(error) {
|
||||
var name = path.basename(files.from),
|
||||
msg = formatMsg('zip', name);
|
||||
|
||||
callback(error, msg);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'unzip':
|
||||
if (!files.from) {
|
||||
callback(body);
|
||||
} else {
|
||||
from = mellow.convertPath(files.from);
|
||||
|
||||
fs.writeFile(JSONDIR + 'config.json', json, function(error) {
|
||||
data = formatMsg('config', name);
|
||||
if (files.to)
|
||||
to = mellow.convertPath(files.to);
|
||||
else
|
||||
to = Util.rmStrOnce(files.from, ['.zip', '.gzip']);
|
||||
|
||||
pack.gunzip(from, to, function(error) {
|
||||
var name = path.basename(files.from),
|
||||
data = formatMsg('unzip', name);
|
||||
|
||||
callback(error, data);
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
callback();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
return ret;
|
||||
case 'config':
|
||||
var passwd = files && files.password,
|
||||
sha = crypto.createHash('sha1');
|
||||
config = main.config;
|
||||
|
||||
if (passwd) {
|
||||
sha.update(passwd);
|
||||
passwd = sha.digest('hex');
|
||||
files.password = passwd;
|
||||
}
|
||||
|
||||
for (name in files)
|
||||
config[name] = files[name];
|
||||
|
||||
json = Util.stringifyJSON(config) + '\n';
|
||||
|
||||
fs.writeFile(JSONDIR + 'config.json', json, function(error) {
|
||||
data = formatMsg('config', name);
|
||||
callback(error, data);
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
callback();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function copyFiles(files, processFunc, callback) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue