improved saving attachment

This commit is contained in:
zadam 2023-03-16 16:37:31 +01:00
parent 2b84f1be00
commit d83005fe4d
6 changed files with 50 additions and 31 deletions

View File

@ -125,7 +125,10 @@ class AbstractBeccaEntity {
} }
/** @protected */ /** @protected */
_setContent(content) { _setContent(content, opts = {}) {
// client code asks to save entity even if blobId didn't change (something else was changed)
opts.forceSave = !!opts.forceSave;
if (content === null || content === undefined) { if (content === null || content === undefined) {
throw new Error(`Cannot set null content to ${this.constructor.primaryKeyName} '${this[this.constructor.primaryKeyName]}'`); throw new Error(`Cannot set null content to ${this.constructor.primaryKeyName} '${this[this.constructor.primaryKeyName]}'`);
} }
@ -149,7 +152,7 @@ class AbstractBeccaEntity {
sql.transactional(() => { sql.transactional(() => {
let newBlobId = this._saveBlob(content); let newBlobId = this._saveBlob(content);
if (newBlobId !== this.blobId) { if (newBlobId !== this.blobId || opts.forceSave) {
this.blobId = newBlobId; this.blobId = newBlobId;
this.save(); this.save();
} }

View File

@ -1,11 +1,8 @@
"use strict"; "use strict";
const protectedSessionService = require('../../services/protected_session');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const sql = require('../../services/sql');
const dateUtils = require('../../services/date_utils'); const dateUtils = require('../../services/date_utils');
const becca = require('../becca'); const becca = require('../becca');
const entityChangesService = require('../../services/entity_changes');
const AbstractBeccaEntity = require("./abstract_becca_entity"); const AbstractBeccaEntity = require("./abstract_becca_entity");
/** /**
@ -34,7 +31,7 @@ class BAttachment extends AbstractBeccaEntity {
} }
/** @type {string} */ /** @type {string} */
this.attachmentId = row.attachmentId || `${this.noteId}_${this.name}`; // FIXME this.attachmentId = row.attachmentId;
/** @type {string} either noteId or noteRevisionId to which this attachment belongs */ /** @type {string} either noteId or noteRevisionId to which this attachment belongs */
this.parentId = row.parentId; this.parentId = row.parentId;
/** @type {string} */ /** @type {string} */
@ -65,8 +62,13 @@ class BAttachment extends AbstractBeccaEntity {
return this._getContent(); return this._getContent();
} }
setContent(content) { /**
this._setContent(content); * @param content
* @param {object} [opts]
* @param {object} [opts.forceSave=false] - will also save this BAttachment entity
*/
setContent(content, opts) {
this._setContent(content, opts);
} }
calculateCheckSum(content) { calculateCheckSum(content) {
@ -78,8 +80,6 @@ class BAttachment extends AbstractBeccaEntity {
throw new Error(`Name must be alphanumerical, "${this.name}" given.`); throw new Error(`Name must be alphanumerical, "${this.name}" given.`);
} }
this.attachmentId = `${this.noteId}_${this.name}`; // FIXME
super.beforeSaving(); super.beforeSaving();
this.utcDateModified = dateUtils.utcNowDateTime(); this.utcDateModified = dateUtils.utcNowDateTime();

View File

@ -5,14 +5,13 @@ const log = require('../../services/log');
const sql = require('../../services/sql'); const sql = require('../../services/sql');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const dateUtils = require('../../services/date_utils'); const dateUtils = require('../../services/date_utils');
const entityChangesService = require('../../services/entity_changes');
const AbstractBeccaEntity = require("./abstract_becca_entity"); const AbstractBeccaEntity = require("./abstract_becca_entity");
const BNoteRevision = require("./bnote_revision"); const BNoteRevision = require("./bnote_revision");
const BAttachment = require("./battachment"); const BAttachment = require("./battachment");
const TaskContext = require("../../services/task_context"); const TaskContext = require("../../services/task_context");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const utc = require('dayjs/plugin/utc'); const utc = require('dayjs/plugin/utc');
const eventService = require("../../services/events"); const NotFoundError = require("../../errors/not_found_error.js");
dayjs.extend(utc); dayjs.extend(utc);
const LABEL = 'label'; const LABEL = 'label';
@ -238,8 +237,13 @@ class BNote extends AbstractBeccaEntity {
return ['text', 'code', 'relationMap', 'canvas', 'mermaid'].includes(this.type); return ['text', 'code', 'relationMap', 'canvas', 'mermaid'].includes(this.type);
} }
setContent(content) { /**
this._setContent(content) * @param content
* @param {object} [opts]
* @param {object} [opts.forceSave=false] - will also save this BNote entity
*/
setContent(content, opts) {
this._setContent(content, opts);
} }
setJsonContent(content) { setJsonContent(content) {
@ -1414,17 +1418,26 @@ class BNote extends AbstractBeccaEntity {
/** /**
* @returns {BAttachment} * @returns {BAttachment}
*/ */
saveAttachment(name, mime, content) { saveAttachment({attachmentId, role, mime, title, content}) {
let attachment = this.getAttachmentByName(name); let attachment;
attachment = new BAttachment({ if (attachmentId) {
noteId: this.noteId, attachment = this.becca.getAttachment(attachmentId);
name,
mime,
isProtected: this.isProtected
});
attachment.setContent(content); if (!attachment) {
throw new NotFoundError(`Attachment '${attachmentId}' has not been found.`);
}
} else {
attachment = new BAttachment({
noteId: this.noteId,
title,
role,
mime,
isProtected: this.isProtected
});
}
attachment.setContent(content, { forceSave: true });
return attachment; return attachment;
} }

View File

@ -2,10 +2,8 @@
const protectedSessionService = require('../../services/protected_session'); const protectedSessionService = require('../../services/protected_session');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const sql = require('../../services/sql');
const dateUtils = require('../../services/date_utils'); const dateUtils = require('../../services/date_utils');
const becca = require('../becca'); const becca = require('../becca');
const entityChangesService = require('../../services/entity_changes');
const AbstractBeccaEntity = require("./abstract_becca_entity"); const AbstractBeccaEntity = require("./abstract_becca_entity");
/** /**
@ -79,8 +77,13 @@ class BNoteRevision extends AbstractBeccaEntity {
return this._getContent(); return this._getContent();
} }
setContent(content) { /**
this._setContent(content); * @param content
* @param {object} [opts]
* @param {object} [opts.forceSave=false] - will also save this BNoteRevision entity
*/
setContent(content, opts) {
this._setContent(content, opts);
} }
beforeSaving() { beforeSaving() {

View File

@ -158,8 +158,8 @@ function getAttachments(req) {
} }
function saveAttachment(req) { function saveAttachment(req) {
const {noteId, name} = req.params; const {noteId} = req.params;
const {mime, content} = req.body; const {attachmentId, role, mime, title, content} = req.body;
const note = becca.getNote(noteId); const note = becca.getNote(noteId);
@ -167,7 +167,7 @@ function saveAttachment(req) {
throw new NotFoundError(`Note '${noteId}' doesn't exist.`); throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
} }
note.saveAttachment(name, mime, content); note.saveAttachment({attachmentId, role, mime, title, content});
} }
function getRelationMap(req) { function getRelationMap(req) {

View File

@ -127,7 +127,7 @@ function register(app) {
apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectNote); apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectNote);
apiRoute(PUT, '/api/notes/:noteId/type', notesApiRoute.setNoteTypeMime); apiRoute(PUT, '/api/notes/:noteId/type', notesApiRoute.setNoteTypeMime);
apiRoute(GET, '/api/notes/:noteId/attachments', notesApiRoute.getAttachments); apiRoute(GET, '/api/notes/:noteId/attachments', notesApiRoute.getAttachments);
apiRoute(PUT, '/api/notes/:noteId/attachments/:name', notesApiRoute.saveAttachment); apiRoute(POST, '/api/notes/:noteId/attachments', notesApiRoute.saveAttachment);
apiRoute(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions); apiRoute(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions);
apiRoute(DELETE, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.eraseAllNoteRevisions); apiRoute(DELETE, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.eraseAllNoteRevisions);
apiRoute(GET, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.getNoteRevision); apiRoute(GET, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.getNoteRevision);