Skip to content

Build your personal knowledge base with Trilium Notes

export & import work correctly with clones

azivner1f96a6beab89
diff --git a/src/routes/api/export.js b/src/routes/api/export.js
index e5cf87aeb4..414fbf6a70 100644
--- a/src/routes/api/export.js
+++ b/src/routes/api/export.js
@@ -13,59 +13,75 @@ async function exportNote(req, res) {
const pack = tar.pack();
- const name = await exportNoteInner(branchId, '', pack);
-
- pack.finalize();
-
- res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"');
- res.setHeader('Content-Type', 'application/tar');
-
- pack.pipe(res);
-}
-
-async function exportNoteInner(branchId, directory, pack) {
- const branch = await repository.getBranch(branchId);
- const note = await branch.getNote();
-
- if (note.isProtected) {
- return;
+ const exportedNoteIds = [];
+ const name = await exportNoteInner(branchId, '');
+
+ async function exportNoteInner(branchId, directory) {
+ const branch = await repository.getBranch(branchId);
+ const note = await branch.getNote();
+ const childFileName = directory + sanitize(note.title);
+
+ if (exportedNoteIds.includes(note.noteId)) {
+ saveMetadataFile(childFileName, {
+ version: 1,
+ clone: true,
+ noteId: note.noteId,
+ prefix: branch.prefix
+ });
+
+ return;
+ }
+
+ const metadata = {
+ version: 1,
+ clone: false,
+ noteId: note.noteId,
+ title: note.title,
+ prefix: branch.prefix,
+ type: note.type,
+ mime: note.mime,
+ labels: (await note.getLabels()).map(label => {
+ return {
+ name: label.name,
+ value: label.value
+ };
+ })
+ };
+
+ if (metadata.labels.find(label => label.name === 'excludeFromExport')) {
+ return;
+ }
+
+ saveMetadataFile(childFileName, metadata);
+ saveDataFile(childFileName, note);
+
+ exportedNoteIds.push(note.noteId);
+
+ for (const child of await note.getChildBranches()) {
+ await exportNoteInner(child.branchId, childFileName + "/");
+ }
+
+ return childFileName;
}
- const metadata = await getMetadata(note);
+ function saveDataFile(childFileName, note) {
+ const content = note.type === 'text' ? html.prettyPrint(note.content, {indent_size: 2}) : note.content;
- if (metadata.labels.find(label => label.name === 'excludeFromExport')) {
- return;
+ pack.entry({name: childFileName + ".dat", size: content.length}, content);
}
- const metadataJson = JSON.stringify(metadata, null, '\t');
- const childFileName = directory + sanitize(note.title);
+ function saveMetadataFile(childFileName, metadata) {
+ const metadataJson = JSON.stringify(metadata, null, '\t');
- pack.entry({ name: childFileName + ".meta", size: metadataJson.length }, metadataJson);
-
- const content = note.type === 'text' ? html.prettyPrint(note.content, {indent_size: 2}) : note.content;
-
- pack.entry({ name: childFileName + ".dat", size: content.length }, content);
-
- for (const child of await note.getChildBranches()) {
- await exportNoteInner(child.branchId, childFileName + "/", pack);
+ pack.entry({name: childFileName + ".meta", size: metadataJson.length}, metadataJson);
}
- return childFileName;
-}
+ pack.finalize();
-async function getMetadata(note) {
- return {
- version: 1,
- title: note.title,
- type: note.type,
- mime: note.mime,
- labels: (await note.getLabels()).map(label => {
- return {
- name: label.name,
- value: label.value
- };
- })
- };
+ res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"');
+ res.setHeader('Content-Type', 'application/tar');
+
+ pack.pipe(res);
}
module.exports = {
diff --git a/src/routes/api/import.js b/src/routes/api/import.js
index a2db68927e..f89ffabe07 100644
--- a/src/routes/api/import.js
+++ b/src/routes/api/import.js
@@ -3,6 +3,7 @@
const repository = require('../../services/repository');
const labelService = require('../../services/labels');
const noteService = require('../../services/notes');
+const Branch = require('../../entities/branch');
const tar = require('tar-stream');
const stream = require('stream');
const path = require('path');
@@ -31,7 +32,7 @@ async function parseImportFile(file) {
const extract = tar.extract();
extract.on('entry', function(header, stream, next) {
- let {name, key} = getFileName(header.name);
+ const {name, key} = getFileName(header.name);
let file = fileMap[name];
@@ -97,30 +98,46 @@ async function importTar(req) {
const files = await parseImportFile(file);
- await importNotes(files, parentNoteId);
+ // maps from original noteId (in tar file) to newly generated noteId
+ const noteIdMap = {};
+
+ await importNotes(files, parentNoteId, noteIdMap);
}
-async function importNotes(files, parentNoteId) {
+async function importNotes(files, parentNoteId, noteIdMap) {
for (const file of files) {
if (file.meta.version !== 1) {
throw new Error("Can't read meta data version " + file.meta.version);
}
+ if (file.meta.clone) {
+ await new Branch({
+ parentNoteId: parentNoteId,
+ noteId: noteIdMap[file.meta.noteId],
+ prefix: file.meta.prefix
+ }).save();
+
+ return;
+ }
+
if (file.meta.type !== 'file') {
file.data = file.data.toString("UTF-8");
}
const {note} = await noteService.createNote(parentNoteId, file.meta.title, file.data, {
type: file.meta.type,
- mime: file.meta.mime
+ mime: file.meta.mime,
+ prefix: file.meta.prefix
});
+ noteIdMap[file.meta.noteId] = note.noteId;
+
for (const label of file.meta.labels) {
await labelService.createLabel(note.noteId, label.name, label.value);
}
if (file.children.length > 0) {
- await importNotes(file.children, note.noteId);
+ await importNotes(file.children, note.noteId, noteIdMap);
}
}
}
diff --git a/src/services/notes.js b/src/services/notes.js
index 29a011e821..e4fb745574 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -56,6 +56,7 @@ async function createNewNote(parentNoteId, noteData) {
noteId: note.noteId,
parentNoteId: parentNoteId,
notePosition: newNotePos,
+ prefix: noteData.prefix,
isExpanded: 0
}).save();