2018-11-20 02:42:21 +02:00
|
|
|
const { _ } = require('lib/locale');
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
function dirname(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2017-11-11 00:18:00 +02:00
|
|
|
let s = path.split(/\/|\\/);
|
2017-06-23 23:32:24 +02:00
|
|
|
s.pop();
|
|
|
|
return s.join('/');
|
|
|
|
}
|
|
|
|
|
2017-06-23 20:51:02 +02:00
|
|
|
function basename(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2017-11-11 00:18:00 +02:00
|
|
|
let s = path.split(/\/|\\/);
|
2017-06-23 20:51:02 +02:00
|
|
|
return s[s.length - 1];
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:42:21 +02:00
|
|
|
function filename(path, includeDir = false) {
|
2017-06-25 01:19:11 +02:00
|
|
|
if (!path) throw new Error('Path is empty');
|
2018-11-20 02:42:21 +02:00
|
|
|
let output = includeDir ? path : basename(path);
|
2017-06-25 01:19:11 +02:00
|
|
|
if (output.indexOf('.') < 0) return output;
|
|
|
|
|
|
|
|
output = output.split('.');
|
|
|
|
output.pop();
|
|
|
|
return output.join('.');
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:17:03 +02:00
|
|
|
function fileExtension(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
|
|
|
|
|
|
|
let output = path.split('.');
|
|
|
|
if (output.length <= 1) return '';
|
|
|
|
return output[output.length - 1];
|
|
|
|
}
|
|
|
|
|
2017-06-23 20:51:02 +02:00
|
|
|
function isHidden(path) {
|
|
|
|
let b = basename(path);
|
|
|
|
if (!b.length) throw new Error('Path empty or not a valid path: ' + path);
|
|
|
|
return b[0] === '.';
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:15:49 +02:00
|
|
|
function safeFileExtension(e) {
|
|
|
|
if (!e || !e.replace) return '';
|
|
|
|
return e.replace(/[^a-zA-Z0-9]/g, '')
|
|
|
|
}
|
|
|
|
|
2018-09-04 12:59:09 +02:00
|
|
|
function safeFilename(e, maxLength = null, allowSpaces = false) {
|
|
|
|
if (maxLength === null) maxLength = 32;
|
2018-05-23 15:25:59 +02:00
|
|
|
if (!e || !e.replace) return '';
|
2018-09-04 12:59:09 +02:00
|
|
|
const regex = allowSpaces ? /[^a-zA-Z0-9\-_\(\)\. ]/g : /[^a-zA-Z0-9\-_\(\)\.]/g
|
|
|
|
let output = e.replace(regex, '_')
|
2018-05-23 15:25:59 +02:00
|
|
|
return output.substr(0, maxLength);
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:42:21 +02:00
|
|
|
let friendlySafeFilename_blackListChars = '/<>:\'"\\|?*';
|
|
|
|
for (let i = 0; i < 32; i++) {
|
|
|
|
friendlySafeFilename_blackListChars += String.fromCharCode(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
const friendlySafeFilename_blackListNames = [".", "..", "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"];
|
|
|
|
|
|
|
|
function friendlySafeFilename(e, maxLength = null) {
|
|
|
|
if (maxLength === null) maxLength = 255;
|
|
|
|
if (!e || !e.replace) return _('Untitled');
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
for (let i = 0; i < e.length; i++) {
|
|
|
|
const c = e[i];
|
|
|
|
if (friendlySafeFilename_blackListChars.indexOf(c) >= 0) {
|
|
|
|
output += '_';
|
|
|
|
} else {
|
|
|
|
output += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.length <= 4) {
|
|
|
|
if (friendlySafeFilename_blackListNames.indexOf(output.toUpperCase()) >= 0) {
|
|
|
|
output = '___';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (output.length) {
|
|
|
|
const c = output[output.length - 1];
|
|
|
|
if (c === ' ' || c === '.') {
|
|
|
|
output = output.substr(0, output.length - 1);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output) return _('Untitled');
|
|
|
|
|
|
|
|
return output.substr(0, maxLength);
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:46:04 +02:00
|
|
|
function toSystemSlashes(path, os = null) {
|
|
|
|
if (os === null) os = process.platform;
|
2017-12-08 23:51:59 +02:00
|
|
|
if (os === 'win32') return path.replace(/\//g, "\\");
|
|
|
|
return path.replace(/\\/g, "/");
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:01:14 +02:00
|
|
|
function rtrimSlashes(path) {
|
2018-05-08 12:29:25 +02:00
|
|
|
return path.replace(/[\/\\]+$/, '');
|
2018-01-25 21:01:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ltrimSlashes(path) {
|
|
|
|
return path.replace(/^\/+/, '');
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:42:21 +02:00
|
|
|
module.exports = { basename, dirname, filename, isHidden, fileExtension, safeFilename, friendlySafeFilename, safeFileExtension, toSystemSlashes, rtrimSlashes, ltrimSlashes };
|