From 28ceb7f7ff71792b289b559a8067a3b91bf62492 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 10 Aug 2020 13:36:41 +0300 Subject: [PATCH] feature(cloudcmd) rest: add rename --- .yaspellerrc | 1 + HELP.md | 4 + client/dom/io.js | 12 +++ client/dom/operations/rename-current.js | 10 +-- client/dom/rest.js | 1 + server/rest/index.js | 40 +++++++-- test/rest/mv.js | 46 ---------- test/rest/rename.js | 112 ++++++++++++++++++++++++ 8 files changed, 166 insertions(+), 60 deletions(-) create mode 100644 test/rest/rename.js diff --git a/.yaspellerrc b/.yaspellerrc index ad0eddc1..12acc336 100644 --- a/.yaspellerrc +++ b/.yaspellerrc @@ -22,6 +22,7 @@ "Iptables", "JitSu", "Node", + "IO", "Olena", "TarZak", "Termux", diff --git a/HELP.md b/HELP.md index 99362aa5..01ff3447 100644 --- a/HELP.md +++ b/HELP.md @@ -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. diff --git a/client/dom/io.js b/client/dom/io.js index bd311192..a82e312f 100644 --- a/client/dom/io.js +++ b/client/dom/io.js @@ -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({ diff --git a/client/dom/operations/rename-current.js b/client/dom/operations/rename-current.js index 401a4378..aa01bf02 100644 --- a/client/dom/operations/rename-current.js +++ b/client/dom/operations/rename-current.js @@ -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(); }; + diff --git a/client/dom/rest.js b/client/dom/rest.js index 53fe28d8..810a16bd 100644 --- a/client/dom/rest.js +++ b/client/dom/rest.js @@ -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), diff --git a/server/rest/index.js b/server/rest/index.js index bfe46d16..d940acf5 100644 --- a/server/rest/index.js +++ b/server/rest/index.js @@ -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); } diff --git a/test/rest/mv.js b/test/rest/mv.js index 06b0200d..8329b9cf 100644 --- a/test/rest/mv.js +++ b/test/rest/mv.js @@ -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; diff --git a/test/rest/rename.js b/test/rest/rename.js new file mode 100644 index 00000000..cdc7454f --- /dev/null +++ b/test/rest/rename.js @@ -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(); +}); +