mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-30 19:52:28 +08:00
Merge pull request #879 from pano9000/refactor_utils_formatDownloadTitle
refactor(utils/formatDownloadTitle): simplify function
This commit is contained in:
commit
2c469283f0
129
spec-es6/utils/formatDownloadTitle.spec.ts
Normal file
129
spec-es6/utils/formatDownloadTitle.spec.ts
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
import { formatDownloadTitle } from "../../src/services/utils.ts";
|
||||||
|
import { describe, it, execute, expect } from "../mini_test.ts";
|
||||||
|
|
||||||
|
const testCases: [fnValue: Parameters<typeof formatDownloadTitle>, expectedValue: ReturnType<typeof formatDownloadTitle>][] = [
|
||||||
|
// empty fileName tests
|
||||||
|
[
|
||||||
|
["", "text", ""],
|
||||||
|
"untitled.html"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["", "canvas", ""],
|
||||||
|
"untitled.json"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["", null, ""],
|
||||||
|
"untitled"
|
||||||
|
],
|
||||||
|
|
||||||
|
// json extension from type tests
|
||||||
|
[
|
||||||
|
["test_file", "canvas", ""],
|
||||||
|
"test_file.json"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file", "relationMap", ""],
|
||||||
|
"test_file.json"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file", "search", ""],
|
||||||
|
"test_file.json"
|
||||||
|
],
|
||||||
|
|
||||||
|
// extension based on mime type
|
||||||
|
[
|
||||||
|
["test_file", null, "text/csv"],
|
||||||
|
"test_file.csv"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file_wo_ext", "image", "image/svg+xml"],
|
||||||
|
"test_file_wo_ext.svg"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file_wo_ext", "file", "application/json"],
|
||||||
|
"test_file_wo_ext.json"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file_w_fake_ext.ext", "image", "image/svg+xml"],
|
||||||
|
"test_file_w_fake_ext.ext.svg"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file_w_correct_ext.svg", "image", "image/svg+xml"],
|
||||||
|
"test_file_w_correct_ext.svg"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file_w_correct_ext.svgz", "image", "image/svg+xml"],
|
||||||
|
"test_file_w_correct_ext.svgz"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file.zip", "file", "application/zip"],
|
||||||
|
"test_file.zip"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file", "file", "application/zip"],
|
||||||
|
"test_file.zip"
|
||||||
|
],
|
||||||
|
|
||||||
|
// application/octet-stream tests
|
||||||
|
[
|
||||||
|
["test_file", "file", "application/octet-stream"],
|
||||||
|
"test_file"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file.zip", "file", "application/octet-stream"],
|
||||||
|
"test_file.zip"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test_file.unknown", null, "application/octet-stream"],
|
||||||
|
"test_file.unknown"
|
||||||
|
],
|
||||||
|
|
||||||
|
// sanitized filename tests
|
||||||
|
[
|
||||||
|
["test/file", null, "application/octet-stream"],
|
||||||
|
"testfile"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
["test:file.zip", "file", "application/zip"],
|
||||||
|
"testfile.zip"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
[":::", "file", "application/zip"],
|
||||||
|
".zip"
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
[":::a", "file", "application/zip"],
|
||||||
|
"a.zip"
|
||||||
|
],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
describe("utils/formatDownloadTitle unit tests", () => {
|
||||||
|
|
||||||
|
testCases.forEach(testCase => {
|
||||||
|
return it(`With args '${JSON.stringify(testCase[0])}' it should return '${testCase[1]}'`, () => {
|
||||||
|
const [value, expected] = testCase;
|
||||||
|
const actual = formatDownloadTitle(...value);
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
execute()
|
@ -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