feature(cloudcmd) rest: add rename

This commit is contained in:
coderaiser 2020-08-10 13:36:41 +03:00
parent d8dd9f5738
commit 28ceb7f7ff
8 changed files with 166 additions and 60 deletions

View file

@ -22,6 +22,7 @@
"Iptables",
"JitSu",
"Node",
"IO",
"Olena",
"TarZak",
"Termux",

View file

@ -542,6 +542,10 @@ Here you can find `API` that can be used in **User Menu**. **DOM** and **CloudCm
- `TerminalRun` - module that shows `Terminal` with a `command` from options and closes terminal when everything is done.
**IO** Files API
- `rename(from, to)` - rename `from` into `to`
### Distribute
Being able to configure Cloud Commander remotely opens the doors to using it as microservice, and that's what the "distribute" options set out to do.

View file

@ -96,6 +96,18 @@ module.exports.mv = async (data) => {
});
};
module.exports.rename = async (from, to) => {
return await sendRequest({
method: 'PUT',
url: '/rename',
data: {
from,
to,
},
imgPosition,
});
};
module.exports.Config = {
read: async () => {
return await sendRequest({

View file

@ -46,12 +46,11 @@ module.exports = async (current) => {
return;
const dirPath = getCurrentDirPath();
const files = {
from : dirPath + from,
to : dirPath + to,
};
const [e] = await RESTful.mv(files);
const fromFull = `${dirPath}${from}`;
const toFull = `${dirPath}${to}`;
const [e] = await RESTful.rename(fromFull, toFull);
if (e)
return;
@ -61,3 +60,4 @@ module.exports = async (current) => {
Storage.remove(dirPath);
CloudCmd.refresh();
};

View file

@ -30,6 +30,7 @@ module.exports.cp = handleError(IO.cp);
module.exports.pack = handleError(IO.pack);
module.exports.extract = handleError(IO.extract);
module.exports.mv = handleError(IO.mv);
module.exports.rename = handleError(IO.rename);
module.exports.Config = {
read: handleError(IO.Config.read),

View file

@ -168,9 +168,16 @@ function getCMD(cmd) {
return cmd;
}
const getMoveMsg = (files) => {
const data = !files.names ? files : files.names.slice();
const msg = formatMsg('move', data);
const getMoveMsg = (names) => {
const msg = formatMsg('move', names);
return msg;
};
const getRenameMsg = (from, to) => {
const msg = formatMsg('rename', {
from,
to,
});
return msg;
};
@ -200,19 +207,19 @@ function onPUT({name, config, body}, callback) {
if (isRootAll(rootDir, [to, from]))
return callback(getWin32RootMsg());
const msg = getMoveMsg(files);
const msg = getMoveMsg(names);
const fn = swap(callback, msg);
const fromRooted = root(from, rootDir);
const toRooted = root(to, rootDir);
if (!names)
return fs.rename(fromRooted, toRooted, fn);
return moveFiles(fromRooted, toRooted, names)
.on('error', fn)
.on('end', fn);
} case 'cp':
} case 'rename':
return rename(rootDir, files.from, files.to, callback);
case 'cp':
if (!files.from || !files.names || !files.to)
return callback(body);
@ -249,6 +256,22 @@ function onPUT({name, config, body}, callback) {
}
}
function rename(rootDir, from, to, callback) {
if (!from)
return callback(UserError('"from" should be filled'));
if (!to)
return callback(UserError('"to" should be filled'));
const msg = getRenameMsg(from, to);
const fn = swap(callback, msg);
const fromRooted = root(from, rootDir);
const toRooted = root(to, rootDir);
return fs.rename(fromRooted, toRooted, fn);
}
function pack(from, to, names, config, fn) {
const rootDir = config('root');
const packer = config('packer');
@ -366,7 +389,6 @@ module.exports._formatMsg = formatMsg;
function formatMsg(msg, data, status) {
const value = parseData(data);
return CloudFunc.formatMsg(msg, value, status);
}

View file

@ -66,52 +66,6 @@ test('cloudcmd: rest: mv', async (t) => {
t.end();
});
test('cloudcmd: rest: mv: rename', async (t) => {
const volume = {
'/fixture/mv.txt': 'hello',
'/fixture/tmp/a.txt': 'a',
};
const vol = Volume.fromJSON(volume, '/');
const unionFS = ufs
.use(vol)
.use(fs);
mockRequire('fs', unionFS);
reRequire('@cloudcmd/rename-files');
reRequire('@cloudcmd/move-files');
reRequire(restPath);
const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd;
const configManager = createConfigManager();
configManager('auth', false);
configManager('root', '/');
const {request} = serveOnce(cloudcmd, {
configManager,
});
const files = {
from: '/fixture/mv.txt',
to: '/fixture/tmp/mv.txt',
};
const {body} = await request.put(`/api/v1/mv`, {
body: files,
});
mockRequire.stop('fs');
const expected = 'move: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
t.equal(body, expected, 'should move');
t.end();
});
test('cloudcmd: rest: mv: no from', async (t) => {
const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd;

112
test/rest/rename.js Normal file
View file

@ -0,0 +1,112 @@
'use strict';
const fs = require('fs');
const test = require('supertape');
const {Volume} = require('memfs');
const {ufs} = require('unionfs');
const mockRequire = require('mock-require');
const {reRequire} = mockRequire;
const serveOnce = require('serve-once');
const cloudcmdPath = '../../';
const dir = cloudcmdPath + 'server/';
const restPath = dir + 'rest';
test('cloudcmd: rest: rename', async (t) => {
const volume = {
'/fixture/mv.txt': 'hello',
'/fixture/tmp/a.txt': 'a',
};
const vol = Volume.fromJSON(volume, '/');
const unionFS = ufs
.use(vol)
.use(fs);
mockRequire('fs', unionFS);
reRequire('@cloudcmd/rename-files');
reRequire('@cloudcmd/move-files');
reRequire(restPath);
const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd;
const configManager = createConfigManager();
configManager('auth', false);
configManager('root', '/');
const {request} = serveOnce(cloudcmd, {
configManager,
});
const files = {
from: '/fixture/mv.txt',
to: '/fixture/tmp/mv.txt',
};
const {body} = await request.put(`/api/v1/rename`, {
body: files,
});
mockRequire.stop('fs');
const expected = 'rename: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
t.equal(body, expected, 'should move');
t.end();
});
test('cloudcmd: rest: rename: no from', async (t) => {
const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd;
const configManager = createConfigManager();
configManager('auth', false);
configManager('root', '/');
const {request} = serveOnce(cloudcmd, {
configManager,
});
const files = {};
const {body} = await request.put(`/api/v1/rename`, {
body: files,
});
const expected = '"from" should be filled';
t.equal(body, expected);
t.end();
});
test('cloudcmd: rest: rename: no to', async (t) => {
const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd;
const configManager = createConfigManager();
configManager('auth', false);
configManager('root', '/');
const {request} = serveOnce(cloudcmd, {
configManager,
});
const files = {
from: '/',
};
const {body} = await request.put(`/api/v1/rename`, {
body: files,
});
const expected = '"to" should be filled';
t.equal(body, expected);
t.end();
});