import { t } from "@lingui/core/macro"; import { artifactUrl } from "./artifact-url"; export type ArtifactTarget = { botId: string } | { groupId: string }; export async function fetchArtifactBytes(artifactId: string): Promise { const response = await fetch(artifactUrl(artifactId), { credentials: "include" }); if (!response.ok) throw new Error(t`Could not load this file.`); return new Uint8Array(await response.arrayBuffer()); } export function downloadArtifactBytes(name: string, mimeType: string, bytes: Uint8Array): void { const blob = new Blob([new Uint8Array(bytes)], { type: mimeType }); const url = URL.createObjectURL(blob); saveUrl(url, name); window.setTimeout(() => URL.revokeObjectURL(url), 60_000); } /** Lets the browser stream the download from the artifact route instead of buffering it. */ export function downloadArtifact(artifactId: string, name: string): void { saveUrl(artifactUrl(artifactId, { download: true }), name); } function saveUrl(url: string, name: string) { const anchor = document.createElement("a"); anchor.href = url; anchor.download = name; anchor.click(); }