mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-25 08:54:10 +00:00
feature(flop) add mv, delete
This commit is contained in:
parent
62aa322513
commit
f2c9ed92c6
4 changed files with 47 additions and 79 deletions
|
|
@ -16,9 +16,9 @@
|
|||
Hash = require(DIR + 'hash'),
|
||||
pipe = require(DIR + 'pipe'),
|
||||
|
||||
fse = {
|
||||
mkdir : tryRequire('mkdirp') || fs.mkdir.bind(fs),
|
||||
};
|
||||
ncp = tryRequire('ncp'),
|
||||
rimraf = tryRequire('rimraf'),
|
||||
mkdir = tryRequire('mkdirp') || fs.mkdir;
|
||||
|
||||
exports.create = function(path, type, callback) {
|
||||
Util.checkArgs(arguments, ['path', 'callback']);
|
||||
|
|
@ -37,7 +37,7 @@
|
|||
break;
|
||||
|
||||
case 'dir':
|
||||
fse.mkdir(path, callback);
|
||||
mkdir(path, callback);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
|
@ -100,16 +100,35 @@
|
|||
|
||||
};
|
||||
|
||||
exports.delete = function() {
|
||||
|
||||
exports.delete = function(path, callback) {
|
||||
if (rimraf)
|
||||
rimraf(path, callback);
|
||||
else
|
||||
dir.isDir(path, function(error, isDir) {
|
||||
if (error)
|
||||
callback(error);
|
||||
else if (isDir)
|
||||
fs.rmdir(path, callback);
|
||||
else
|
||||
fs.unlink(path, callback);
|
||||
});
|
||||
};
|
||||
|
||||
exports.mv = function() {
|
||||
|
||||
exports.mv = function(from, to, callback) {
|
||||
dir.isDir(from, function(error, isDir) {
|
||||
if (error)
|
||||
callback(error);
|
||||
else if (isDir && ncp && rimraf)
|
||||
ncp(from, to, function() {
|
||||
rimraf(from, callback);
|
||||
});
|
||||
else
|
||||
fs.rename(from, to, callback);
|
||||
});
|
||||
};
|
||||
|
||||
exports.cp = function() {
|
||||
|
||||
exports.cp = function(path, callback) {
|
||||
(ncp || pipe.create)(path, callback);
|
||||
};
|
||||
|
||||
function tryRequire(name) {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
SLASH,
|
||||
ISWIN32,
|
||||
ext, dir,
|
||||
ext,
|
||||
path, fs, zlib, url, pipe, CloudFunc, diffPatch, querystring,
|
||||
|
||||
OK, RANGE, FILE_NOT_FOUND, MOVED_PERMANENTLY,
|
||||
|
|
@ -113,14 +113,12 @@
|
|||
exports.express = srvrequire('express'),
|
||||
exports.auth = srvrequire('auth').auth,
|
||||
exports.appcache = srvrequire('appcache'),
|
||||
exports.dir = dir = srvrequire('dir'),
|
||||
diffPatch = librequire('diff/diff-match-patch').diff_match_patch,
|
||||
exports.diff = new (librequire('diff').DiffProto)(diffPatch),
|
||||
exports.users = srvrequire('users');
|
||||
exports.rest = srvrequire('rest').api,
|
||||
exports.update = srvrequire('update'),
|
||||
exports.ischanged = srvrequire('ischanged');
|
||||
exports.commander = srvrequire('commander');
|
||||
exports.files = srvrequire('files');
|
||||
|
||||
exports.minify = srvrequire('minify').Minify;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@
|
|||
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,
|
||||
|
|
@ -34,33 +33,11 @@
|
|||
Header = main.generateHeaders({
|
||||
name:'api.json'
|
||||
}),
|
||||
rimraf = main.require('rimraf'),
|
||||
ncp = main.srvrequire('ncp'),
|
||||
|
||||
NOT_LOG = true,
|
||||
flop = require('./flop'),
|
||||
|
||||
NOT_LOG = true;
|
||||
|
||||
fse = {
|
||||
copy : ncp || pipe.create,
|
||||
|
||||
delete : rimraf || function(path, callback) {
|
||||
dir.isDir(path, function(error, isDir) {
|
||||
if (error)
|
||||
callback(error);
|
||||
else 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);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* rest interface
|
||||
|
|
@ -307,7 +284,7 @@
|
|||
|
||||
copyFiles(files,
|
||||
function(path, callback) {
|
||||
fse.delete(path, function(error) {
|
||||
flop.delete(path, function(error) {
|
||||
if (error)
|
||||
sendError(params, error);
|
||||
else
|
||||
|
|
@ -420,7 +397,7 @@
|
|||
function copyFiles(files, callbackProcess, callback) {
|
||||
var names = files.names,
|
||||
isFunc = Util.isFunction(callbackProcess),
|
||||
processFunc = names ? fse.copy : fs.rename,
|
||||
processFunc = names ? flop.copy : fs.rename,
|
||||
|
||||
copy = function() {
|
||||
var isLast, name, process,
|
||||
|
|
|
|||
|
|
@ -1,30 +1,21 @@
|
|||
(function(){
|
||||
'use strict';
|
||||
|
||||
var main = global.cloudcmd.main,
|
||||
fs = require('fs'),
|
||||
dir = main.dir,
|
||||
Util = main.util,
|
||||
rimraf = main.require('rimraf'),
|
||||
fse = {
|
||||
delete : rimraf || fs.rmdir,
|
||||
};
|
||||
var
|
||||
DIR = '../../../',
|
||||
DIR_SERVER = DIR + 'server/',
|
||||
Util = require(DIR + 'util'),
|
||||
flop = require(DIR_SERVER + 'flop');
|
||||
|
||||
exports.onDelete = onDelete;
|
||||
|
||||
function onDelete(name, files, query, callback) {
|
||||
var func = Util.exec.ret(callback),
|
||||
fileNames = Util.slice(files),
|
||||
rmFile = fs.unlink,
|
||||
rmDir = fse.delete;
|
||||
fileNames = Util.slice(files);
|
||||
|
||||
switch(query) {
|
||||
default:
|
||||
rmFile(name, func);
|
||||
break;
|
||||
|
||||
case 'dir':
|
||||
rmDir(name, func);
|
||||
flop.delete(name, func);
|
||||
break;
|
||||
|
||||
case 'files':
|
||||
|
|
@ -35,8 +26,6 @@
|
|||
|
||||
function deleteFiles(from, names, callback) {
|
||||
var name,
|
||||
rmFile = fs.unlink,
|
||||
rmDir = fse.delete,
|
||||
isLast = true;
|
||||
|
||||
isLast = !names.length;
|
||||
|
|
@ -45,26 +34,11 @@
|
|||
if (isLast)
|
||||
callback(null);
|
||||
else
|
||||
Util.exec.if(rimraf, function(result, func) {
|
||||
(func || rimraf)(from + name, function(error) {
|
||||
if (error)
|
||||
callback(error);
|
||||
else
|
||||
deleteFiles(from, names, callback);
|
||||
});
|
||||
}, function(func) {
|
||||
dir.isDir(name, function(error, isDir) {
|
||||
var funcRm;
|
||||
|
||||
if (error)
|
||||
callback(error);
|
||||
else if (isDir)
|
||||
funcRm = rmDir;
|
||||
else
|
||||
funcRm = rmFile;
|
||||
|
||||
func(null, funcRm);
|
||||
});
|
||||
flop.delete(from + name, function(error) {
|
||||
if (error)
|
||||
callback(error);
|
||||
else
|
||||
deleteFiles(from, names, callback);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue