rename attachment

This commit is contained in:
zadam 2023-06-14 00:28:59 +02:00
parent 2ebbc33081
commit 34c642a49a
7 changed files with 40 additions and 6 deletions

View File

@ -26,12 +26,14 @@ const TPL = `
</style> </style>
<button type="button" data-toggle="dropdown" aria-haspopup="true" <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"> <div class="dropdown-menu dropdown-menu-right">
<a data-trigger-command="openAttachment" class="dropdown-item" <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> 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="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="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="copyAttachmentLinkToClipboard" class="dropdown-item">Copy link to clipboard</a>
<a data-trigger-command="convertAttachmentIntoNote" class="dropdown-item">Convert attachment into note</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 ws.waitForMaxKnownEntityChangeId();
await appContext.tabManager.getActiveContext().setNote(newNote.noteId); 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});
}
} }

View File

@ -14,8 +14,8 @@ const TPL = `
} }
.attachment-detail .links-wrapper { .attachment-detail .links-wrapper {
padding: 16px;
font-size: larger; font-size: larger;
padding: 16px 0 16px 0;
} }
.attachment-detail .attachment-wrapper { .attachment-detail .attachment-wrapper {

View File

@ -110,7 +110,7 @@ export default class EtapiOptions extends OptionsWidget {
defaultValue: oldName defaultValue: oldName
}); });
if(tokenName === null) { if (!tokenName?.trim()) {
return; return;
} }

View File

@ -110,7 +110,7 @@ button.close:hover {
.icon-action { .icon-action {
border: 1px solid transparent; border: 1px solid transparent;
padding: 5px; padding: 5px;
width: 35px; width: 37px;
height: 35px; height: 35px;
cursor: pointer; cursor: pointer;
font-size: 1.5em; font-size: 1.5em;

View File

@ -1,5 +1,6 @@
const becca = require("../../becca/becca"); const becca = require("../../becca/becca");
const blobService = require("../../services/blob.js"); const blobService = require("../../services/blob.js");
const ValidationError = require("../../errors/validation_error");
function getAttachmentBlob(req) { function getAttachmentBlob(req) {
const preview = req.query.preview === 'true'; const preview = req.query.preview === 'true';
@ -41,7 +42,7 @@ function uploadAttachment(req) {
const note = becca.getNoteOrThrow(noteId); const note = becca.getNoteOrThrow(noteId);
const attachment = note.saveAttachment({ note.saveAttachment({
role: 'file', role: 'file',
mime: file.mimetype, mime: file.mimetype,
title: file.originalname, 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) { function deleteAttachment(req) {
const {attachmentId} = req.params; const {attachmentId} = req.params;
@ -77,6 +92,7 @@ module.exports = {
getAllAttachments, getAllAttachments,
saveAttachment, saveAttachment,
uploadAttachment, uploadAttachment,
renameAttachment,
deleteAttachment, deleteAttachment,
convertAttachmentToNote convertAttachmentToNote
}; };

View File

@ -35,6 +35,7 @@ function updateFile(req) {
function updateAttachment(req) { function updateAttachment(req) {
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId); const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
const file = req.file; const file = req.file;
attachment.getNote().saveRevision(); attachment.getNote().saveRevision();
attachment.mime = file.mimetype.toLowerCase(); attachment.mime = file.mimetype.toLowerCase();

View File

@ -159,6 +159,7 @@ function register(app) {
apiRoute(GET, '/api/attachments/:attachmentId/all', attachmentsApiRoute.getAllAttachments); apiRoute(GET, '/api/attachments/:attachmentId/all', attachmentsApiRoute.getAllAttachments);
apiRoute(PST, '/api/attachments/:attachmentId/convert-to-note', attachmentsApiRoute.convertAttachmentToNote); apiRoute(PST, '/api/attachments/:attachmentId/convert-to-note', attachmentsApiRoute.convertAttachmentToNote);
apiRoute(DEL, '/api/attachments/:attachmentId', attachmentsApiRoute.deleteAttachment); apiRoute(DEL, '/api/attachments/:attachmentId', attachmentsApiRoute.deleteAttachment);
apiRoute(PUT, '/api/attachments/:attachmentId/rename', attachmentsApiRoute.renameAttachment);
apiRoute(GET, '/api/attachments/:attachmentId/blob', attachmentsApiRoute.getAttachmentBlob); apiRoute(GET, '/api/attachments/:attachmentId/blob', attachmentsApiRoute.getAttachmentBlob);
route(GET, '/api/attachments/:attachmentId/image/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnAttachedImage); route(GET, '/api/attachments/:attachmentId/image/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnAttachedImage);
route(GET, '/api/attachments/:attachmentId/open', [auth.checkApiAuthOrElectron], filesRoute.openAttachment); route(GET, '/api/attachments/:attachmentId/open', [auth.checkApiAuthOrElectron], filesRoute.openAttachment);