diff --git a/stores/__tests__/file-store.test.ts b/stores/__tests__/file-store.test.ts index 651eb4a67..ec499372f 100644 --- a/stores/__tests__/file-store.test.ts +++ b/stores/__tests__/file-store.test.ts @@ -1019,3 +1019,20 @@ describe('file-store actions by id', () => { expect(useFileStore.getState().recentFiles.map(r => r.id)).toEqual(['2']); }); }); + +describe('file-store preview content', () => { + it('reads a file through the client, so the demo, which has no download URL, can preview it', async () => { + const client = makeMockClient([file('a', 'notes.txt', null)]); + Object.assign(client, { + getBlobDownloadUrl: () => 'data:application/octet-stream;demo-blob=b-a', + getAuthHeader: () => '', + fetchBlob: async (blobId: string) => new Blob([`content of ${blobId}`], { type: 'text/plain' }), + }); + useFileStore.getState().initClient(client); + await useFileStore.getState().navigate(null); + + const { blob, contentType } = await useFileStore.getState().getFileContent('notes.txt'); + expect(await blob.text()).toBe('content of b-a'); + expect(contentType).toBe('text/plain'); + }); +}); diff --git a/stores/file-store.ts b/stores/file-store.ts index 712bea0a1..bf4b1d832 100644 --- a/stores/file-store.ts +++ b/stores/file-store.ts @@ -1227,12 +1227,8 @@ export const useFileStore = create((set, get) => ({ const resource = resources.find(r => r.name === name); if (!resource?.blobId) throw new Error('No blob'); - const url = client.getBlobDownloadUrl(resource.blobId, resource.name, resource.contentType); - const response = await fetch(url, { - headers: { 'Authorization': client.getAuthHeader() }, - }); - if (!response.ok) throw new Error(`Failed to fetch file: ${response.status}`); - const blob = await response.blob(); + // Through the client, like getImageUrl: the demo has no download URL to fetch. + const blob = await client.fetchBlob(resource.blobId, resource.name, resource.contentType); return { blob, contentType: resource.contentType || 'application/octet-stream' }; },