Skip to content

Build your personal knowledge base with Trilium Notes

rename attachment

zadam34c642a49a20
diff --git a/src/public/app/widgets/buttons/attachments_actions.js b/src/public/app/widgets/buttons/attachments_actions.js
index 20e5a7d038..5480ded249 100644
--- a/src/public/app/widgets/buttons/attachments_actions.js
+++ b/src/public/app/widgets/buttons/attachments_actions.js
@@ -26,12 +26,14 @@ const TPL = `
</style>
<button type="button" data-toggle="dropdown" aria-haspopup="true"
- aria-expanded="false" class="icon-action icon-action-always-border bx bx-dots-vertical-rounded"></button>
+ aria-expanded="false" class="icon-action icon-action-always-border bx bx-dots-vertical-rounded"
+ style="position: relative; top: 5px;"></button>
<div class="dropdown-menu dropdown-menu-right">
<a data-trigger-command="openAttachment" class="dropdown-item"
title="File will be open in an external application and watched for changes. You'll then be able to upload the modified version back to Trilium.">Open externally</a>
<a data-trigger-command="downloadAttachment" class="dropdown-item">Download</a>
+ <a data-trigger-command="renameAttachment" class="dropdown-item">Rename attachment</a>
<a data-trigger-command="uploadNewAttachmentRevision" class="dropdown-item">Upload new revision</a>
<a data-trigger-command="copyAttachmentLinkToClipboard" class="dropdown-item">Copy link to clipboard</a>
<a data-trigger-command="convertAttachmentIntoNote" class="dropdown-item">Convert attachment into note</a>
@@ -129,4 +131,18 @@ export default class AttachmentActionsWidget extends BasicWidget {
await ws.waitForMaxKnownEntityChangeId();
await appContext.tabManager.getActiveContext().setNote(newNote.noteId);
}
+
+ async renameAttachmentCommand() {
+ const attachmentTitle = await dialogService.prompt({
+ title: "Rename attachment",
+ message: "Please enter new attachment's name",
+ defaultValue: this.attachment.title
+ });
+
+ if (!attachmentTitle?.trim()) {
+ return;
+ }
+
+ await server.put(`attachments/${this.attachmentId}/rename`, {title: attachmentTitle});
+ }
}
diff --git a/src/public/app/widgets/type_widgets/attachment_detail.js b/src/public/app/widgets/type_widgets/attachment_detail.js
index f3636176b3..d336879de7 100644
--- a/src/public/app/widgets/type_widgets/attachment_detail.js
+++ b/src/public/app/widgets/type_widgets/attachment_detail.js
@@ -14,8 +14,8 @@ const TPL = `
}
.attachment-detail .links-wrapper {
- padding: 16px;
font-size: larger;
+ padding: 16px 0 16px 0;
}
.attachment-detail .attachment-wrapper {
diff --git a/src/public/app/widgets/type_widgets/options/etapi.js b/src/public/app/widgets/type_widgets/options/etapi.js
index 2fa773f8f3..2de97c738f 100644
--- a/src/public/app/widgets/type_widgets/options/etapi.js
+++ b/src/public/app/widgets/type_widgets/options/etapi.js
@@ -109,8 +109,8 @@ export default class EtapiOptions extends OptionsWidget {
message: "Please enter new token's name",
defaultValue: oldName
});
-
- if(tokenName === null) {
+
+ if (!tokenName?.trim()) {
return;
}
diff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.css
index 9603dd032b..63dca0def8 100644
--- a/src/public/stylesheets/style.css
+++ b/src/public/stylesheets/style.css
@@ -110,7 +110,7 @@ button.close:hover {
.icon-action {
border: 1px solid transparent;
padding: 5px;
- width: 35px;
+ width: 37px;
height: 35px;
cursor: pointer;
font-size: 1.5em;
diff --git a/src/routes/api/attachments.js b/src/routes/api/attachments.js
index 5b8049b241..4714b0fe5c 100644
--- a/src/routes/api/attachments.js
+++ b/src/routes/api/attachments.js
@@ -1,5 +1,6 @@
const becca = require("../../becca/becca");
const blobService = require("../../services/blob.js");
+const ValidationError = require("../../errors/validation_error");
function getAttachmentBlob(req) {
const preview = req.query.preview === 'true';
@@ -41,7 +42,7 @@ function uploadAttachment(req) {
const note = becca.getNoteOrThrow(noteId);
- const attachment = note.saveAttachment({
+ note.saveAttachment({
role: 'file',
mime: file.mimetype,
title: file.originalname,
@@ -53,6 +54,20 @@ function uploadAttachment(req) {
};
}
+function renameAttachment(req) {
+ const {title} = req.body;
+ const {attachmentId} = req.params;
+
+ const attachment = becca.getAttachmentOrThrow(attachmentId);
+
+ if (!title?.trim()) {
+ throw new ValidationError("Title must not be empty");
+ }
+
+ attachment.title = title;
+ attachment.save();
+}
+
function deleteAttachment(req) {
const {attachmentId} = req.params;
@@ -77,6 +92,7 @@ module.exports = {
getAllAttachments,
saveAttachment,
uploadAttachment,
+ renameAttachment,
deleteAttachment,
convertAttachmentToNote
};
diff --git a/src/routes/api/files.js b/src/routes/api/files.js
index 044865707d..4da4f53a61 100644
--- a/src/routes/api/files.js
+++ b/src/routes/api/files.js
@@ -35,6 +35,7 @@ function updateFile(req) {
function updateAttachment(req) {
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
const file = req.file;
+
attachment.getNote().saveRevision();
attachment.mime = file.mimetype.toLowerCase();
diff --git a/src/routes/routes.js b/src/routes/routes.js
index 3869b7136d..c91d09fff9 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -159,6 +159,7 @@ function register(app) {
apiRoute(GET, '/api/attachments/:attachmentId/all', attachmentsApiRoute.getAllAttachments);
apiRoute(PST, '/api/attachments/:attachmentId/convert-to-note', attachmentsApiRoute.convertAttachmentToNote);
apiRoute(DEL, '/api/attachments/:attachmentId', attachmentsApiRoute.deleteAttachment);
+ apiRoute(PUT, '/api/attachments/:attachmentId/rename', attachmentsApiRoute.renameAttachment);
apiRoute(GET, '/api/attachments/:attachmentId/blob', attachmentsApiRoute.getAttachmentBlob);
route(GET, '/api/attachments/:attachmentId/image/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnAttachedImage);
route(GET, '/api/attachments/:attachmentId/open', [auth.checkApiAuthOrElectron], filesRoute.openAttachment);