mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-11-03 00:13:09 +00:00
fix: open text, Markdown and PDF previews in the Files demo instead of failing
This commit is contained in:
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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' };
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user