mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
migrate images to attachments
This commit is contained in:
parent
bd489d5780
commit
5cc5859211
@ -1,4 +1,3 @@
|
|||||||
const sql = require("../../src/services/sql");
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
const sql = require("../../src/services/sql");
|
const sql = require("../../src/services/sql");
|
||||||
const utils = require("../../src/services/utils");
|
const utils = require("../../src/services/utils");
|
||||||
|
21
db/migrations/0218__migrate_images_to_attachments.js
Normal file
21
db/migrations/0218__migrate_images_to_attachments.js
Normal file
@ -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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
@ -1033,6 +1033,7 @@ class BNote extends AbstractBeccaEntity {
|
|||||||
return this.noteId === '_hidden' || this.hasAncestor('_hidden');
|
return this.noteId === '_hidden' || this.hasAncestor('_hidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {BAttribute[]} */
|
||||||
getTargetRelations() {
|
getTargetRelations() {
|
||||||
return this.targetRelations;
|
return this.targetRelations;
|
||||||
}
|
}
|
||||||
@ -1328,6 +1329,66 @@ class BNote extends AbstractBeccaEntity {
|
|||||||
|
|
||||||
return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
|
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.
|
* (Soft) delete a note and all its descendants.
|
||||||
|
@ -4,7 +4,7 @@ const build = require('./build');
|
|||||||
const packageJson = require('../../package');
|
const packageJson = require('../../package');
|
||||||
const {TRILIUM_DATA_DIR} = require('./data_dir');
|
const {TRILIUM_DATA_DIR} = require('./data_dir');
|
||||||
|
|
||||||
const APP_DB_VERSION = 217;
|
const APP_DB_VERSION = 218;
|
||||||
const SYNC_VERSION = 30;
|
const SYNC_VERSION = 30;
|
||||||
const CLIPPER_PROTOCOL_VERSION = "1.0";
|
const CLIPPER_PROTOCOL_VERSION = "1.0";
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user