diff --git a/db/migrations/0218__migrate_images_to_attachments.js b/db/migrations/0218__migrate_images_to_attachments.js index 5100abe06..76ba971da 100644 --- a/db/migrations/0218__migrate_images_to_attachments.js +++ b/db/migrations/0218__migrate_images_to_attachments.js @@ -11,11 +11,13 @@ module.exports = () => { try { const attachment = note.convertToParentAttachment({force: false}); - log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`) + if (attachment) { + 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 file +}; diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js index 2c459663c..69ecffa0b 100644 --- a/src/becca/entities/bnote.js +++ b/src/becca/entities/bnote.js @@ -1086,11 +1086,15 @@ class BNote extends AbstractBeccaEntity { .map(row => new BAttachment(row)); } - /** @returns {BAttachment|undefined} */ - getAttachmentByName(name) { - return sql.getRows("SELECT * FROM attachments WHERE parentId = ? AND name = ? AND isDeleted = 0", [this.noteId, name]) - .map(row => new BAttachment(row)) - [0]; + /** @returns {BAttachment|null} */ + getAttachmentById(attachmentId) { + return sql.getRows(` + SELECT attachments.* + FROM attachments + WHERE parentId = ? + AND attachmentId = ? + AND isDeleted = 0`, [this.noteId, attachmentId]) + .map(row => new BAttachment(row))[0]; } /** diff --git a/src/public/app/widgets/attachment_detail.js b/src/public/app/widgets/attachment_detail.js new file mode 100644 index 000000000..bf8a1fb5d --- /dev/null +++ b/src/public/app/widgets/attachment_detail.js @@ -0,0 +1,101 @@ +import TypeWidget from "./type_widget.js"; +import server from "../../services/server.js"; +import utils from "../../services/utils.js"; +import AttachmentActionsWidget from "../buttons/attachments_actions.js"; +import BasicWidget from "./basic_widget.js"; + +const TPL = ` +
`; + +export default class AttachmentDetailWidget extends BasicWidget { + constructor(attachment) { + super(); + + this.attachment = attachment; + } + + doRender() { + this.$widget = $(TPL); + this.$wrapper = this.$widget.find('.attachment-detail-wrapper'); + + super.doRender(); + } + + async doRefresh(note) { + this.$list.empty(); + this.children = []; + + const attachments = await server.get(`notes/${this.noteId}/attachments?includeContent=true`); + + if (attachments.length === 0) { + this.$list.html("This note has no attachments."); + + return; + } + + for (const attachment of attachments) { + const attachmentActionsWidget = new AttachmentActionsWidget(attachment); + this.child(attachmentActionsWidget); + + this.$list.append( + $('