fix: open text, Markdown and PDF previews in the Files demo instead of failing

This commit is contained in:
Linus Rath
2026-10-28 02:32:29 +01:00
parent 5a898dcfd7
commit 066d2e292d
2 changed files with 19 additions and 6 deletions
+17
View File
@@ -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');
});
});
+2 -6
View File
@@ -1227,12 +1227,8 @@ export const useFileStore = create<FileState>((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' };
},