diff --git a/server/rest.js b/server/rest/index.js similarity index 89% rename from server/rest.js rename to server/rest/index.js index c39dd4af..4d6404d2 100644 --- a/server/rest.js +++ b/server/rest/index.js @@ -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 === '/'; diff --git a/server/rest/move-files.js b/server/rest/move-files.js new file mode 100644 index 00000000..34136c39 --- /dev/null +++ b/server/rest/move-files.js @@ -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(); +}; + diff --git a/test/rest/mv.js b/test/rest/mv.js index 150ef550..33431be1 100644 --- a/test/rest/mv.js +++ b/test/rest/mv.js @@ -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); + }); +});