/* ════════════════════════════ Download ════════════════════════════ * Utility om IPFS content te downloaden als bestand. */ import { gatewayLink } from './helpers'; /** * Download een bestand van IPFS via de explorer/cat proxy. * Maakt een Blob van de response en simuleert een klik op . */ export async function downloadFile(cid: string, filename?: string): Promise { const res = await fetch(`/api/explorer/cat/${encodeURIComponent(cid)}`); if (!res.ok) throw new Error(`Download mislukt (${res.status})`); const blob = await res.blob(); const url = URL.createObjectURL(blob); const ext = filename?.includes('.') ? '' : '.bin'; const name = filename || `${cid.slice(0, 12)}${ext}`; const a = document.createElement('a'); a.href = url; a.download = name; a.rel = 'noopener noreferrer'; document.body.appendChild(a); a.click(); // Cleanup setTimeout(() => { document.body.removeChild(a); URL.revokeObjectURL(url); }, 100); } /** * Download via gateway (alternatief als proxy traag is). */ export function downloadViaGateway(cid: string, filename?: string) { const a = document.createElement('a'); a.href = gatewayLink(cid); a.download = filename || cid; a.target = '_blank'; a.rel = 'noopener noreferrer'; a.click(); console.warn('[download] Gateway download — no programmatic feedback'); }