// Attachment type helpers shared by main (file names) and renderer (display). import type { MediaRef } from './types' const EXT_BY_MIME: Record = { 'image/jpeg': 'jpg', 'image/png': 'png', 'image/gif': 'gif', 'image/webp': 'webp', 'image/bmp': 'bmp', 'video/mp4': 'mp4', 'video/3gpp': '3gp', 'video/quicktime': 'mov', 'audio/mpeg': 'mp3', 'audio/mp4': 'm4a', 'audio/amr': 'amr', 'audio/ogg': 'ogg', 'text/vcard': 'vcf', 'text/x-vcard': 'vcf', 'text/plain': 'txt' } const MIME_BY_EXT: Record = { ...Object.fromEntries(Object.entries(EXT_BY_MIME).map(([m, e]) => [e, m])), jpeg: 'image/jpeg' } export function guessMime(nameOrUrl: string): string | undefined { const ext = /\.([a-z0-9]+)(?:[?#].*)?$/i.exec(nameOrUrl)?.[1]?.toLowerCase() return ext ? MIME_BY_EXT[ext] : undefined } export function extFor(mime: string | undefined): string { return (mime && EXT_BY_MIME[mime.split(';')[0].trim()]) || 'bin' } export type MediaKind = 'image' | 'video' | 'audio' | 'file' export function mediaKind(ref: MediaRef): MediaKind { const mime = ref.mime ?? guessMime(ref.file ?? ref.url ?? '') ?? '' const top = mime.split('/')[0] return top === 'image' || top === 'video' || top === 'audio' ? top : 'file' }