From 1a556b6ff2840737fa5a07fad9f153edb75c1d7b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 9 Nov 2023 10:12:33 -0500 Subject: [PATCH] fix: Internal server error during import with nested documents --- server/queues/tasks/ImportMarkdownZipTask.ts | 23 ++++++++++++++------ server/utils/ZipHelper.ts | 8 +++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/server/queues/tasks/ImportMarkdownZipTask.ts b/server/queues/tasks/ImportMarkdownZipTask.ts index 5828c1179..b9a0e1272 100644 --- a/server/queues/tasks/ImportMarkdownZipTask.ts +++ b/server/queues/tasks/ImportMarkdownZipTask.ts @@ -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); + } }) ); } diff --git a/server/utils/ZipHelper.ts b/server/utils/ZipHelper.ts index b3205d7f3..fbe5efe21 100644 --- a/server/utils/ZipHelper.ts +++ b/server/utils/ZipHelper.ts @@ -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: [], };