diff --git a/src/lib/__tests__/helpers.test.ts b/src/lib/__tests__/helpers.test.ts index 55792bc..bac48cf 100644 --- a/src/lib/__tests__/helpers.test.ts +++ b/src/lib/__tests__/helpers.test.ts @@ -1,6 +1,13 @@ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, afterEach } from 'vitest'; import { formatBytes, gatewayLink, gatewayUrl, truncateCid } from '../helpers'; +const ORIGINAL_GATEWAY = process.env.NEXT_PUBLIC_GATEWAY_URL; + +afterEach(() => { + if (ORIGINAL_GATEWAY === undefined) delete process.env.NEXT_PUBLIC_GATEWAY_URL; + else process.env.NEXT_PUBLIC_GATEWAY_URL = ORIGINAL_GATEWAY; +}); + describe('formatBytes', () => { it('formats 0 as "0 B"', () => { expect(formatBytes(0)).toBe('0 B'); @@ -68,18 +75,43 @@ describe('formatBytes', () => { }); describe('gatewayLink', () => { - it('returns correct gateway URL for valid CID', () => { + it('zet het CID achter het /ipfs/-segment van de gateway', () => { const cid = 'QmTest123'; - expect(gatewayLink(cid)).toBe('https://ipfs.maos.dedyn.io/QmTest123'); + expect(gatewayLink(cid)).toBe('https://ipfs.maos.dedyn.io/ipfs/QmTest123'); }); it('handles bafy CIDs', () => { - const cid = 'bafybeigdyrzt5xxyjaz6k3w3h3y7a7q7q7q7q7q7q7q7q7q7q7q7q7q'; - expect(gatewayLink(cid)).toBe('https://ipfs.maos.dedyn.io/bafybeigdyrzt5xxyjaz6k3w3h3y7a7q7q7q7q7q7q7q7q7q7q7q7q7q'); + const cid = 'bafybeigdyrzt5xxyjaz6k3w3h3y7a7q7q7q7q7q7q7q7q7q7q7q7q'; + expect(gatewayLink(cid)).toBe('https://ipfs.maos.dedyn.io/ipfs/bafybeigdyrzt5xxyjaz6k3w3h3y7a7q7q7q7q7q7q7q7q7q7q7q7q'); }); it('handles empty string', () => { - expect(gatewayLink('')).toBe('https://ipfs.maos.dedyn.io/'); + expect(gatewayLink('')).toBe('https://ipfs.maos.dedyn.io/ipfs/'); + }); + + it('volgt de geconfigureerde gateway uit config.ts', () => { + process.env.NEXT_PUBLIC_GATEWAY_URL = 'https://gateway.example.org'; + expect(gatewayLink('QmTest123')).toBe('https://gateway.example.org/ipfs/QmTest123'); + }); + + it('verwijdert een trailing slash van de geconfigureerde gateway', () => { + process.env.NEXT_PUBLIC_GATEWAY_URL = 'https://gateway.example.org/'; + expect(gatewayLink('QmTest123')).toBe('https://gateway.example.org/ipfs/QmTest123'); + }); + + it('laat de gateway nooit dubbel sluiten als de CID leeg is', () => { + process.env.NEXT_PUBLIC_GATEWAY_URL = 'https://gateway.example.org/'; + expect(gatewayLink('')).toBe('https://gateway.example.org/ipfs/'); + }); + + it('voegt een ?filename= suffix toe voor de gateway-route', () => { + expect(gatewayLink('QmTest123', 'rap portaal.txt')).toBe( + 'https://ipfs.maos.dedyn.io/ipfs/QmTest123?filename=rap%20portaal.txt', + ); + }); + + it('laat de query-string weg als er geen filename is', () => { + expect(gatewayLink('QmTest123')).not.toContain('?'); }); }); diff --git a/src/lib/download.ts b/src/lib/download.ts index 9004ce9..8328c78 100644 --- a/src/lib/download.ts +++ b/src/lib/download.ts @@ -2,6 +2,8 @@ * 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 . @@ -34,9 +36,8 @@ export async function downloadFile(cid: string, filename?: string): Promise` (`src/app/ipfs/[cid]/route.ts`), niet op `/`. + * Een optionele `filename` komt als `?filename=` terecht, wat diezelfde route + * in `Content-Disposition` verwerkt. + */ +export function gatewayLink(cid: string, filename?: string): string { + const suffix = filename ? `?filename=${encodeURIComponent(filename)}` : ''; + return `${gatewayBaseUrl()}/ipfs/${cid}${suffix}`; } /** Alias for gatewayLink — used in FilePreview */