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 path = require('path');
|
2019-09-30 20:22:09 +02:00
|
|
|
const mimeService = require('./mime');
|
2019-06-05 21:27:07 +02:00
|
|
|
|
2019-02-24 13:10:47 +01:00
|
|
|
async function importSingleFile(importContext, file, parentNote) {
|
2019-10-06 18:28:53 +02:00
|
|
|
const mime = mimeService.getMime(file.originalname) || file.mimetype;
|
2019-03-03 00:25:31 +01:00
|
|
|
|
2019-02-24 13:10:47 +01:00
|
|
|
if (importContext.textImportedAsText) {
|
2019-03-03 00:25:31 +01:00
|
|
|
if (mime === 'text/html') {
|
2019-02-25 21:22:57 +01:00
|
|
|
return await importHtml(importContext, file, parentNote);
|
2019-03-03 00:25:31 +01:00
|
|
|
} else if (['text/markdown', 'text/x-markdown'].includes(mime)) {
|
2019-02-25 21:22:57 +01:00
|
|
|
return await importMarkdown(importContext, file, parentNote);
|
2019-03-03 00:25:31 +01:00
|
|
|
} else if (mime === 'text/plain') {
|
2019-02-25 21:22:57 +01:00
|
|
|
return await importPlainText(importContext, file, parentNote);
|
2019-02-24 13:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-25 21:22:57 +01:00
|
|
|
|
2019-09-30 20:22:09 +02:00
|
|
|
if (importContext.codeImportedAsCode && mimeService.getType(importContext, mime) === 'code') {
|
2019-02-25 21:22:57 +01:00
|
|
|
return await importCodeNote(importContext, file, parentNote);
|
|
|
|
}
|
|
|
|
|
2019-06-23 15:22:05 +02:00
|
|
|
if (["image/jpeg", "image/gif", "image/png", "image/webp"].includes(mime)) {
|
2019-02-25 21:22:57 +01:00
|
|
|
return await importImage(file, parentNote, importContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await importFile(importContext, file, parentNote);
|
2019-02-24 13:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 21:22:57 +01:00
|
|
|
async function importImage(file, parentNote, importContext) {
|
2019-07-10 23:01:30 +02:00
|
|
|
const {note} = await imageService.saveImage(file.buffer, file.originalname, parentNote.noteId, importContext.shrinkImages);
|
2019-02-25 21:22:57 +01:00
|
|
|
|
|
|
|
importContext.increaseProgressCount();
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function importFile(importContext, file, parentNote) {
|
|
|
|
const originalName = file.originalname;
|
|
|
|
const size = file.size;
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, originalName, file.buffer, {
|
|
|
|
target: 'into',
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
|
|
|
type: 'file',
|
2019-10-06 18:28:53 +02:00
|
|
|
mime: mimeService.getMime(originalName) || file.mimetype,
|
2019-02-25 21:22:57 +01:00
|
|
|
attributes: [
|
|
|
|
{ type: "label", name: "originalFileName", value: originalName },
|
|
|
|
{ type: "label", name: "fileSize", value: size }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
importContext.increaseProgressCount();
|
2019-02-24 13:10:47 +01:00
|
|
|
|
2019-02-25 21:22:57 +01:00
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function importCodeNote(importContext, file, parentNote) {
|
|
|
|
const title = getFileNameWithoutExtension(file.originalname);
|
|
|
|
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
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, title, content, {
|
|
|
|
type: 'code',
|
|
|
|
mime: mime,
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
|
|
|
|
});
|
|
|
|
|
|
|
|
importContext.increaseProgressCount();
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function importPlainText(importContext, file, parentNote) {
|
2019-02-24 13:10:47 +01:00
|
|
|
const title = getFileNameWithoutExtension(file.originalname);
|
|
|
|
const plainTextContent = file.buffer.toString("UTF-8");
|
|
|
|
const htmlContent = convertTextToHtml(plainTextContent);
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, title, htmlContent, {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
importContext.increaseProgressCount();
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertTextToHtml(text) {
|
|
|
|
// 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
|
|
|
|
text = "<p>" + text + "</p>";
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
async function importMarkdown(importContext, 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);
|
|
|
|
const htmlContent = writer.render(parsed);
|
|
|
|
|
|
|
|
const title = getFileNameWithoutExtension(file.originalname);
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, title, htmlContent, {
|
|
|
|
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-02-10 22:30:55 +01:00
|
|
|
importContext.increaseProgressCount();
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2018-11-26 23:47:02 +01:00
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
async function importHtml(importContext, file, parentNote) {
|
2018-11-26 23:47:02 +01:00
|
|
|
const title = getFileNameWithoutExtension(file.originalname);
|
|
|
|
const content = file.buffer.toString("UTF-8");
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, title, content, {
|
|
|
|
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-02-10 22:30:55 +01:00
|
|
|
importContext.increaseProgressCount();
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2018-11-26 23:47:02 +01:00
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileNameWithoutExtension(filePath) {
|
|
|
|
const extension = path.extname(filePath);
|
|
|
|
|
|
|
|
return filePath.substr(0, filePath.length - extension.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-02-24 13:10:47 +01:00
|
|
|
importSingleFile
|
2018-11-26 23:47:02 +01:00
|
|
|
};
|