mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
refactor(move-files) move out from rest
This commit is contained in:
parent
d10c024d8d
commit
b269ce95b4
3 changed files with 74 additions and 39 deletions
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const DIR = './';
|
||||
const DIR_COMMON = '../common/';
|
||||
const DIR = '../';
|
||||
const DIR_COMMON = DIR + '../common/';
|
||||
const path = require('path');
|
||||
|
||||
const root = require(DIR + 'root');
|
||||
|
|
@ -12,16 +12,15 @@ const markdown = require(DIR + 'markdown');
|
|||
const jaguar = require('jaguar');
|
||||
const onezip = require('onezip');
|
||||
const inly = require('inly');
|
||||
const pullout = require('pullout/legacy');
|
||||
const currify = require('currify/legacy');
|
||||
const promisify = require('es6-promisify').promisify;
|
||||
const flop = require('flop');
|
||||
const move = promisify(flop.move);
|
||||
const pullout = require('pullout/legacy');
|
||||
const ponse = require('ponse');
|
||||
const copymitter = require('copymitter');
|
||||
const json = require('jonny');
|
||||
const check = require('checkup');
|
||||
|
||||
const moveFiles = require('./move-files');
|
||||
|
||||
const swap = currify((fn, a, b) => fn(b, a));
|
||||
const isWin32 = process.platform === 'win32';
|
||||
|
||||
|
|
@ -311,37 +310,6 @@ function copy(from, to, names, fn) {
|
|||
});
|
||||
}
|
||||
|
||||
function moveFiles(files, callback) {
|
||||
if (!files.names)
|
||||
return move(files.from, files.to)
|
||||
.then(callback)
|
||||
.catch(callback);
|
||||
|
||||
const names = files.names.slice();
|
||||
const copy = () => {
|
||||
const isLast = !names.length;
|
||||
|
||||
if (isLast)
|
||||
return callback(null);
|
||||
|
||||
const name = names.shift();
|
||||
const from = path.join(files.from, name);
|
||||
const to = path.join(files.to, name);
|
||||
|
||||
move(from, to)
|
||||
.then(copy)
|
||||
.catch(callback);
|
||||
};
|
||||
|
||||
check
|
||||
.type('callback', callback, 'function')
|
||||
.check({
|
||||
files,
|
||||
});
|
||||
|
||||
copy();
|
||||
}
|
||||
|
||||
module.exports._isRootWin32 = isRootWin32;
|
||||
function isRootWin32(path) {
|
||||
const isRoot = path === '/';
|
||||
40
server/rest/move-files.js
Normal file
40
server/rest/move-files.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const flop = require('flop');
|
||||
const check = require('checkup');
|
||||
const promisify = require('es6-promisify').promisify;
|
||||
|
||||
const move = promisify(flop.move);
|
||||
|
||||
module.exports = (files, callback) => {
|
||||
if (!files.names)
|
||||
return move(files.from, files.to)
|
||||
.then(callback)
|
||||
.catch(callback);
|
||||
|
||||
const names = files.names.slice();
|
||||
const copy = () => {
|
||||
const isLast = !names.length;
|
||||
|
||||
if (isLast)
|
||||
return callback(null);
|
||||
|
||||
const name = names.shift();
|
||||
const from = path.join(files.from, name);
|
||||
const to = path.join(files.to, name);
|
||||
|
||||
move(from, to)
|
||||
.then(copy)
|
||||
.catch(callback);
|
||||
};
|
||||
|
||||
check
|
||||
.type('callback', callback, 'function')
|
||||
.check({
|
||||
files,
|
||||
});
|
||||
|
||||
copy();
|
||||
};
|
||||
|
||||
|
|
@ -44,8 +44,7 @@ test('cloudcmd: rest: mv', (t) => {
|
|||
t.equal(body, 'move: ok("["mv.txt"]")', 'should move');
|
||||
t.end();
|
||||
|
||||
const file = fs.readFileSync(`${tmp}/mv.txt`);
|
||||
fs.writeFileSync(`${fixtureDir}/mv.txt`, file);
|
||||
fs.renameSync(`${tmp}/mv.txt`, `${fixtureDir}/mv.txt`);
|
||||
|
||||
after();
|
||||
})
|
||||
|
|
@ -54,3 +53,31 @@ test('cloudcmd: rest: mv', (t) => {
|
|||
});
|
||||
});
|
||||
|
||||
test('cloudcmd: rest: mv: rename', (t) => {
|
||||
before({}, (port, after) => {
|
||||
const tmp = join(fixtureDir, 'tmp');
|
||||
const files = {
|
||||
from: '/fixture/mv.txt',
|
||||
to: '/fixture/tmp/mv.txt',
|
||||
};
|
||||
|
||||
mkdirp.sync(tmp);
|
||||
|
||||
const rmTmp = () => rimraf.sync(tmp);
|
||||
|
||||
put(`http://localhost:${port}/api/v1/mv`, files)
|
||||
.then(warp(_pullout, 'string'))
|
||||
.then((body) => {
|
||||
const expected = 'move: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
|
||||
|
||||
t.equal(body, expected, 'should move');
|
||||
t.end();
|
||||
|
||||
fs.renameSync(`${tmp}/mv.txt`, `${fixtureDir}/mv.txt`);
|
||||
|
||||
after();
|
||||
})
|
||||
.catch(console.error)
|
||||
.then(rmTmp);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue