Notes/dump-db/inc/extension.ts

37 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-10 18:23:49 +02:00
import path from "path";
import mimeTypes from "mime-types";
2022-02-12 22:20:15 +01:00
2024-08-10 18:23:49 +02:00
function getFileName(note: any, childTargetPath: string, safeTitle: string) {
2022-02-12 22:20:15 +01:00
let existingExtension = path.extname(safeTitle).toLowerCase();
let newExtension;
2025-01-09 18:07:02 +02:00
if (note.type === "text") {
newExtension = "html";
} else if (note.mime === "application/x-javascript" || note.mime === "text/javascript") {
newExtension = "js";
} else if (existingExtension.length > 0) {
// if the page already has an extension, then we'll just keep it
2022-02-12 22:20:15 +01:00
newExtension = null;
} else {
2025-01-09 18:07:02 +02:00
if (note.mime?.toLowerCase()?.trim() === "image/jpg") {
// image/jpg is invalid but pretty common
newExtension = "jpg";
2022-02-12 22:20:15 +01:00
} else {
newExtension = mimeTypes.extension(note.mime) || "dat";
}
}
let fileNameWithPath = childTargetPath;
// if the note is already named with extension (e.g. "jquery"), then it's silly to append exact same extension again
if (newExtension && existingExtension !== "." + newExtension.toLowerCase()) {
fileNameWithPath += "." + newExtension;
}
return fileNameWithPath;
}
2024-08-10 18:23:49 +02:00
export default {
2022-02-12 22:20:15 +01:00
getFileName
};