Notes/src/services/import/single.js

169 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-11-26 23:47:02 +01:00
"use strict";
const noteService = require('../../services/notes');
2019-02-25 21:22:57 +01:00
const imageService = require('../../services/image');
const protectedSessionService = require('../protected_session');
2018-11-26 23:47:02 +01:00
const commonmark = require('commonmark');
const mimeService = require('./mime');
const utils = require('../../services/utils');
2020-06-30 23:37:06 +02:00
const htmlSanitizer = require('../html_sanitizer');
2020-06-20 12:31:38 +02:00
function importSingleFile(taskContext, file, parentNote) {
const mime = mimeService.getMime(file.originalname) || file.mimetype;
if (taskContext.data.textImportedAsText) {
if (mime === 'text/html') {
2020-06-20 12:31:38 +02:00
return importHtml(taskContext, file, parentNote);
} else if (['text/markdown', 'text/x-markdown'].includes(mime)) {
2020-06-20 12:31:38 +02:00
return importMarkdown(taskContext, file, parentNote);
} 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
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
}
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
}
2020-06-20 12:31:38 +02:00
function importImage(file, parentNote, taskContext) {
const {note} = imageService.saveImage(parentNote.noteId, file.buffer, file.originalname, taskContext.data.shrinkImages);
2019-02-25 21:22:57 +01:00
taskContext.increaseProgressCount();
2019-02-25 21:22:57 +01:00
return note;
}
2020-06-20 12:31:38 +02:00
function importFile(taskContext, file, parentNote) {
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
taskContext.increaseProgressCount();
2019-02-24 13:10:47 +01:00
2019-02-25 21:22:57 +01:00
return note;
}
2020-06-20 12:31:38 +02:00
function importCodeNote(taskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
2019-02-25 21:22:57 +01:00
const content = file.buffer.toString("UTF-8");
const detectedMime = mimeService.getMime(file.originalname) || file.mimetype;
const mime = mimeService.normalizeMimeType(detectedMime);
2019-02-25 21:22:57 +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,
2019-02-25 21:22:57 +01:00
type: 'code',
mime: mime,
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
});
taskContext.increaseProgressCount();
2019-02-25 21:22:57 +01:00
return note;
}
2020-06-20 12:31:38 +02:00
function importPlainText(taskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
2019-02-24 13:10:47 +01:00
const plainTextContent = file.buffer.toString("UTF-8");
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
});
taskContext.increaseProgressCount();
2019-02-24 13:10:47 +01:00
return note;
}
function convertTextToHtml(text) {
// 1: Plain Text Search
text = text.replace(/&/g, "&").
replace(/</g, "&lt;").
replace(/>/g, "&gt;");
// 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
text = "<p>" + text + "</p>";
return text;
}
2020-06-20 12:31:38 +02:00
function importMarkdown(taskContext, file, parentNote) {
2018-11-26 23:47:02 +01:00
const markdownContent = file.buffer.toString("UTF-8");
const reader = new commonmark.Parser();
const writer = new commonmark.HtmlRenderer();
const parsed = reader.parse(markdownContent);
2020-06-30 23:37:06 +02:00
let htmlContent = writer.render(parsed);
htmlContent = htmlSanitizer.sanitize(htmlContent);
2018-11-26 23:47:02 +01:00
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
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
});
taskContext.increaseProgressCount();
2019-02-10 19:36:03 +01:00
2018-11-26 23:47:02 +01:00
return note;
}
2020-06-20 12:31:38 +02:00
function importHtml(taskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
2020-06-30 23:37:06 +02:00
let content = file.buffer.toString("UTF-8");
content = htmlSanitizer.sanitize(content);
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,
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
});
taskContext.increaseProgressCount();
2019-02-10 19:36:03 +01:00
2018-11-26 23:47:02 +01:00
return note;
}
module.exports = {
2019-02-24 13:10:47 +01:00
importSingleFile
2020-06-20 12:31:38 +02:00
};