small fixes to ENEX import

This commit is contained in:
zadam 2020-11-17 22:35:20 +01:00
parent 845907b8d2
commit c0a29ede05
5 changed files with 37 additions and 18 deletions

View File

@ -4,6 +4,7 @@ const imageType = require('image-type');
const imageService = require('../../services/image'); const imageService = require('../../services/image');
const dateNoteService = require('../../services/date_notes'); const dateNoteService = require('../../services/date_notes');
const noteService = require('../../services/notes'); const noteService = require('../../services/notes');
const attributeService = require('../../services/attributes');
function uploadImage(req) { function uploadImage(req) {
const file = req.file; const file = req.file;
@ -37,7 +38,7 @@ function saveNote(req) {
if (req.body.labels) { if (req.body.labels) {
for (const {name, value} of req.body.labels) { for (const {name, value} of req.body.labels) {
note.setLabel(name, value); note.setLabel(attributeService.sanitizeAttributeName(name), value);
} }
} }

View File

@ -2,7 +2,6 @@
const repository = require('./repository'); const repository = require('./repository');
const sql = require('./sql'); const sql = require('./sql');
const utils = require('./utils');
const Attribute = require('../entities/attribute'); const Attribute = require('../entities/attribute');
const ATTRIBUTE_TYPES = [ 'label', 'relation' ]; const ATTRIBUTE_TYPES = [ 'label', 'relation' ];
@ -146,6 +145,20 @@ function getBuiltinAttributeNames() {
]); ]);
} }
function sanitizeAttributeName(origName) {
let fixedName;
if (origName === '') {
fixedName = "unnamed";
}
else {
// any not allowed character should be replaced with underscore
fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_");
}
return fixedName;
}
module.exports = { module.exports = {
getNotesWithLabel, getNotesWithLabel,
getNotesWithLabels, getNotesWithLabels,
@ -156,5 +169,6 @@ module.exports = {
getAttributeNames, getAttributeNames,
isAttributeType, isAttributeType,
isAttributeDangerous, isAttributeDangerous,
getBuiltinAttributeNames getBuiltinAttributeNames,
sanitizeAttributeName
}; };

View File

@ -11,6 +11,7 @@ const entityChangesService = require('./entity_changes.js');
const optionsService = require('./options'); const optionsService = require('./options');
const Branch = require('../entities/branch'); const Branch = require('../entities/branch');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const attributeService = require('./attributes');
class ConsistencyChecks { class ConsistencyChecks {
constructor(autoFix) { constructor(autoFix) {
@ -607,20 +608,10 @@ class ConsistencyChecks {
findWronglyNamedAttributes() { findWronglyNamedAttributes() {
const attrNames = sql.getColumn(`SELECT DISTINCT name FROM attributes`); const attrNames = sql.getColumn(`SELECT DISTINCT name FROM attributes`);
const attrNameMatcher = new RegExp("^[\\p{L}\\p{N}_:]+$", "u");
for (const origName of attrNames) { for (const origName of attrNames) {
if (!attrNameMatcher.test(origName)) { const fixedName = attributeService.sanitizeAttributeName(origName);
let fixedName;
if (origName === '') {
fixedName = "unnamed";
}
else {
// any not allowed character should be replaced with underscore
fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_");
}
if (fixedName !== origName) {
if (this.autoFix) { if (this.autoFix) {
// there isn't a good way to update this: // there isn't a good way to update this:
// - just SQL query will fix it in DB but not notify frontend (or other caches) that it has been fixed // - just SQL query will fix it in DB but not notify frontend (or other caches) that it has been fixed

View File

@ -37,7 +37,7 @@ function getImageType(buffer) {
} }
} }
else { else {
return imageType(buffer); return imageType(buffer) || "jpg"; // optimistic JPG default
} }
} }

View File

@ -7,6 +7,7 @@ const noteService = require("../notes");
const imageService = require("../image"); const imageService = require("../image");
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const htmlSanitizer = require("../html_sanitizer"); const htmlSanitizer = require("../html_sanitizer");
const attributeService = require("../attributes");
// date format is e.g. 20181121T193703Z // date format is e.g. 20181121T193703Z
function parseDate(text) { function parseDate(text) {
@ -105,9 +106,17 @@ function importEnex(taskContext, file, parentNote) {
const previousTag = getPreviousTag(); const previousTag = getPreviousTag();
if (previousTag === 'note-attributes') { if (previousTag === 'note-attributes') {
let labelName = currentTag;
if (labelName === 'source-url') {
labelName = 'sourceUrl';
}
labelName = attributeService.sanitizeAttributeName(labelName);
note.attributes.push({ note.attributes.push({
type: 'label', type: 'label',
name: currentTag, name: labelName,
value: text value: text
}); });
} }
@ -149,7 +158,7 @@ function importEnex(taskContext, file, parentNote) {
} else if (currentTag === 'tag') { } else if (currentTag === 'tag') {
note.attributes.push({ note.attributes.push({
type: 'label', type: 'label',
name: text, name: attributeService.sanitizeAttributeName(text),
value: '' value: ''
}) })
} }
@ -227,6 +236,10 @@ function importEnex(taskContext, file, parentNote) {
taskContext.increaseProgressCount(); taskContext.increaseProgressCount();
for (const resource of resources) { for (const resource of resources) {
if (!resource.content) {
continue;
}
const hash = utils.md5(resource.content); const hash = utils.md5(resource.content);
const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g'); const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g');