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)
: zipObject.date;
const existingEmptyDocumentIndex = output.documents.findIndex(
const existingDocumentIndex = output.documents.findIndex(
(doc) =>
doc.title === title &&
doc.collectionId === collectionId &&
doc.parentDocumentId === parentDocumentId &&
doc.text === ""
doc.parentDocumentId === parentDocumentId
);
const existingDocument = output.documents[existingDocumentIndex];
// 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
if (existingEmptyDocumentIndex !== -1) {
output.documents[existingEmptyDocumentIndex].text = text;
if (existingDocument) {
if (existingDocument.text === "") {
output.documents[existingDocumentIndex].text = text;
}
await parseNodeChildren(
child.children,
collectionId,
existingDocument.id
);
} else {
output.documents.push({
id,
@@ -131,9 +140,9 @@ export default class ImportMarkdownZipTask extends ImportTask {
parentDocumentId,
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
pathParts.forEach(function (rawName) {
const { name } = path.parse(path.basename(rawName));
pathParts.forEach(function (name) {
// check to see if the path already exists.
const existingPath = find(currentLevel, {
name,
@@ -71,13 +69,13 @@ export default class ZipHelper {
// The path to this item was already in the tree, so don't add again.
// Set the current level to this path's children
currentLevel = existingPath.children;
} else if (rawName.endsWith(".DS_Store") || !rawName) {
} else if (name.endsWith(".DS_Store") || !name) {
return;
} else {
const newPart = {
name,
path: filePath.replace(/^\//, ""),
title: deserializeFilename(name),
title: deserializeFilename(path.parse(path.basename(name)).name),
children: [],
};