mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 11:02:28 +08:00
refactor(formatDownloadTitle): simplify function
I've kept the "extension determination process" in a nested function, that reuses the formatDownloadTitle arguments, however it could also be refactored into an own util function later, if it is ever required. The for loop got replaced by the built functions in `mimeType`
This commit is contained in:
parent
d173daa14e
commit
6da656cd67
@ -178,45 +178,29 @@ export function replaceAll(string: string, replaceWhat: string, replaceWith: str
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function formatDownloadTitle(fileName: string, type: string | null, mime: string) {
|
export function formatDownloadTitle(fileName: string, type: string | null, mime: string) {
|
||||||
if (!fileName) {
|
const fileNameBase = (!fileName) ? "untitled" : sanitize(fileName);
|
||||||
fileName = "untitled";
|
|
||||||
}
|
|
||||||
|
|
||||||
fileName = sanitize(fileName);
|
const getExtension = () => {
|
||||||
|
if (type === "text") return ".html";
|
||||||
|
if (type === "relationMap" || type === "canvas" || type === "search") return ".json";
|
||||||
|
if (!mime) return "";
|
||||||
|
|
||||||
if (type === 'text') {
|
const mimeLc = mime.toLowerCase();
|
||||||
return `${fileName}.html`;
|
|
||||||
} else if (type && ['relationMap', 'canvas', 'search'].includes(type)) {
|
|
||||||
return `${fileName}.json`;
|
|
||||||
} else {
|
|
||||||
if (!mime) {
|
|
||||||
return fileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
mime = mime.toLowerCase();
|
// better to just return the current name without a fake extension
|
||||||
const filenameLc = fileName.toLowerCase();
|
// it's possible that the title still preserves the correct extension anyways
|
||||||
const extensions = mimeTypes.extensions[mime];
|
if (mimeLc === 'application/octet-stream') return "";
|
||||||
|
|
||||||
if (!extensions || extensions.length === 0) {
|
// if fileName has an extension matching the mime already - reuse it
|
||||||
return fileName;
|
const mimeTypeFromFileName = mimeTypes.lookup(fileName);
|
||||||
}
|
if (mimeTypeFromFileName === mimeLc) return "";
|
||||||
|
|
||||||
for (const ext of extensions) {
|
// as last resort try to get extension from mimeType
|
||||||
if (filenameLc.endsWith(`.${ext}`)) {
|
const extensions = mimeTypes.extension(mime);
|
||||||
return fileName;
|
return extensions ? `.${extensions}` : "";
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
if (mime === 'application/octet-stream') {
|
return `${fileNameBase}${getExtension()}`;
|
||||||
// we didn't find any good guess for this one, it will be better to just return
|
|
||||||
// the current name without a fake extension. It's possible that the title still preserves the correct
|
|
||||||
// extension too
|
|
||||||
|
|
||||||
return fileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
return `${fileName}.${extensions[0]}`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeTextFileExtension(filePath: string) {
|
export function removeTextFileExtension(filePath: string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user