fix import zip corner case where there are 2 independent notes with the same name

This commit is contained in:
zadam 2020-03-21 15:14:44 +01:00
parent 6de0b19569
commit d927865cbd

View file

@ -25,12 +25,13 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
// maps from original noteId (in tar file) to newly generated noteId // maps from original noteId (in tar file) to newly generated noteId
const noteIdMap = {}; const noteIdMap = {};
const attributes = []; const attributes = [];
// path => noteId // path => noteId, used only when meta file is not available
const createdPaths = { '/': importRootNote.noteId, '\\': importRootNote.noteId }; const createdPaths = { '/': importRootNote.noteId, '\\': importRootNote.noteId };
const mdReader = new commonmark.Parser(); const mdReader = new commonmark.Parser();
const mdWriter = new commonmark.HtmlRenderer(); const mdWriter = new commonmark.HtmlRenderer();
let metaFile = null; let metaFile = null;
let firstNote = null; let firstNote = null;
const createdNoteIds = {};
function getNewNoteId(origNoteId) { function getNewNoteId(origNoteId) {
// in case the original noteId is empty. This probably shouldn't happen, but still good to have this precaution // in case the original noteId is empty. This probably shouldn't happen, but still good to have this precaution
@ -111,13 +112,17 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
} }
function getNoteId(noteMeta, filePath) { function getNoteId(noteMeta, filePath) {
if (noteMeta) {
return getNewNoteId(noteMeta.noteId);
}
const filePathNoExt = getTextFileWithoutExtension(filePath); const filePathNoExt = getTextFileWithoutExtension(filePath);
if (filePathNoExt in createdPaths) { if (filePathNoExt in createdPaths) {
return createdPaths[filePathNoExt]; return createdPaths[filePathNoExt];
} }
const noteId = noteMeta ? getNewNoteId(noteMeta.noteId) : utils.newEntityId(); const noteId = utils.newEntityId();
createdPaths[filePathNoExt] = noteId; createdPaths[filePathNoExt] = noteId;
@ -191,6 +196,8 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(), isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
})); }));
createdNoteIds[note.noteId] = true;
await saveAttributes(note, noteMeta); await saveAttributes(note, noteMeta);
if (!firstNote) { if (!firstNote) {
@ -245,6 +252,10 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
const noteId = getNoteId(noteMeta, filePath); const noteId = getNoteId(noteMeta, filePath);
const parentNoteId = await getParentNoteId(filePath, parentNoteMeta); const parentNoteId = await getParentNoteId(filePath, parentNoteMeta);
if (!parentNoteId) {
throw new Error(`Cannot find parentNoteId for ${filePath}`);
}
if (noteMeta && noteMeta.isClone) { if (noteMeta && noteMeta.isClone) {
await new Branch({ await new Branch({
noteId, noteId,
@ -343,6 +354,8 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(), isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
})); }));
createdNoteIds[note.noteId] = true;
await saveAttributes(note, noteMeta); await saveAttributes(note, noteMeta);
if (!firstNote) { if (!firstNote) {
@ -418,7 +431,6 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
metaFile = JSON.parse(content.toString("UTF-8")); metaFile = JSON.parse(content.toString("UTF-8"));
} }
taskContext.increaseProgressCount();
zipfile.readEntry(); zipfile.readEntry();
}); });
@ -438,14 +450,6 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
zipfile.readEntry(); zipfile.readEntry();
}); });
const createdNoteIds = {};
for (const path in createdPaths) {
const noteId = createdPaths[path];
createdNoteIds[noteId] = true;
}
for (const noteId in createdNoteIds) { // now the noteIds are unique for (const noteId in createdNoteIds) { // now the noteIds are unique
await noteService.scanForLinks(noteId); await noteService.scanForLinks(noteId);