diff --git a/tauri/src/platform/filesystem.ts b/tauri/src/platform/filesystem.ts index 2292a99a..ff0eaf4d 100644 --- a/tauri/src/platform/filesystem.ts +++ b/tauri/src/platform/filesystem.ts @@ -2,40 +2,25 @@ import type { PlatformFilesystem, FileFilter } from '@/platform/types'; export const tauriFilesystem: PlatformFilesystem = { async saveFile(filename: string, blob: Blob, filters?: FileFilter[]) { - try { - const { save } = await import('@tauri-apps/plugin-dialog'); - const filePath = await save({ - defaultPath: filename, - filters: filters || [], - }); + const { save } = await import('@tauri-apps/plugin-dialog'); + const { writeFile } = await import('@tauri-apps/plugin-fs'); - if (filePath) { - let resolvedPath = ''; - if (typeof filePath === 'string') { - resolvedPath = filePath; - } else if (filePath && typeof filePath === 'object' && 'path' in filePath) { - resolvedPath = (filePath as { path: string }).path; - } + const filePath = await save({ + defaultPath: filename, + filters: filters || [], + }); - if (!resolvedPath) { - throw new Error('Failed to resolve save path'); - } + if (!filePath) return; // User cancelled the dialog - const { writeFile } = await import('@tauri-apps/plugin-fs'); - const arrayBuffer = await blob.arrayBuffer(); - await writeFile(resolvedPath, new Uint8Array(arrayBuffer)); - } - } catch (error) { - console.error('Failed to use Tauri dialog, falling back to browser download:', error); - // Fall back to browser download if Tauri dialog fails - const url = window.URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = filename; - document.body.appendChild(a); - a.click(); - window.URL.revokeObjectURL(url); - document.body.removeChild(a); + const resolvedPath = typeof filePath === 'string' + ? filePath + : (filePath as { path: string }).path; + + if (!resolvedPath) { + throw new Error('Failed to resolve save path from dialog'); } + + const arrayBuffer = await blob.arrayBuffer(); + await writeFile(resolvedPath, new Uint8Array(arrayBuffer)); }, };