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 dateNoteService = require('../../services/date_notes');
const noteService = require('../../services/notes');
const attributeService = require('../../services/attributes');
function uploadImage(req) {
const file = req.file;
@ -37,7 +38,7 @@ function saveNote(req) {
if (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 sql = require('./sql');
const utils = require('./utils');
const Attribute = require('../entities/attribute');
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 = {
getNotesWithLabel,
getNotesWithLabels,
@ -156,5 +169,6 @@ module.exports = {
getAttributeNames,
isAttributeType,
isAttributeDangerous,
getBuiltinAttributeNames
getBuiltinAttributeNames,
sanitizeAttributeName
};

View File

@ -11,6 +11,7 @@ const entityChangesService = require('./entity_changes.js');
const optionsService = require('./options');
const Branch = require('../entities/branch');
const dateUtils = require('./date_utils');
const attributeService = require('./attributes');
class ConsistencyChecks {
constructor(autoFix) {
@ -607,20 +608,10 @@ class ConsistencyChecks {
findWronglyNamedAttributes() {
const attrNames = sql.getColumn(`SELECT DISTINCT name FROM attributes`);
const attrNameMatcher = new RegExp("^[\\p{L}\\p{N}_:]+$", "u");
for (const origName of attrNames) {
if (!attrNameMatcher.test(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, "_");
}
const fixedName = attributeService.sanitizeAttributeName(origName);
if (fixedName !== origName) {
if (this.autoFix) {
// 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

View File

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