mirror of
https://github.com/zadam/trilium.git
synced 2025-11-09 14:55:50 +01:00
syncification
This commit is contained in:
@@ -22,7 +22,7 @@ const treeService = require("../tree");
|
||||
* @param {Note} importRootNote
|
||||
* @return {Promise<*>}
|
||||
*/
|
||||
async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
// maps from original noteId (in tar file) to newly generated noteId
|
||||
const noteIdMap = {};
|
||||
const attributes = [];
|
||||
@@ -77,7 +77,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
};
|
||||
}
|
||||
|
||||
async function getParentNoteId(filePath, parentNoteMeta) {
|
||||
function getParentNoteId(filePath, parentNoteMeta) {
|
||||
let parentNoteId;
|
||||
|
||||
if (parentNoteMeta) {
|
||||
@@ -95,7 +95,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
else {
|
||||
// tar allows creating out of order records - i.e. file in a directory can appear in the tar stream before actual directory
|
||||
// (out-of-order-directory-records.tar in test set)
|
||||
parentNoteId = await saveDirectory(parentPath);
|
||||
parentNoteId = saveDirectory(parentPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
return { mime, type };
|
||||
}
|
||||
|
||||
async function saveAttributes(note, noteMeta) {
|
||||
function saveAttributes(note, noteMeta) {
|
||||
if (!noteMeta) {
|
||||
return;
|
||||
}
|
||||
@@ -153,20 +153,20 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
}
|
||||
}
|
||||
|
||||
async function saveDirectory(filePath) {
|
||||
function saveDirectory(filePath) {
|
||||
const { parentNoteMeta, noteMeta } = getMeta(filePath);
|
||||
|
||||
const noteId = getNoteId(noteMeta, filePath);
|
||||
const noteTitle = utils.getNoteTitle(filePath, taskContext.data.replaceUnderscoresWithSpaces, noteMeta);
|
||||
const parentNoteId = await getParentNoteId(filePath, parentNoteMeta);
|
||||
const parentNoteId = getParentNoteId(filePath, parentNoteMeta);
|
||||
|
||||
let note = await repository.getNote(noteId);
|
||||
let note = repository.getNote(noteId);
|
||||
|
||||
if (note) {
|
||||
return;
|
||||
}
|
||||
|
||||
({note} = await noteService.createNewNote({
|
||||
({note} = noteService.createNewNote({
|
||||
parentNoteId: parentNoteId,
|
||||
title: noteTitle,
|
||||
content: '',
|
||||
@@ -178,7 +178,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
||||
}));
|
||||
|
||||
await saveAttributes(note, noteMeta);
|
||||
saveAttributes(note, noteMeta);
|
||||
|
||||
if (!firstNote) {
|
||||
firstNote = note;
|
||||
@@ -211,7 +211,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
return targetNoteId;
|
||||
}
|
||||
|
||||
async function saveNote(filePath, content) {
|
||||
function saveNote(filePath, content) {
|
||||
const {parentNoteMeta, noteMeta} = getMeta(filePath);
|
||||
|
||||
if (noteMeta && noteMeta.noImport) {
|
||||
@@ -219,10 +219,10 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
}
|
||||
|
||||
const noteId = getNoteId(noteMeta, filePath);
|
||||
const parentNoteId = await getParentNoteId(filePath, parentNoteMeta);
|
||||
const parentNoteId = getParentNoteId(filePath, parentNoteMeta);
|
||||
|
||||
if (noteMeta && noteMeta.isClone) {
|
||||
await new Branch({
|
||||
new Branch({
|
||||
noteId,
|
||||
parentNoteId,
|
||||
isExpanded: noteMeta.isExpanded,
|
||||
@@ -300,13 +300,13 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
}
|
||||
}
|
||||
|
||||
let note = await repository.getNote(noteId);
|
||||
let note = repository.getNote(noteId);
|
||||
|
||||
if (note) {
|
||||
await note.setContent(content);
|
||||
note.setContent(content);
|
||||
}
|
||||
else {
|
||||
({note} = await noteService.createNewNote({
|
||||
({note} = noteService.createNewNote({
|
||||
parentNoteId: parentNoteId,
|
||||
title: noteTitle,
|
||||
content: content,
|
||||
@@ -319,7 +319,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
||||
}));
|
||||
|
||||
await saveAttributes(note, noteMeta);
|
||||
saveAttributes(note, noteMeta);
|
||||
|
||||
if (!firstNote) {
|
||||
firstNote = note;
|
||||
@@ -366,7 +366,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
// stream is the content body (might be an empty stream)
|
||||
// call next when you are done with this entry
|
||||
|
||||
stream.on('end', async function() {
|
||||
stream.on('end', function() {
|
||||
const filePath = normalizeFilePath(header.name);
|
||||
|
||||
const content = Buffer.concat(chunks);
|
||||
@@ -375,10 +375,10 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
metaFile = JSON.parse(content.toString("UTF-8"));
|
||||
}
|
||||
else if (header.type === 'directory') {
|
||||
await saveDirectory(filePath);
|
||||
saveDirectory(filePath);
|
||||
}
|
||||
else if (header.type === 'file') {
|
||||
await saveNote(filePath, content);
|
||||
saveNote(filePath, content);
|
||||
}
|
||||
else {
|
||||
log.info("Ignoring tar import entry with type " + header.type);
|
||||
@@ -393,7 +393,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
});
|
||||
|
||||
return new Promise(resolve => {
|
||||
extract.on('finish', async function() {
|
||||
extract.on('finish', function() {
|
||||
const createdNoteIds = {};
|
||||
|
||||
for (const path in createdPaths) {
|
||||
@@ -403,12 +403,12 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
}
|
||||
|
||||
for (const noteId in createdNoteIds) { // now the noteIds are unique
|
||||
await noteService.scanForLinks(await repository.getNote(noteId));
|
||||
noteService.scanForLinks(repository.getNote(noteId));
|
||||
|
||||
if (!metaFile) {
|
||||
// if there's no meta file then the notes are created based on the order in that tar file but that
|
||||
// is usually quite random so we sort the notes in the way they would appear in the file manager
|
||||
await treeService.sortNotesAlphabetically(noteId, true);
|
||||
treeService.sortNotesAlphabetically(noteId, true);
|
||||
}
|
||||
|
||||
taskContext.increaseProgressCount();
|
||||
@@ -418,7 +418,7 @@ async function importTar(taskContext, fileBuffer, importRootNote) {
|
||||
// are already in the database (we don't want to have "broken" relations, not even transitionally)
|
||||
for (const attr of attributes) {
|
||||
if (attr.type !== 'relation' || attr.value in createdNoteIds) {
|
||||
await new Attribute(attr).save();
|
||||
new Attribute(attr).save();
|
||||
}
|
||||
else {
|
||||
log.info("Relation not imported since target note doesn't exist: " + JSON.stringify(attr));
|
||||
|
||||
Reference in New Issue
Block a user