fix: harden file previews and downloads
This commit is contained in:
@@ -243,12 +243,21 @@ export const createWebFilesAPI = ({ urls }: WebFilesAPIOptions): FilesAPI => ({
|
||||
|
||||
async downloadFile(path: string): Promise<void> {
|
||||
const target = normalizePath(path);
|
||||
const url = urls.rawFile(target, { download: true });
|
||||
const response = await runtimeFetch('/api/fs/raw', {
|
||||
query: { path: target, download: true },
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Download failed (${response.status})`);
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = target.split('/').pop() || 'file';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
setTimeout(() => URL.revokeObjectURL(url), 100);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user