share support for attachment images

This commit is contained in:
zadam
2023-06-05 23:05:05 +02:00
parent 69c7eb14aa
commit 44bcfd47c0
8 changed files with 151 additions and 8 deletions

View File

@@ -37,13 +37,30 @@ function requestCredentials(res) {
.sendStatus(401);
}
/** @returns {SAttachment|boolean} */
function checkAttachmentAccess(attachmentId, req, res) {
const attachment = shaca.getAttachment(attachmentId);
if (!attachment) {
res.status(404)
.json({ message: `Attachment '${attachmentId}' not found.` });
return false;
}
const note = checkNoteAccess(attachment.parentId, req, res);
// truthy note means user has access, and we can return the attachment
return note ? attachment : false;
}
/** @returns {SNote|boolean} */
function checkNoteAccess(noteId, req, res) {
const note = shaca.getNote(noteId);
if (!note) {
res.status(404)
.json({ message: `Note '${noteId}' not found` });
.json({ message: `Note '${noteId}' not found.` });
return false;
}
@@ -151,7 +168,7 @@ function register(router) {
addNoIndexHeader(note, res);
res.json(note.getPojoWithAttributes());
res.json(note.getPojo());
});
router.get('/share/api/notes/:noteId/download', (req, res, next) => {
@@ -216,6 +233,26 @@ function register(router) {
}
});
// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
router.get('/share/api/attachments/:attachmentId/image/:filename', (req, res, next) => {
shacaLoader.ensureLoad();
let attachment;
if (!(attachment = checkAttachmentAccess(req.params.attachmentId, req, res))) {
return;
}
if (attachment.role === "image") {
res.set('Content-Type', attachment.mime);
addNoIndexHeader(attachment.note, res);
res.send(attachment.getContent());
} else {
return res.status(400)
.json({ message: "Requested attachment is not a shareable image" });
}
});
// used for PDF viewing
router.get('/share/api/notes/:noteId/view', (req, res, next) => {
shacaLoader.ensureLoad();