2017-01-24 06:32:40 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2017-03-09 05:37:02 +00:00
|
|
|
const EnigAssert = require('./enigma_assert.js');
|
2017-01-24 06:32:40 +00:00
|
|
|
|
|
|
|
// deps
|
2017-03-09 05:37:02 +00:00
|
|
|
const fse = require('fs-extra');
|
|
|
|
const paths = require('path');
|
|
|
|
const async = require('async');
|
2017-01-24 06:32:40 +00:00
|
|
|
|
|
|
|
exports.moveFileWithCollisionHandling = moveFileWithCollisionHandling;
|
2017-03-09 05:37:02 +00:00
|
|
|
exports.copyFileWithCollisionHandling = copyFileWithCollisionHandling;
|
2017-01-24 06:32:40 +00:00
|
|
|
exports.pathWithTerminatingSeparator = pathWithTerminatingSeparator;
|
|
|
|
|
2017-03-09 05:37:02 +00:00
|
|
|
function moveOrCopyFileWithCollisionHandling(src, dst, operation, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
operation = operation || 'copy';
|
|
|
|
const dstPath = paths.dirname(dst);
|
|
|
|
const dstFileExt = paths.extname(dst);
|
|
|
|
const dstFileSuffix = paths.basename(dst, dstFileExt);
|
2017-01-24 06:32:40 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
EnigAssert('move' === operation || 'copy' === operation);
|
2017-03-09 05:37:02 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
let renameIndex = 0;
|
|
|
|
let opOk = false;
|
|
|
|
let tryDstPath;
|
2017-01-24 06:32:40 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
function tryOperation(src, dst, callback) {
|
|
|
|
if('move' === operation) {
|
|
|
|
fse.move(src, tryDstPath, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
} else if('copy' === operation) {
|
|
|
|
fse.copy(src, tryDstPath, { overwrite : false, errorOnExist : true }, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-09 05:37:02 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.until(
|
|
|
|
() => opOk, // until moved OK
|
|
|
|
(cb) => {
|
|
|
|
if(0 === renameIndex) {
|
|
|
|
// try originally supplied path first
|
|
|
|
tryDstPath = dst;
|
|
|
|
} else {
|
|
|
|
tryDstPath = paths.join(dstPath, `${dstFileSuffix}(${renameIndex})${dstFileExt}`);
|
|
|
|
}
|
2017-01-24 06:32:40 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
tryOperation(src, tryDstPath, err => {
|
|
|
|
if(err) {
|
|
|
|
// for some reason fs-extra copy doesn't pass err.code
|
|
|
|
// :TODO: this is dangerous: submit a PR to fs-extra to set EEXIST
|
|
|
|
if('EEXIST' === err.code || 'copy' === operation) {
|
|
|
|
renameIndex += 1;
|
|
|
|
return cb(null); // keep trying
|
|
|
|
}
|
2017-01-24 06:32:40 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2017-01-24 06:32:40 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
opOk = true;
|
|
|
|
return cb(null, tryDstPath);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(err, finalPath) => {
|
|
|
|
return cb(err, finalPath);
|
|
|
|
}
|
|
|
|
);
|
2017-01-24 06:32:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 05:37:02 +00:00
|
|
|
//
|
2018-01-15 19:22:11 +00:00
|
|
|
// Move |src| -> |dst| renaming to file(1).ext, file(2).ext, etc.
|
2017-03-09 05:37:02 +00:00
|
|
|
// in the case of collisions.
|
|
|
|
//
|
|
|
|
function moveFileWithCollisionHandling(src, dst, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return moveOrCopyFileWithCollisionHandling(src, dst, 'move', cb);
|
2017-03-09 05:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function copyFileWithCollisionHandling(src, dst, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return moveOrCopyFileWithCollisionHandling(src, dst, 'copy', cb);
|
2017-03-09 05:37:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 06:32:40 +00:00
|
|
|
function pathWithTerminatingSeparator(path) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(path && paths.sep !== path.charAt(path.length - 1)) {
|
|
|
|
path = path + paths.sep;
|
|
|
|
}
|
|
|
|
return path;
|
2017-01-24 06:32:40 +00:00
|
|
|
}
|