Files
IPFS-portal/src/lib/download.ts
T
maikrolf 4a5b146713 fix: share-links missen het /ipfs/-segment
- gatewayLink() bouwde <gateway>/<cid>; dat pad bestaat niet in deze app
- Nu <gateway>/ipfs/<cid>, wat overeenkomt met de route GET /ipfs/<cid>
- Gateway-basis komt uit gatewayUrl() in lib/config.ts (NEXT_PUBLIC_GATEWAY_URL)
- Optionele filename -> ?filename=, uitgeconsumed door dezelfde route
- downloadViaGateway() hergebruikt de helper i.p.v. eigen gateway-constant
  (valideerde fallback-host was de oude maos.dedyn.io)
- helpers.test.ts legde het foute pad vast; asserties vervangen door de
  werkelijke vorm + een test op de config-override
2026-10-01 19:13:43 +02:00

47 lines
1.5 KiB
TypeScript

/* ════════════════════════════ 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 <a download>.
*/
export async function downloadFile(cid: string, filename?: string): Promise<void> {
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');
}