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
migrate images to attachments
zadam5cc585921160
diff --git a/db/migrations/0215__move_content_into_blobs.js b/db/migrations/0215__move_content_into_blobs.jsindex e447e9c304..77ab76dfe1 100644--- a/db/migrations/0215__move_content_into_blobs.js+++ b/db/migrations/0215__move_content_into_blobs.js@@ -1,4 +1,3 @@-const sql = require("../../src/services/sql");module.exports = () => {const sql = require("../../src/services/sql");const utils = require("../../src/services/utils");diff --git a/db/migrations/0218__migrate_images_to_attachments.js b/db/migrations/0218__migrate_images_to_attachments.jsnew file mode 100644index 0000000000..5100abe06e--- /dev/null+++ b/db/migrations/0218__migrate_images_to_attachments.js@@ -0,0 +1,21 @@+module.exports = () => {+ const beccaLoader = require("../../src/becca/becca_loader");+ const becca = require("../../src/becca/becca");+ const cls = require("../../src/services/cls");+ const log = require("../../src/services/log");++ cls.init(() => {+ beccaLoader.load();++ for (const note of Object.values(becca.notes)) {+ try {+ const attachment = note.convertToParentAttachment({force: false});++ log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`)+ }+ catch (e) {+ log.error(`Cannot convert note '${note.noteId}' to attachment: ${e.message} ${e.stack}`);+ }+ }+ });+};\ No newline at end of filediff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.jsindex 2b153588eb..2c459663c8 100644--- a/src/becca/entities/bnote.js+++ b/src/becca/entities/bnote.js@@ -1033,6 +1033,7 @@ class BNote extends AbstractBeccaEntity {return this.noteId === '_hidden' || this.hasAncestor('_hidden');}+ /** @returns {BAttribute[]} */getTargetRelations() {return this.targetRelations;}@@ -1328,6 +1329,66 @@ class BNote extends AbstractBeccaEntity {return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);}+ /**+ * Some notes are eligible for conversion into an attachment of its parent, note must have these properties:+ * - it has exactly one target relation+ * - it has a relation from its parent note+ * - it has no children+ * - it has no clones+ * - parent is of type text+ * - both notes are either unprotected or user is in protected session+ *+ * Currently, works only for image notes.+ *+ * In future this functionality might get more generic and some of the requirements relaxed.+ *+ * @params {Object} [opts]+ * @params {bolean} [opts.force=false} it is envisioned that user can force the conversion even if some conditions+ * are not satisfied (e.g. relation to parent doesn't exist).+ *+ * @returns {BAttachment|null} - null if note is not eligible for conversion+ */++ convertToParentAttachment(opts = {force: false}) {+ if (this.type !== 'image' || !this.isContentAvailable() || this.hasChildren() || this.getParentBranches().length !== 1) {+ return null;+ }++ const targetRelations = this.getTargetRelations().filter(relation => relation.name === 'imageLink');++ if (targetRelations.length !== 1) {+ return null;+ }++ const parentNote = this.getParentNotes()[0]; // at this point note can have only one parent+ const referencingNote = targetRelations[0].note;++ if (parentNote !== referencingNote || parentNote.type !== 'text' || !parentNote.isContentAvailable()) {+ return null;+ }++ const content = this.getContent();++ const attachment = parentNote.saveAttachment({+ role: 'image',+ mime: this.mime,+ title: this.title,+ content: content+ });++ let parentContent = parentNote.getContent();++ const oldNoteUrl = `api/images/${this.noteId}/`;+ const newAttachmentUrl = `api/notes/${parentNote.noteId}/images/${attachment.attachmentId}/`;++ const fixedContent = utils.replaceAll(parentContent, oldNoteUrl, newAttachmentUrl);++ parentNote.setContent(fixedContent);++ this.deleteNote();++ return attachment;+ }/*** (Soft) delete a note and all its descendants.diff --git a/src/services/app_info.js b/src/services/app_info.jsindex 667b156a29..c943d17ad9 100644--- a/src/services/app_info.js+++ b/src/services/app_info.js@@ -4,7 +4,7 @@ const build = require('./build');const packageJson = require('../../package');const {TRILIUM_DATA_DIR} = require('./data_dir');-const APP_DB_VERSION = 217;+const APP_DB_VERSION = 218;const SYNC_VERSION = 30;const CLIPPER_PROTOCOL_VERSION = "1.0";