1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-23 22:36:32 +02:00

Server: Resolves #5286: Set better filename and mime type for files downloaded via published notes

This commit is contained in:
Laurent Cozic
2021-08-10 19:13:16 +01:00
parent 4b5318c6d0
commit 77cdd3467d
4 changed files with 53 additions and 14 deletions

View File

@@ -58,20 +58,27 @@ export function safeFilename(e: string, maxLength: number = null, allowSpaces: b
return output.substr(0, maxLength);
}
let friendlySafeFilename_blackListChars = '/<>:\'"\\|?*#';
let friendlySafeFilename_blackListChars = '/\n\r<>:\'"\\|?*#';
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'];
export function friendlySafeFilename(e: string, maxLength: number = null) {
export function friendlySafeFilename(e: string, maxLength: number = null, preserveExtension: boolean = false) {
// 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;
if (!e || !e.replace) return _('Untitled');
let fileExt = '';
if (preserveExtension) {
fileExt = `.${safeFileExtension(fileExtension(e))}`;
e = filename(e);
}
let output = '';
for (let i = 0; i < e.length; i++) {
const c = e[i];
@@ -106,9 +113,9 @@ export function friendlySafeFilename(e: string, maxLength: number = null) {
}
}
if (!output) return _('Untitled');
if (!output) return _('Untitled') + fileExt;
return output.substr(0, maxLength);
return output.substr(0, maxLength) + fileExt;
}
export function toFileProtocolPath(filePathEncode: string, os: string = null) {