fixes to note attachment handling

This commit is contained in:
zadam 2023-01-23 23:37:58 +01:00
parent 8a33645360
commit f59e19d93b
8 changed files with 59 additions and 11 deletions

View File

@ -1106,16 +1106,19 @@ class BNote extends AbstractBeccaEntity {
return minDistance; return minDistance;
} }
/** @returns {BNoteRevision[]} */
getNoteRevisions() { getNoteRevisions() {
return sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]) return sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId])
.map(row => new BNoteRevision(row)); .map(row => new BNoteRevision(row));
} }
/** @returns {BNoteAttachment[]} */
getNoteAttachments() { getNoteAttachments() {
return sql.getRows("SELECT * FROM note_attachments WHERE noteId = ? AND isDeleted = 0", [this.noteId]) return sql.getRows("SELECT * FROM note_attachments WHERE noteId = ? AND isDeleted = 0", [this.noteId])
.map(row => new BNoteAttachment(row)); .map(row => new BNoteAttachment(row));
} }
/** @returns {BNoteAttachment|undefined} */
getNoteAttachmentByName(name) { getNoteAttachmentByName(name) {
return sql.getRows("SELECT * FROM note_attachments WHERE noteId = ? AND name = ? AND isDeleted = 0", [this.noteId, name]) return sql.getRows("SELECT * FROM note_attachments WHERE noteId = ? AND name = ? AND isDeleted = 0", [this.noteId, name])
.map(row => new BNoteAttachment(row)) .map(row => new BNoteAttachment(row))
@ -1442,6 +1445,7 @@ class BNote extends AbstractBeccaEntity {
this.getNoteAttachments() this.getNoteAttachments()
const noteAttachment = new BNoteAttachment({ const noteAttachment = new BNoteAttachment({
noteId: this.noteId,
name, name,
mime, mime,
isProtected: this.isProtected isProtected: this.isProtected

View File

@ -125,12 +125,12 @@ class BNoteAttachment extends AbstractBeccaEntity {
return { return {
noteAttachmentId: this.noteAttachmentId, noteAttachmentId: this.noteAttachmentId,
noteId: this.noteId, noteId: this.noteId,
mime: this.mime,
isProtected: this.isProtected,
name: this.name, name: this.name,
mime: this.mime,
isProtected: !!this.isProtected,
isDeleted: false,
utcDateModified: this.utcDateModified, utcDateModified: this.utcDateModified,
content: this.content, content: this.content,
contentLength: this.contentLength
}; };
} }

View File

@ -88,8 +88,12 @@ export default class RootCommandExecutor extends Component {
await this.showAndHoistSubtree('_hidden'); await this.showAndHoistSubtree('_hidden');
} }
async showOptionsCommand() { async showOptionsCommand({section}) {
await this.showAndHoistSubtree('_options'); await appContext.tabManager.openContextWithNote(
section || '_options',
true,
null,
'_options');
} }
async showSQLConsoleHistoryCommand() { async showSQLConsoleHistoryCommand() {

View File

@ -28,7 +28,7 @@ export default class PasswordNoteSetDialog extends BasicWidget {
this.$widget = $(TPL); this.$widget = $(TPL);
this.$openPasswordOptionsButton = this.$widget.find(".open-password-options-button"); this.$openPasswordOptionsButton = this.$widget.find(".open-password-options-button");
this.$openPasswordOptionsButton.on("click", () => { this.$openPasswordOptionsButton.on("click", () => {
this.triggerCommand("showOptions", { openTab: 'PasswordOptions' }); this.triggerCommand("showOptions", { section: '_optionsPassword' });
}); });
} }

View File

@ -8,6 +8,7 @@ import hoistedNoteService from "../../services/hoisted_note.js";
import BasicWidget from "../basic_widget.js"; import BasicWidget from "../basic_widget.js";
import dialogService from "../../services/dialog.js"; import dialogService from "../../services/dialog.js";
import toastService from "../../services/toast.js"; import toastService from "../../services/toast.js";
import ws from "../../services/ws.js";
const TPL = ` const TPL = `
<div class="recent-changes-dialog modal fade mx-auto" tabindex="-1" role="dialog"> <div class="recent-changes-dialog modal fade mx-auto" tabindex="-1" role="dialog">
@ -94,9 +95,9 @@ export default class RecentChangesDialog extends BasicWidget {
this.$widget.modal('hide'); this.$widget.modal('hide');
await froca.reloadNotes([change.noteId]); setTimeout(() => {
appContext.tabManager.getActiveContext().setNote(change.noteId); appContext.tabManager.getActiveContext().setNote(change.noteId);
}, 1000);
} }
}); });

View File

@ -0,0 +1,37 @@
const protectedSession = require("./protected_session.js");
const log = require("./log.js");
/**
* @param {BNote} note
*/
function protectNoteAttachments(note) {
for (const noteAttachment of note.getNoteAttachments()) {
if (note.isProtected !== noteAttachment.isProtected) {
if (!protectedSession.isProtectedSessionAvailable()) {
log.error("Protected session is not available to fix note attachments.");
return;
}
try {
const content = noteAttachment.getContent();
noteAttachment.isProtected = note.isProtected;
// this will force de/encryption
noteAttachment.setContent(content);
noteAttachment.save();
}
catch (e) {
log.error(`Could not un/protect note attachment ID = ${noteAttachment.noteAttachmentId}`);
throw e;
}
}
}
}
module.exports = {
protectNoteAttachments
}

View File

@ -9,6 +9,7 @@ const protectedSessionService = require('../services/protected_session');
const log = require('../services/log'); const log = require('../services/log');
const utils = require('../services/utils'); const utils = require('../services/utils');
const noteRevisionService = require('../services/note_revisions'); const noteRevisionService = require('../services/note_revisions');
const noteAttachmentService = require('../services/note_attachments');
const attributeService = require('../services/attributes'); const attributeService = require('../services/attributes');
const request = require('./request'); const request = require('./request');
const path = require('path'); const path = require('path');
@ -17,11 +18,11 @@ const becca = require('../becca/becca');
const BBranch = require('../becca/entities/bbranch'); const BBranch = require('../becca/entities/bbranch');
const BNote = require('../becca/entities/bnote'); const BNote = require('../becca/entities/bnote');
const BAttribute = require('../becca/entities/battribute'); const BAttribute = require('../becca/entities/battribute');
const BNoteAttachment = require("../becca/entities/bnote_attachment");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const htmlSanitizer = require("./html_sanitizer"); const htmlSanitizer = require("./html_sanitizer");
const ValidationError = require("../errors/validation_error"); const ValidationError = require("../errors/validation_error");
const noteTypesService = require("./note_types"); const noteTypesService = require("./note_types");
const BNoteAttachment = require("../becca/entities/bnote_attachment.js");
function getNewNotePosition(parentNoteId) { function getNewNotePosition(parentNoteId) {
const note = becca.notes[parentNoteId]; const note = becca.notes[parentNoteId];
@ -300,6 +301,7 @@ function protectNote(note, protect) {
} }
noteRevisionService.protectNoteRevisions(note); noteRevisionService.protectNoteRevisions(note);
noteAttachmentService.protectNoteAttachments(note);
} }
catch (e) { catch (e) {
log.error(`Could not un/protect note ID = ${note.noteId}`); log.error(`Could not un/protect note ID = ${note.noteId}`);

View File

@ -2,7 +2,6 @@
const log = require('./log'); const log = require('./log');
const dataEncryptionService = require('./data_encryption'); const dataEncryptionService = require('./data_encryption');
const options = require("./options");
let dataKey = null; let dataKey = null;
@ -64,6 +63,7 @@ function touchProtectedSession() {
} }
function checkProtectedSessionExpiration() { function checkProtectedSessionExpiration() {
const options = require("./options");
const protectedSessionTimeout = options.getOptionInt('protectedSessionTimeout'); const protectedSessionTimeout = options.getOptionInt('protectedSessionTimeout');
if (isProtectedSessionAvailable() if (isProtectedSessionAvailable()
&& lastProtectedSessionOperationDate && lastProtectedSessionOperationDate