import { isTauri } from '../platform'; export interface SaveResult { /** Where the file went. A real filesystem path on desktop; an Android SAF * content:// URI on Android; null for a browser download. */ target: string | null; /** The saved filename, always human-readable (unlike an Android content URI). */ name: string; /** Can the OS reveal it in a file manager? Only real desktop paths can. */ revealable: boolean; /** False when the user dismissed the native save dialog. */ saved: boolean; } // A real filesystem path (desktop) vs an Android Storage Access Framework URI. // SAF returns an opaque content:// URI with no folder and no revealable path, // so the two need different follow-up UX. function isContentUri(target: string): boolean { return target.startsWith('content://') || !target.startsWith('/'); } // Saves the workspace pack. In a shell, a native "Save as" dialog picks the // location and the bytes are written there. On desktop that yields a real path // the caller can reveal in the file manager; on Android it yields a SAF // content:// URI (writable, but no folder to reveal and not human-readable), so // the caller shows the filename instead. In the browser it falls back to a // download. export async function savePack(pack: Blob, suggestedName: string): Promise { if (isTauri()) { const { save } = await import('@tauri-apps/plugin-dialog'); const target = await save({ defaultPath: suggestedName, filters: [{ name: 'Synapse workspace', extensions: ['synapse', 'zip'] }], }); if (!target) return { target: null, name: suggestedName, revealable: false, saved: false }; const { writeFile } = await import('@tauri-apps/plugin-fs'); await writeFile(target, new Uint8Array(await pack.arrayBuffer())); const revealable = !isContentUri(target); return { target, // A content URI is not a path; fall back to the name we suggested (which // the picker used as the default filename the user saw). name: revealable ? (target.split('/').pop() ?? suggestedName) : suggestedName, revealable, saved: true, }; } const url = URL.createObjectURL(pack); const a = document.createElement('a'); a.href = url; a.download = suggestedName; a.click(); URL.revokeObjectURL(url); return { target: null, name: suggestedName, revealable: false, saved: true }; } // Saves an arbitrary file the same way savePack does (native "Save as" in the // shell, a download in the browser). filter names the file kind in the desktop // dialog; ext is the extension without a dot. export async function saveFile( blob: Blob, suggestedName: string, filter: { name: string; ext: string }, ): Promise { if (isTauri()) { const { save } = await import('@tauri-apps/plugin-dialog'); const target = await save({ defaultPath: suggestedName, filters: [{ name: filter.name, extensions: [filter.ext] }], }); if (!target) return { target: null, name: suggestedName, revealable: false, saved: false }; const { writeFile } = await import('@tauri-apps/plugin-fs'); await writeFile(target, new Uint8Array(await blob.arrayBuffer())); const revealable = !isContentUri(target); return { target, name: revealable ? (target.split('/').pop() ?? suggestedName) : suggestedName, revealable, saved: true, }; } const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = suggestedName; a.click(); URL.revokeObjectURL(url); return { target: null, name: suggestedName, revealable: false, saved: true }; } // Show a saved file in the OS file manager (Finder, Explorer). Desktop only - // revealItemInDir is unsupported on Android/iOS, so the caller gates on // SaveResult.revealable before offering this. export async function revealPack(path: string): Promise { if (!isTauri()) return; const { revealItemInDir } = await import('@tauri-apps/plugin-opener'); await revealItemInDir(path); }