Files
vue-chat-layer/app/lib/attachments.ts
Jakub Zych 269c75b5dd feat: threads, pins, reactions, attachments, link embeds and the command picker
- threads: reply counts, ChatThreadPane with replies, live replies and thread mute
- channel pins in ChatPinsPanel; a refused pin hides the action for the session
- reactions, attachments with upload progress and bearer downloads, inline edits, the delete dialog (moderators also in channels)
- link cards with the registerEmbedRenderer registry; linkify for escaped text
- slash commands: the command picker over chatbots, dispatch with sender-only answers
- slots conversation-actions (DMs only) and sidebar-footer
- chat-tokens.css and chat-base.css, EN and PL copy, README
2026-10-31 14:04:54 +01:00

102 lines
3.6 KiB
TypeScript

// Client-side attachment checks, ported from a Golem15 SPA. They mirror
// golem15.chat's ChatAttachmentPolicy so a refused file never uploads; the
// server's checks (RejectBanned, the per-file and body limits) stay
// authoritative. The error copy defaults to the SPA's Polish; the layer
// passes its localized messages.
import type { ChatLimits } from './types'
/** golem15.chat BannedExts: script-capable types (stored XSS). */
export const BANNED_EXTENSIONS: readonly string[] = ['svg', 'svgz', 'html', 'htm', 'xhtml', 'xml']
/** golem15.chat BannedMIMEs. */
export const BANNED_MIME_TYPES: readonly string[] = ['image/svg+xml', 'text/html', 'application/xhtml+xml', 'text/xml', 'application/xml']
/** What checkFiles needs of a File. */
export interface FileLike {
name: string
size: number
type: string
}
export interface FileCheck<F extends FileLike> {
accepted: F[]
/** The error lines, one per kind (too-large names each file). */
errors: string[]
}
/** A byte limit in MB for the copy: 62914560 → "60", 1572864 → "1,5". */
export function mbLabel(bytes: number): string {
const mb = bytes / (1024 * 1024)
if (Number.isInteger(mb)) return String(mb)
return (Math.floor(mb * 10) / 10).toFixed(1).replace('.', ',')
}
/** True for SVG, HTML and XML, by extension or MIME type. */
export function isBanned(file: FileLike): boolean {
const dot = file.name.lastIndexOf('.')
const ext = (dot >= 0 ? file.name.slice(dot + 1) : file.name).toLowerCase()
if (BANNED_EXTENSIONS.includes(ext)) return true
const mime = (file.type || '').split(';')[0].trim().toLowerCase()
return BANNED_MIME_TYPES.includes(mime)
}
/** The error lines of checkFiles. */
export interface FileCheckMessages {
tooLarge(name: string, mb: string): string
overTotal(mb: string): string
banned(): string
tooMany(max: number): string
}
const PL_MESSAGES: FileCheckMessages = {
tooLarge: (name, mb) => `Plik „${name}” jest za duży. Limit to ${mb} MB.`,
overTotal: (mb) => `Załączniki jednej wiadomości mogą mieć razem najwyżej ${mb} MB.`,
banned: () => 'Plików SVG, HTML i XML nie można wysyłać na czacie.',
tooMany: (max) => `Możesz dołączyć najwyżej ${max} plików do jednej wiadomości.`,
}
/**
* Splits picked files into those that may upload and the error lines for
* the rest: banned types, files that would take the message's attachments
* above limits.max_bytes in total (stagedBytes is what is already staged),
* and anything over limits.max_files counting the chips already staged.
*/
export function checkFiles<F extends FileLike>(
files: readonly F[],
limits: Pick<ChatLimits, 'max_files' | 'max_bytes'>,
stagedCount: number,
stagedBytes = 0,
messages: FileCheckMessages = PL_MESSAGES,
): FileCheck<F> {
const accepted: F[] = []
const errors: string[] = []
let banned = false
let tooMany = false
let overTotal = false
let total = stagedBytes
for (const f of files) {
if (isBanned(f)) {
banned = true
continue
}
if (limits.max_bytes > 0 && f.size > limits.max_bytes) {
errors.push(messages.tooLarge(f.name, mbLabel(limits.max_bytes)))
continue
}
if (stagedCount + accepted.length >= limits.max_files) {
tooMany = true
continue
}
if (limits.max_bytes > 0 && total + f.size > limits.max_bytes) {
overTotal = true
continue
}
total += f.size
accepted.push(f)
}
if (overTotal) errors.unshift(messages.overTotal(mbLabel(limits.max_bytes)))
if (banned) errors.unshift(messages.banned())
if (tooMany) errors.unshift(messages.tooMany(limits.max_files))
return { accepted, errors }
}