Repository Public
1/** Browser-only helpers: previews, optional compression and waveforms are all computed on the device. */2import { b64 } from "./bytes"34async function toJpeg(source: CanvasImageSource, w: number, h: number, max: number, quality: number): Promise<{ blob: Blob; w: number; h: number }> {5 const scale = Math.min(1, max / Math.max(w, h))6 const cw = Math.max(1, Math.round(w * scale)), ch = Math.max(1, Math.round(h * scale))7 const canvas = document.createElement("canvas")8 canvas.width = cw9 canvas.height = ch10 canvas.getContext("2d")!.drawImage(source, 0, 0, cw, ch)11 const blob = await new Promise<Blob>((res, rej) => canvas.toBlob((b) => (b ? res(b) : rej(new Error("encode failed"))), "image/jpeg", quality))12 return { blob, w: cw, h: ch }13}14const blobB64 = async (b: Blob) => b64(new Uint8Array(await b.arrayBuffer()))1516export type Prepared = { blob: Blob; name: string; mime: string; w?: number; h?: number; dur?: number; thumb?: string; wave?: number[] }1718/** Builds the encrypted-preview data for a file and optionally downsizes large photos. */19export async function prepare(file: File, compress: boolean): Promise<Prepared> {20 const base: Prepared = { blob: file, name: file.name || "file", mime: file.type || "application/octet-stream" }21 try {22 if (file.type.startsWith("image/") && file.type !== "image/svg+xml") {23 const bmp = await createImageBitmap(file)24 const thumb = await toJpeg(bmp, bmp.width, bmp.height, 320, 0.6)25 base.thumb = await blobB64(thumb.blob)26 base.w = bmp.width27 base.h = bmp.height28 if (compress && file.type !== "image/gif" && (Math.max(bmp.width, bmp.height) > 2560 || file.size > 3_000_000)) {29 const c = await toJpeg(bmp, bmp.width, bmp.height, 2560, 0.85)30 if (c.blob.size < file.size) {31 base.blob = c.blob32 base.mime = "image/jpeg"33 base.name = base.name.replace(/\.[a-z0-9]+$/i, "") + ".jpg"34 base.w = c.w35 base.h = c.h36 }37 }38 bmp.close()39 } else if (file.type.startsWith("video/")) {40 const url = URL.createObjectURL(file)41 try {42 const v = document.createElement("video")43 v.muted = true44 v.playsInline = true45 v.preload = "auto"46 v.src = url47 await new Promise<void>((res, rej) => {48 v.onloadeddata = () => res()49 v.onerror = () => rej(new Error("video"))50 setTimeout(() => rej(new Error("timeout")), 6000)51 })52 v.currentTime = Math.min(0.2, (v.duration || 1) / 2)53 await new Promise((res) => ((v.onseeked = res), setTimeout(res, 1500)))54 const t = await toJpeg(v, v.videoWidth, v.videoHeight, 320, 0.6)55 base.thumb = await blobB64(t.blob)56 base.w = v.videoWidth57 base.h = v.videoHeight58 base.dur = isFinite(v.duration) ? v.duration : undefined59 } finally {60 URL.revokeObjectURL(url)61 }62 } else if (file.type.startsWith("audio/")) {63 const w = await waveform(file)64 base.wave = w.peaks65 base.dur = w.duration66 }67 } catch {68 /* formats the browser cannot decode (for example HEIC outside Safari) are sent as plain files */69 }70 return base71}7273export async function waveform(blob: Blob, bars = 48): Promise<{ peaks: number[]; duration: number }> {74 const Ctx = window.AudioContext || (window as any).webkitAudioContext75 const ctx: AudioContext = new Ctx()76 try {77 const buf = await ctx.decodeAudioData(await blob.arrayBuffer())78 const data = buf.getChannelData(0)79 const step = Math.max(1, Math.floor(data.length / bars))80 const peaks: number[] = []81 for (let i = 0; i < bars; i++) {82 let max = 083 for (let j = i * step; j < Math.min(data.length, (i + 1) * step); j += 16) max = Math.max(max, Math.abs(data[j]))84 peaks.push(Math.round(max * 100) / 100)85 }86 const top = Math.max(0.05, ...peaks)87 return { peaks: peaks.map((p) => Math.round((p / top) * 100) / 100), duration: buf.duration }88 } finally {89 void ctx.close()90 }91}9293export class VoiceRecorder {94 private rec!: MediaRecorder95 private chunks: Blob[] = []96 private stream!: MediaStream97 startedAt = 098 async start() {99 this.stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } })100 const mime = ["audio/mp4", "audio/webm;codecs=opus", "audio/webm"].find((m) => MediaRecorder.isTypeSupported(m))101 this.rec = new MediaRecorder(this.stream, mime ? { mimeType: mime } : undefined)102 this.rec.ondataavailable = (e) => e.data.size && this.chunks.push(e.data)103 this.rec.start(250)104 this.startedAt = Date.now()105 }106 async stop(): Promise<File> {107 await new Promise<void>((res) => {108 this.rec.onstop = () => res()109 this.rec.stop()110 })111 this.stream.getTracks().forEach((t) => t.stop())112 const type = (this.rec.mimeType || "audio/webm").split(";")[0]113 return new File(this.chunks, `voice-${new Date().toISOString().slice(0, 19).replace(/[:T]/g, "-")}.${type.includes("mp4") ? "m4a" : "webm"}`, { type })114 }115 cancel() {116 try {117 this.rec.stop()118 } catch {}119 this.stream?.getTracks().forEach((t) => t.stop())120 }121}122123export function formatBytes(n: number) {124 if (n < 1024) return `${n} B`125 const u = ["KB", "MB", "GB", "TB"]126 let i = -1127 do {128 n /= 1024129 i++130 } while (n >= 1024 && i < u.length - 1)131 return `${n.toFixed(n < 10 ? 1 : 0)} ${u[i]}`132}133export function formatDuration(s: number) {134 s = Math.max(0, Math.round(s))135 return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`136}137