diff --git a/src/share/content_renderer.js b/src/share/content_renderer.js index d73fa6206..341cb61d0 100644 --- a/src/share/content_renderer.js +++ b/src/share/content_renderer.js @@ -62,12 +62,29 @@ function renderText(result, note) { for (const linkEl of document.querySelectorAll("a")) { const href = linkEl.getAttribute("href"); - if (href?.startsWith("#")) { - const notePathSegments = href.split("/"); + if (!href?.startsWith("#")) { + continue; + } + const linkRegExp = /attachmentId=([a-zA-Z0-9_]+)/g; + let attachmentMatch + if (attachmentMatch = linkRegExp.exec(href)) { + const attachmentId = attachmentMatch[1]; + const attachment = shaca.getAttachment(attachmentId); + + if (attachment) { + linkEl.setAttribute("href", `api/attachments/${attachmentId}/download`); + linkEl.classList.add(`attachment-link`); + linkEl.classList.add(`role-${attachment.role}`); + linkEl.innerText = attachment.title; + } else { + linkEl.removeAttribute("href"); + } + } else { + const [notePath] = href.split('?'); + const notePathSegments = notePath.split("/"); const noteId = notePathSegments[notePathSegments.length - 1]; const linkedNote = shaca.getNote(noteId); - if (linkedNote) { linkEl.setAttribute("href", linkedNote.shareId); linkEl.classList.add(`type-${linkedNote.type}`); diff --git a/src/share/routes.js b/src/share/routes.js index b0cb1b7cf..b33ace112 100644 --- a/src/share/routes.js +++ b/src/share/routes.js @@ -8,6 +8,7 @@ const shareRoot = require("./share_root"); const contentRenderer = require("./content_renderer"); const assetPath = require("../services/asset_path"); const appPath = require("../services/app_path"); +const utils = require("../services/utils.js"); function getSharedSubTreeRoot(note) { if (note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) { @@ -253,6 +254,29 @@ function register(router) { } }); + router.get('/share/api/attachments/:attachmentId/download', (req, res, next) => { + shacaLoader.ensureLoad(); + + let attachment; + + if (!(attachment = checkAttachmentAccess(req.params.attachmentId, req, res))) { + return; + } + + addNoIndexHeader(attachment.note, res); + + const utils = require("../services/utils"); + + const filename = utils.formatDownloadTitle(attachment.title, null, attachment.mime); + + res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); + + res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + res.setHeader('Content-Type', attachment.mime); + + res.send(attachment.getContent()); + }); + // used for PDF viewing router.get('/share/api/notes/:noteId/view', (req, res, next) => { shacaLoader.ensureLoad();