mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-19 01:17:18 +00:00
refactor(rest) copyFiles
This commit is contained in:
parent
024a72d421
commit
f85ef76896
1 changed files with 79 additions and 38 deletions
|
|
@ -20,6 +20,7 @@
|
|||
Util = main.util,
|
||||
pipe = main.pipe,
|
||||
CloudFunc = main.cloudfunc,
|
||||
dir = main.dir,
|
||||
|
||||
onFSGet = main.srvrequire('rest/fs/get').onGet,
|
||||
onFSPut = main.srvrequire('rest/fs/put').onPut,
|
||||
|
|
@ -33,16 +34,34 @@
|
|||
Header = main.generateHeaders({
|
||||
name:'api.json'
|
||||
}),
|
||||
rimraf = main.require('rimraf'),
|
||||
ncp = main.srvrequire('ncp'),
|
||||
|
||||
NOT_LOG = true,
|
||||
|
||||
fse = {
|
||||
copy: main.srvrequire('ncp') || function(from, to, callback) {
|
||||
copy : ncp || function(from, to, callback) {
|
||||
pipe.create({
|
||||
from : from,
|
||||
to : to,
|
||||
callback : callback
|
||||
from : from,
|
||||
to : to,
|
||||
callback : callback
|
||||
});
|
||||
},
|
||||
delete : rimraf || function(path, callback) {
|
||||
dir.isDir(path, function(isDir) {
|
||||
if (isDir)
|
||||
fs.rmDir(path, callback);
|
||||
else
|
||||
fs.unlink(path, callback);
|
||||
});
|
||||
},
|
||||
move : function(from, to, callback) {
|
||||
if (ncp && rimraf)
|
||||
ncp(from, to, function() {
|
||||
rimraf(from, callback);
|
||||
});
|
||||
else
|
||||
fs.rename(from, to, callback);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -242,13 +261,13 @@
|
|||
* @param pParams {command, method, body, requrest, response}
|
||||
*/
|
||||
function onPUT(params) {
|
||||
var p, cmd, files, name, json, config, namesAll, func,
|
||||
ret = main.checkParams(params, ['body']);
|
||||
var p, cmd, files, name, json, config,
|
||||
ret = main.checkParams(params, ['body']);
|
||||
|
||||
if (ret) {
|
||||
p = params,
|
||||
cmd = p.command,
|
||||
files = Util.parseJSON(p.body);
|
||||
cmd = p.command,
|
||||
files = Util.parseJSON(p.body);
|
||||
|
||||
switch(cmd) {
|
||||
case 'auth':
|
||||
|
|
@ -264,36 +283,43 @@
|
|||
break;
|
||||
|
||||
case 'mv':
|
||||
if (!Util.checkObjTrue(files, ['from', 'to']) )
|
||||
if (!Util.checkObjTrue(files, ['from', 'names', 'to']) )
|
||||
sendError(params, p.data);
|
||||
else
|
||||
fs.rename(files.from, files.to, function(error) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
sendResponse(params);
|
||||
});
|
||||
|
||||
else {
|
||||
files.namesAll = Util.slice(files.names);
|
||||
copyFiles(files,
|
||||
function(name, callback) {
|
||||
fse.remove(name, function(error) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
callback();
|
||||
});
|
||||
},
|
||||
|
||||
function(error, length) {
|
||||
fse.remove(name, function(error) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else if (!length)
|
||||
sendMsg(params, 'move', files.namesAll);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'cp':
|
||||
if (!Util.checkObjTrue(files, ['from', 'names', 'to']))
|
||||
sendError(params, p.data);
|
||||
else {
|
||||
namesAll = Util.slice(files.names);
|
||||
|
||||
func = function(error, files) {
|
||||
var length = files.names.length;
|
||||
|
||||
files.namesAll = Util.slice(files.names);
|
||||
copyFiles(files, null, function(error, isLast) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else if (!length)
|
||||
sendMsg(params, 'copy', namesAll);
|
||||
else
|
||||
copyFiles(files, func);
|
||||
};
|
||||
|
||||
copyFiles(files, func);
|
||||
else if (isLast)
|
||||
sendMsg(params, 'copy', files.namesAll);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -371,16 +397,31 @@
|
|||
return ret;
|
||||
}
|
||||
|
||||
function copyFiles(files, callback) {
|
||||
var names = files.names,
|
||||
name = names.shift(),
|
||||
from = files.from,
|
||||
to = files.to,
|
||||
func = Util.retExec(callback);
|
||||
function copyFiles(files, callbackProcess, callback) {
|
||||
var names = files.names,
|
||||
|
||||
from = files.from,
|
||||
to = files.to,
|
||||
|
||||
isFunc = Util.isFunction(callbackProcess),
|
||||
|
||||
copy = function() {
|
||||
var isLast = !names.length,
|
||||
name = names.shift(),
|
||||
process = Util.retExec(callbackProcess, name);
|
||||
|
||||
if (isLast)
|
||||
Util.exec(callback, null, isLast);
|
||||
else
|
||||
fse.copy(from + name, to + name, function(error) {
|
||||
if (error)
|
||||
Util.exec(callback, error);
|
||||
else
|
||||
Util.ifExec(!isFunc, copy, process);
|
||||
});
|
||||
};
|
||||
|
||||
fse.copy(from + name, to + name, function(error) {
|
||||
func(error, files);
|
||||
});
|
||||
copy();
|
||||
}
|
||||
|
||||
function sendMsg(pParams, pMsg, pName, pStatus) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue