2018-11-26 23:47:02 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import BNote from "../../becca/entities/bnote.js";
|
|
|
|
|
import TaskContext from "../task_context.js";
|
2024-02-25 14:52:20 +02:00
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import noteService from "../../services/notes.js";
|
|
|
|
|
import imageService from "../../services/image.js";
|
|
|
|
|
import protectedSessionService from "../protected_session.js";
|
|
|
|
|
import markdownService from "./markdown.js";
|
|
|
|
|
import mimeService from "./mime.js";
|
|
|
|
|
import utils from "../../services/utils.js";
|
|
|
|
|
import importUtils from "./utils.js";
|
|
|
|
|
import htmlSanitizer from "../html_sanitizer.js";
|
2024-04-13 17:30:48 +03:00
|
|
|
import { File } from "./common";
|
2024-02-25 15:06:43 +02:00
|
|
|
|
|
|
|
|
function importSingleFile(taskContext: TaskContext, file: File, parentNote: BNote) {
|
2019-10-06 18:28:53 +02:00
|
|
|
const mime = mimeService.getMime(file.originalname) || file.mimetype;
|
2019-03-03 00:25:31 +01:00
|
|
|
|
2024-02-25 14:52:20 +02:00
|
|
|
if (taskContext?.data?.textImportedAsText) {
|
2019-03-03 00:25:31 +01:00
|
|
|
if (mime === 'text/html') {
|
2020-06-20 12:31:38 +02:00
|
|
|
return importHtml(taskContext, file, parentNote);
|
2019-03-03 00:25:31 +01:00
|
|
|
} else if (['text/markdown', 'text/x-markdown'].includes(mime)) {
|
2020-06-20 12:31:38 +02:00
|
|
|
return importMarkdown(taskContext, file, parentNote);
|
2019-03-03 00:25:31 +01:00
|
|
|
} else if (mime === 'text/plain') {
|
2020-06-20 12:31:38 +02:00
|
|
|
return importPlainText(taskContext, file, parentNote);
|
2019-02-24 13:10:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-25 21:22:57 +01:00
|
|
|
|
2024-02-25 14:52:20 +02:00
|
|
|
if (taskContext?.data?.codeImportedAsCode && mimeService.getType(taskContext.data, mime) === 'code') {
|
2020-06-20 12:31:38 +02:00
|
|
|
return importCodeNote(taskContext, file, parentNote);
|
2019-02-25 21:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-24 21:47:50 +02:00
|
|
|
if (mime.startsWith("image/")) {
|
2020-06-20 12:31:38 +02:00
|
|
|
return importImage(file, parentNote, taskContext);
|
2019-02-25 21:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
return importFile(taskContext, file, parentNote);
|
2019-02-24 13:10:47 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importImage(file: File, parentNote: BNote, taskContext: TaskContext) {
|
|
|
|
|
if (typeof file.buffer === "string") {
|
|
|
|
|
throw new Error("Invalid file content for image.");
|
|
|
|
|
}
|
|
|
|
|
const {note} = imageService.saveImage(parentNote.noteId, file.buffer, file.originalname, !!taskContext.data?.shrinkImages);
|
2019-02-25 21:22:57 +01:00
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-25 21:22:57 +01:00
|
|
|
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importFile(taskContext: TaskContext, file: File, parentNote: BNote) {
|
2019-02-25 21:22:57 +01:00
|
|
|
const originalName = file.originalname;
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const {note} = noteService.createNewNote({
|
2019-11-16 11:09:52 +01:00
|
|
|
parentNoteId: parentNote.noteId,
|
|
|
|
|
title: originalName,
|
|
|
|
|
content: file.buffer,
|
2019-02-25 21:22:57 +01:00
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
|
|
|
|
type: 'file',
|
2019-11-16 11:09:52 +01:00
|
|
|
mime: mimeService.getMime(originalName) || file.mimetype
|
2019-02-25 21:22:57 +01:00
|
|
|
});
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
note.addLabel("originalFileName", originalName);
|
2019-11-16 11:09:52 +01:00
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-24 13:10:47 +01:00
|
|
|
|
2019-02-25 21:22:57 +01:00
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importCodeNote(taskContext: TaskContext, file: File, parentNote: BNote) {
|
|
|
|
|
const title = utils.getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);
|
2023-05-06 22:50:28 +02:00
|
|
|
const content = file.buffer.toString("utf-8");
|
2019-10-06 18:28:53 +02:00
|
|
|
const detectedMime = mimeService.getMime(file.originalname) || file.mimetype;
|
2019-09-30 20:22:09 +02:00
|
|
|
const mime = mimeService.normalizeMimeType(detectedMime);
|
2019-02-25 21:22:57 +01:00
|
|
|
|
2023-05-05 23:17:23 +02:00
|
|
|
const { note } = noteService.createNewNote({
|
2019-11-16 11:09:52 +01:00
|
|
|
parentNoteId: parentNote.noteId,
|
|
|
|
|
title,
|
|
|
|
|
content,
|
2019-02-25 21:22:57 +01:00
|
|
|
type: 'code',
|
|
|
|
|
mime: mime,
|
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-25 21:22:57 +01:00
|
|
|
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importPlainText(taskContext: TaskContext, file: File, parentNote: BNote) {
|
|
|
|
|
const title = utils.getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);
|
2023-05-06 22:50:28 +02:00
|
|
|
const plainTextContent = file.buffer.toString("utf-8");
|
2019-02-24 13:10:47 +01:00
|
|
|
const htmlContent = convertTextToHtml(plainTextContent);
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const {note} = noteService.createNewNote({
|
2019-11-16 11:09:52 +01:00
|
|
|
parentNoteId: parentNote.noteId,
|
|
|
|
|
title,
|
|
|
|
|
content: htmlContent,
|
2019-02-24 13:10:47 +01:00
|
|
|
type: 'text',
|
2019-02-25 21:22:57 +01:00
|
|
|
mime: 'text/html',
|
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
2019-02-24 13:10:47 +01:00
|
|
|
});
|
|
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-24 13:10:47 +01:00
|
|
|
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 14:52:20 +02:00
|
|
|
function convertTextToHtml(text: string) {
|
2019-02-24 13:10:47 +01:00
|
|
|
// 1: Plain Text Search
|
|
|
|
|
text = text.replace(/&/g, "&").
|
|
|
|
|
replace(/</g, "<").
|
|
|
|
|
replace(/>/g, ">");
|
|
|
|
|
|
|
|
|
|
// 2: Line Breaks
|
|
|
|
|
text = text.replace(/\r\n?|\n/g, "<br>");
|
|
|
|
|
|
|
|
|
|
// 3: Paragraphs
|
|
|
|
|
text = text.replace(/<br>\s*<br>/g, "</p><p>");
|
|
|
|
|
|
|
|
|
|
// 4: Wrap in Paragraph Tags
|
2022-12-21 15:19:05 +01:00
|
|
|
text = `<p>${text}</p>`;
|
2019-02-24 13:10:47 +01:00
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importMarkdown(taskContext: TaskContext, file: File, parentNote: BNote) {
|
|
|
|
|
const title = utils.getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);
|
2020-11-01 20:38:39 +01:00
|
|
|
|
2023-05-06 22:50:28 +02:00
|
|
|
const markdownContent = file.buffer.toString("utf-8");
|
2023-10-21 17:54:07 +02:00
|
|
|
let htmlContent = markdownService.renderToHtml(markdownContent, title);
|
|
|
|
|
|
2024-02-25 14:52:20 +02:00
|
|
|
if (taskContext.data?.safeImport) {
|
2023-10-21 17:54:07 +02:00
|
|
|
htmlContent = htmlSanitizer.sanitize(htmlContent);
|
|
|
|
|
}
|
2018-11-26 23:47:02 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const {note} = noteService.createNewNote({
|
2019-11-16 11:09:52 +01:00
|
|
|
parentNoteId: parentNote.noteId,
|
|
|
|
|
title,
|
|
|
|
|
content: htmlContent,
|
2018-11-26 23:47:02 +01:00
|
|
|
type: 'text',
|
2019-02-25 21:22:57 +01:00
|
|
|
mime: 'text/html',
|
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
2018-11-26 23:47:02 +01:00
|
|
|
});
|
|
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2018-11-26 23:47:02 +01:00
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importHtml(taskContext: TaskContext, file: File, parentNote: BNote) {
|
|
|
|
|
const title = utils.getNoteTitle(file.originalname, !!taskContext.data?.replaceUnderscoresWithSpaces);
|
2023-05-06 22:50:28 +02:00
|
|
|
let content = file.buffer.toString("utf-8");
|
2020-06-30 23:37:06 +02:00
|
|
|
|
2024-02-25 14:52:20 +02:00
|
|
|
if (taskContext?.data?.safeImport) {
|
2023-10-21 17:54:07 +02:00
|
|
|
content = htmlSanitizer.sanitize(content);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 10:31:50 +02:00
|
|
|
content = importUtils.handleH1(content, title);
|
2020-11-01 20:38:39 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const {note} = noteService.createNewNote({
|
2019-11-16 11:09:52 +01:00
|
|
|
parentNoteId: parentNote.noteId,
|
|
|
|
|
title,
|
|
|
|
|
content,
|
2018-11-26 23:47:02 +01:00
|
|
|
type: 'text',
|
2019-02-25 21:22:57 +01:00
|
|
|
mime: 'text/html',
|
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
2018-11-26 23:47:02 +01:00
|
|
|
});
|
|
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2018-11-26 23:47:02 +01:00
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
function importAttachment(taskContext: TaskContext, file: File, parentNote: BNote) {
|
2023-05-09 00:05:27 +02:00
|
|
|
const mime = mimeService.getMime(file.originalname) || file.mimetype;
|
|
|
|
|
|
2024-02-25 15:06:43 +02:00
|
|
|
if (mime.startsWith("image/") && typeof file.buffer !== "string") {
|
2024-02-25 14:52:20 +02:00
|
|
|
imageService.saveImageToAttachment(parentNote.noteId, file.buffer, file.originalname, taskContext.data?.shrinkImages);
|
2023-05-09 00:05:27 +02:00
|
|
|
|
|
|
|
|
taskContext.increaseProgressCount();
|
|
|
|
|
} else {
|
|
|
|
|
parentNote.saveAttachment({
|
|
|
|
|
title: file.originalname,
|
|
|
|
|
content: file.buffer,
|
|
|
|
|
role: 'file',
|
|
|
|
|
mime: mime
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
taskContext.increaseProgressCount();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 14:52:20 +02:00
|
|
|
export = {
|
2023-05-09 00:05:27 +02:00
|
|
|
importSingleFile,
|
|
|
|
|
importAttachment
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|