improved ZIP import validation and error handling

This commit is contained in:
zadam 2022-12-29 10:25:49 +01:00
parent f4cf04232f
commit f150c223bc
3 changed files with 26 additions and 11 deletions

View File

@ -16,7 +16,7 @@ function isString(obj) {
if (obj === undefined || obj === null) { if (obj === undefined || obj === null) {
return; return;
} }
if (typeof obj !== 'string') { if (typeof obj !== 'string') {
return `'${obj}' is not a string`; return `'${obj}' is not a string`;
} }
@ -26,7 +26,7 @@ function isBoolean(obj) {
if (obj === undefined || obj === null) { if (obj === undefined || obj === null) {
return; return;
} }
if (typeof obj !== 'boolean') { if (typeof obj !== 'boolean') {
return `'${obj}' is not a boolean`; return `'${obj}' is not a boolean`;
} }
@ -36,7 +36,7 @@ function isInteger(obj) {
if (obj === undefined || obj === null) { if (obj === undefined || obj === null) {
return; return;
} }
if (!Number.isInteger(obj)) { if (!Number.isInteger(obj)) {
return `'${obj}' is not an integer`; return `'${obj}' is not an integer`;
} }
@ -46,13 +46,13 @@ function isNoteId(obj) {
if (obj === undefined || obj === null) { if (obj === undefined || obj === null) {
return; return;
} }
const becca = require('../becca/becca'); const becca = require('../becca/becca');
if (typeof obj !== 'string') { if (typeof obj !== 'string') {
return `'${obj}' is not a valid noteId`; return `'${obj}' is not a valid noteId`;
} }
if (!(obj in becca.notes)) { if (!(obj in becca.notes)) {
return `Note '${obj}' does not exist`; return `Note '${obj}' does not exist`;
} }
@ -84,8 +84,8 @@ function isValidEntityId(obj) {
if (obj === undefined || obj === null) { if (obj === undefined || obj === null) {
return; return;
} }
if (typeof obj !== 'string' || !/^[A-Za-z0-9]{4,32}$/.test(obj)) { if (typeof obj !== 'string' || !/^[A-Za-z0-9_]{4,128}$/.test(obj)) {
return `'${obj}' is not a valid entityId. Only alphanumeric characters are allowed of length 4 to 32.`; return `'${obj}' is not a valid entityId. Only alphanumeric characters are allowed of length 4 to 32.`;
} }
} }
@ -100,4 +100,4 @@ module.exports = {
isNoteType, isNoteType,
isAttributeType, isAttributeType,
isValidEntityId isValidEntityId
}; };

View File

@ -214,13 +214,13 @@ function exportToZip(taskContext, branch, format, res) {
} }
function findLinks(content, noteMeta) { function findLinks(content, noteMeta) {
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9]+)\/[^"]*"/g, (match, targetNoteId) => { content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/[^"]*"/g, (match, targetNoteId) => {
const url = getTargetUrl(targetNoteId, noteMeta); const url = getTargetUrl(targetNoteId, noteMeta);
return url ? `src="${url}"` : match; return url ? `src="${url}"` : match;
}); });
content = content.replace(/href="[^"]*#root[a-zA-Z0-9\/]*\/([a-zA-Z0-9]+)\/?"/g, (match, targetNoteId) => { content = content.replace(/href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)\/?"/g, (match, targetNoteId) => {
const url = getTargetUrl(targetNoteId, noteMeta); const url = getTargetUrl(targetNoteId, noteMeta);
return url ? `href="${url}"` : match; return url ? `href="${url}"` : match;

View File

@ -231,6 +231,13 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
absUrl += `${absUrl.length > 0 ? '/' : ''}${url}`; absUrl += `${absUrl.length > 0 ? '/' : ''}${url}`;
const {noteMeta} = getMeta(absUrl); const {noteMeta} = getMeta(absUrl);
if (!noteMeta) {
log.info(`Could not find note meta for URL '${absUrl}'.`);
return null;
}
const targetNoteId = getNoteId(noteMeta, absUrl); const targetNoteId = getNoteId(noteMeta, absUrl);
return targetNoteId; return targetNoteId;
} }
@ -312,6 +319,10 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
const targetNoteId = getNoteIdFromRelativeUrl(url, filePath); const targetNoteId = getNoteIdFromRelativeUrl(url, filePath);
if (!targetNoteId) {
return match;
}
return `src="api/images/${targetNoteId}/${path.basename(url)}"`; return `src="api/images/${targetNoteId}/${path.basename(url)}"`;
}); });
@ -329,6 +340,10 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
const targetNoteId = getNoteIdFromRelativeUrl(url, filePath); const targetNoteId = getNoteIdFromRelativeUrl(url, filePath);
if (!targetNoteId) {
return match;
}
return `href="#root/${targetNoteId}"`; return `href="#root/${targetNoteId}"`;
}); });