fix: Internal server error during import with nested documents

This commit is contained in:
Tom Moor
2023-11-09 10:12:33 -05:00
parent 0964d03a17
commit 1a556b6ff2
2 changed files with 19 additions and 12 deletions

View File

@@ -107,18 +107,27 @@ export default class ImportMarkdownZipTask extends ImportTask {
? new Date(metadata.updatedAt) ? new Date(metadata.updatedAt)
: zipObject.date; : zipObject.date;
const existingEmptyDocumentIndex = output.documents.findIndex( const existingDocumentIndex = output.documents.findIndex(
(doc) => (doc) =>
doc.title === title && doc.title === title &&
doc.collectionId === collectionId && doc.collectionId === collectionId &&
doc.parentDocumentId === parentDocumentId && doc.parentDocumentId === parentDocumentId
doc.text === ""
); );
const existingDocument = output.documents[existingDocumentIndex];
// When there is a file and a folder with the same name this handles // When there is a file and a folder with the same name this handles
// the case by combining the two into one document with nested children // the case by combining the two into one document with nested children
if (existingEmptyDocumentIndex !== -1) { if (existingDocument) {
output.documents[existingEmptyDocumentIndex].text = text; if (existingDocument.text === "") {
output.documents[existingDocumentIndex].text = text;
}
await parseNodeChildren(
child.children,
collectionId,
existingDocument.id
);
} else { } else {
output.documents.push({ output.documents.push({
id, id,
@@ -131,9 +140,9 @@ export default class ImportMarkdownZipTask extends ImportTask {
parentDocumentId, parentDocumentId,
path: child.path, path: child.path,
}); });
}
await parseNodeChildren(child.children, collectionId, id); await parseNodeChildren(child.children, collectionId, id);
}
}) })
); );
} }

View File

@@ -59,9 +59,7 @@ export default class ZipHelper {
let currentLevel = tree; // initialize currentLevel to root let currentLevel = tree; // initialize currentLevel to root
pathParts.forEach(function (rawName) { pathParts.forEach(function (name) {
const { name } = path.parse(path.basename(rawName));
// check to see if the path already exists. // check to see if the path already exists.
const existingPath = find(currentLevel, { const existingPath = find(currentLevel, {
name, name,
@@ -71,13 +69,13 @@ export default class ZipHelper {
// The path to this item was already in the tree, so don't add again. // The path to this item was already in the tree, so don't add again.
// Set the current level to this path's children // Set the current level to this path's children
currentLevel = existingPath.children; currentLevel = existingPath.children;
} else if (rawName.endsWith(".DS_Store") || !rawName) { } else if (name.endsWith(".DS_Store") || !name) {
return; return;
} else { } else {
const newPart = { const newPart = {
name, name,
path: filePath.replace(/^\//, ""), path: filePath.replace(/^\//, ""),
title: deserializeFilename(name), title: deserializeFilename(path.parse(path.basename(name)).name),
children: [], children: [],
}; };