import { downloadBytes, type AppServiceResult } from "./app-services.js"; import { ProjectSessionStore } from "./project-session.js"; const EXTENSIONS = new Set([".fcstd", ".step", ".stp", ".brep", ".brp", ".iges", ".igs", ".stl", ".obj", ".ply", ".svg", ".dxf", ".csv"]); export interface NativeByteResult { module: string; format: string; fileName: string; bytes: ArrayBuffer; } function extension(fileName: string): string { const index = fileName.lastIndexOf("."); return index >= 0 ? fileName.slice(index).toLowerCase() : ""; } export class ExportService { constructor(private readonly project?: ProjectSessionStore) {} download(native: Partial): AppServiceResult<{ fileName: string; bytes: number }> { const fileName = native.fileName ?? "FreeCAD-export"; const bytes = native.bytes; const ext = extension(fileName); if (!bytes || !(bytes instanceof ArrayBuffer)) return { schema: "freecad-web-app-service-result/v1", service: "file.download", status: "error", error: { code: "native_bytes_unavailable", message: "Native Worker response did not include export bytes" } }; if (!EXTENSIONS.has(ext)) return { schema: "freecad-web-app-service-result/v1", service: "file.download", status: "error", error: { code: "unsupported_extension", message: `Unsupported CAD export extension ${ext || "(none)"}` } }; const result = downloadBytes(fileName, bytes, native.format || "application/octet-stream"); if (result.status === "ok") this.project?.touchFile({ path: fileName, module: native.module ?? "", format: native.format ?? ext, updatedAt: new Date().toISOString() }); return result; } }