2019-10-09 21:35:13 +02:00
|
|
|
/* eslint no-useless-escape: 0*/
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2024-01-18 13:20:10 +02:00
|
|
|
import { _ } from './locale';
|
|
|
|
import { fileExtension, filename, safeFileExtension } from '@joplin/utils/path';
|
|
|
|
export * from '@joplin/utils/path';
|
2018-05-23 15:25:59 +02:00
|
|
|
|
2021-08-10 20:13:16 +02:00
|
|
|
let friendlySafeFilename_blackListChars = '/\n\r<>:\'"\\|?*#';
|
2018-11-20 02:42:21 +02:00
|
|
|
for (let i = 0; i < 32; i++) {
|
|
|
|
friendlySafeFilename_blackListChars += String.fromCharCode(i);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
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'];
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2021-08-18 00:29:46 +02:00
|
|
|
// The goal of this function is to provide a safe filename, that should work in
|
|
|
|
// any filesystem, but that's still user friendly, in particular because it
|
|
|
|
// supports any charset - Chinese, Russian, etc.
|
|
|
|
//
|
|
|
|
// "Safe" however doesn't mean it can be safely inserted in any content (HTML,
|
|
|
|
// Markdown, etc.) - it still needs to be encoded by the calling code according
|
|
|
|
// to the context.
|
|
|
|
|
2023-06-30 10:11:26 +02:00
|
|
|
export function friendlySafeFilename(e: string, maxLength: number = null, preserveExtension = false) {
|
2020-09-07 23:12:51 +02:00
|
|
|
// Although Windows supports paths up to 255 characters, but that includes the filename and its
|
|
|
|
// parent directory path. Also there's generally no good reason for dir or file names
|
|
|
|
// to be so long, so keep it at 50, which should prevent various errors.
|
|
|
|
if (maxLength === null) maxLength = 50;
|
2018-11-20 02:42:21 +02:00
|
|
|
if (!e || !e.replace) return _('Untitled');
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2021-08-10 20:13:16 +02:00
|
|
|
let fileExt = '';
|
|
|
|
|
|
|
|
if (preserveExtension) {
|
2021-08-17 07:57:00 +02:00
|
|
|
const baseExt = fileExtension(e);
|
|
|
|
fileExt = baseExt ? `.${safeFileExtension(baseExt)}` : '';
|
2021-08-10 20:13:16 +02:00
|
|
|
e = filename(e);
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:42:21 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:36:23 +02:00
|
|
|
while (output.length) {
|
|
|
|
const c = output[0];
|
|
|
|
if (c === ' ') {
|
|
|
|
output = output.substr(1, output.length - 1);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 20:13:16 +02:00
|
|
|
if (!output) return _('Untitled') + fileExt;
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2021-08-10 20:13:16 +02:00
|
|
|
return output.substr(0, maxLength) + fileExt;
|
2018-11-20 02:42:21 +02:00
|
|
|
}
|