Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
preserve dateCreated and dateModified in ENEX import, fixes #725
zadam6c7d8a966774
diff --git a/src/entities/note.js b/src/entities/note.jsindex 7e0e544f18..d6506bf50c 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -788,6 +788,7 @@ class Note extends Entity {delete pojo.isContentAvailable;delete pojo.__attributeCache;delete pojo.content;+ /** zero references to contentHash, probably can be removed */delete pojo.contentHash;}}diff --git a/src/services/import/enex.js b/src/services/import/enex.jsindex e3e38c8570..44908b9046 100644--- a/src/services/import/enex.js+++ b/src/services/import/enex.js@@ -3,6 +3,7 @@ const fileType = require('file-type');const stream = require('stream');const log = require("../log");const utils = require("../utils");+const sql = require("../sql");const noteService = require("../notes");const imageService = require("../image");const protectedSessionService = require('../protected_session');@@ -11,7 +12,7 @@ const protectedSessionService = require('../protected_session');function parseDate(text) {// insert - and : to make it ISO formattext = text.substr(0, 4) + "-" + text.substr(4, 2) + "-" + text.substr(6, 2)- + "T" + text.substr(9, 2) + ":" + text.substr(11, 2) + ":" + text.substr(13, 2) + "Z";+ + " " + text.substr(9, 2) + ":" + text.substr(11, 2) + ":" + text.substr(13, 2) + ".000Z";return text;}@@ -150,7 +151,7 @@ async function importEnex(taskContext, file, parentNote) {} else if (currentTag === 'created') {note.utcDateCreated = parseDate(text);} else if (currentTag === 'updated') {- // updated is currently ignored since utcDateModified is updated automatically with each save+ note.utcDateModified = parseDate(text);} else if (currentTag === 'tag') {note.attributes.push({type: 'label',@@ -187,9 +188,27 @@ async function importEnex(taskContext, file, parentNote) {}});+ async function updateDates(noteId, utcDateCreated, utcDateModified) {+ // it's difficult to force custom dateCreated and dateModified to Note entity so we do it post-creation with SQL+ await sql.execute(`+ UPDATE notes+ SET dateCreated = ?,+ utcDateCreated = ?,+ dateModified = ?,+ utcDateModified = ?+ WHERE noteId = ?`,+ [utcDateCreated, utcDateCreated, utcDateModified, utcDateModified, noteId]);++ await sql.execute(`+ UPDATE note_contents+ SET utcDateModified = ?+ WHERE noteId = ?`,+ [utcDateModified, noteId]);+ }+async function saveNote() {// make a copy because stream continues with the next async call and note gets overwritten- let {title, content, attributes, resources, utcDateCreated} = note;+ let {title, content, attributes, resources, utcDateCreated, utcDateModified} = note;content = extractContent(content);@@ -201,6 +220,9 @@ async function importEnex(taskContext, file, parentNote) {isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),})).note;+ utcDateCreated = utcDateCreated || noteEntity.utcDateCreated;+ utcDateModified = utcDateModified || noteEntity.utcDateModified;+taskContext.increaseProgressCount();let noteContent = await noteEntity.getContent();@@ -224,6 +246,8 @@ async function importEnex(taskContext, file, parentNote) {isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),})).note;+ await updateDates(resourceNote.noteId, utcDateCreated, utcDateModified);+taskContext.increaseProgressCount();const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;@@ -235,7 +259,9 @@ async function importEnex(taskContext, file, parentNote) {try {const originalName = "image." + resource.mime.substr(6);- const {url} = await imageService.saveImage(noteEntity.noteId, resource.content, originalName, taskContext.data.shrinkImages);+ const {url, note: imageNote} = await imageService.saveImage(noteEntity.noteId, resource.content, originalName, taskContext.data.shrinkImages);++ await updateDates(imageNote.noteId, utcDateCreated, utcDateModified);const imageLink = `<img src="${url}">`;@@ -257,6 +283,10 @@ async function importEnex(taskContext, file, parentNote) {// save updated content with links to files/imagesawait noteEntity.setContent(noteContent);++ await noteService.scanForLinks(noteEntity.noteId);++ await updateDates(noteEntity.noteId, utcDateCreated, utcDateModified);}saxStream.on("closetag", async tag => {