|
| 1 | +/** |
| 2 | + * Public recipient endpoints for share links (no authentication required, though an |
| 3 | + * authenticated session is honoured for AUTHENTICATED-mode shares). Mounted under /s. |
| 4 | + * |
| 5 | + * Access policy (expiry, code, sign-in, download cap, download-disabled) is enforced |
| 6 | + * identically here for both metadata and bytes via services/share.ts. A `code` is passed |
| 7 | + * as a query parameter so these stay simple GETs (no CSRF surface). |
| 8 | + */ |
| 9 | +import type { FastifyPluginAsync } from 'fastify'; |
| 10 | +import type { PublicShareView } from '@opencoperlock/shared'; |
| 11 | +import { prisma } from '../db.js'; |
| 12 | +import { decideAccess, isExhausted, isExpired, shareEntries, shareFile } from '../services/share.js'; |
| 13 | +import { decryptServerFile } from '../services/download.js'; |
| 14 | +import { audit } from '../services/audit.js'; |
| 15 | + |
| 16 | +export const sharePublicRoutes: FastifyPluginAsync = async (app) => { |
| 17 | + // GET /s/:token — recipient metadata. Returns entries only once access is granted. |
| 18 | + app.get('/:token', async (req, reply) => { |
| 19 | + const { token } = req.params as { token: string }; |
| 20 | + const { code } = req.query as { code?: string }; |
| 21 | + const share = await prisma.shareLink.findUnique({ where: { token } }); |
| 22 | + if (!share) return reply.code(404).send({ error: 'Link not found' }); |
| 23 | + |
| 24 | + const base: PublicShareView = { |
| 25 | + token: share.token, |
| 26 | + viewType: share.viewType, |
| 27 | + access: share.access, |
| 28 | + allowDownload: share.allowDownload, |
| 29 | + isFolder: share.folderId !== null, |
| 30 | + }; |
| 31 | + |
| 32 | + if (isExpired(share) || isExhausted(share)) { |
| 33 | + return { ...base, expired: true }; |
| 34 | + } |
| 35 | + |
| 36 | + const decision = await decideAccess(share, { code, userId: req.user?.id ?? null }); |
| 37 | + if (!decision.allowed) { |
| 38 | + return { ...base, requiresCode: decision.requiresCode, requiresAuth: decision.requiresAuth }; |
| 39 | + } |
| 40 | + |
| 41 | + const entries = await shareEntries(share); |
| 42 | + return share.folderId ? { ...base, entries } : { ...base, file: entries[0] }; |
| 43 | + }); |
| 44 | + |
| 45 | + // GET /s/:token/file/:fileId — stream a shared file. `inline=1` previews without |
| 46 | + // counting against the download cap; otherwise it's an attachment and is counted. |
| 47 | + app.get('/:token/file/:fileId', async (req, reply) => { |
| 48 | + const { token, fileId } = req.params as { token: string; fileId: string }; |
| 49 | + const { code, inline } = req.query as { code?: string; inline?: string }; |
| 50 | + const isInline = inline === '1'; |
| 51 | + |
| 52 | + const share = await prisma.shareLink.findUnique({ where: { token } }); |
| 53 | + if (!share) return reply.code(404).send({ error: 'Link not found' }); |
| 54 | + if (isExpired(share) || isExhausted(share)) return reply.code(410).send({ error: 'Link expired' }); |
| 55 | + |
| 56 | + const decision = await decideAccess(share, { code, userId: req.user?.id ?? null }); |
| 57 | + if (!decision.allowed) { |
| 58 | + return reply.code(decision.requiresAuth ? 401 : 403).send({ error: 'Access denied' }); |
| 59 | + } |
| 60 | + if (!share.allowDownload && !isInline) { |
| 61 | + return reply.code(403).send({ error: 'Downloads are disabled for this link' }); |
| 62 | + } |
| 63 | + |
| 64 | + const file = await shareFile(share, fileId); |
| 65 | + if (!file) return reply.code(404).send({ error: 'File not found' }); |
| 66 | + if (file.encMode === 'ZK') return reply.code(400).send({ error: 'Unsupported' }); |
| 67 | + |
| 68 | + if (!isInline) { |
| 69 | + // Count the download atomically and respect the cap under concurrency. |
| 70 | + const claimed = await prisma.shareLink.updateMany({ |
| 71 | + where: { |
| 72 | + id: share.id, |
| 73 | + OR: [{ maxDownloads: null }, { downloadCount: { lt: share.maxDownloads ?? 0 } }], |
| 74 | + }, |
| 75 | + data: { downloadCount: { increment: 1 } }, |
| 76 | + }); |
| 77 | + if (claimed.count === 0) return reply.code(410).send({ error: 'Link expired' }); |
| 78 | + await audit(req, 'share.download', { actorId: null, target: share.id }); |
| 79 | + } |
| 80 | + |
| 81 | + const disposition = isInline ? 'inline' : 'attachment'; |
| 82 | + reply |
| 83 | + .header('Content-Type', file.mimeType) |
| 84 | + .header('Content-Length', Number(file.sizeBytes)) |
| 85 | + .header('Content-Disposition', `${disposition}; filename="${encodeURIComponent(file.name)}"`); |
| 86 | + return reply.send(decryptServerFile(app.ctx, file)); |
| 87 | + }); |
| 88 | +}; |
0 commit comments