Files
cnc_wams/wasm-port/runtime/opfs/file-service.js
wangdequan 84bdc819b4 按建议,继续完成后续工作
结论:已新增 OPFS host-side 路径模型,覆盖 INI、tool table、parameter、G-code、preview cache 与 session snapshot 存储目标,并让 file-service 和浏览器 smoke 复用该模型;聚合 smoke 验证通过。
2026-06-08 03:26:39 +08:00

40 lines
1.3 KiB
JavaScript

import { splitOpfsPath } from "./path-model.js";
export async function getOpfsRoot(storage = globalThis.navigator?.storage) {
if (!storage?.getDirectory) {
throw new Error("OPFS is not available in this browser.");
}
return storage.getDirectory();
}
export async function ensureParentDir(root, path) {
const parts = splitOpfsPath(path);
let current = root;
for (const part of parts.slice(0, -1)) {
current = await current.getDirectoryHandle(part, { create: true });
}
return current;
}
export async function saveTextFile(path, text, storage) {
const root = await getOpfsRoot(storage);
const dir = await ensureParentDir(root, path);
const filename = splitOpfsPath(path).at(-1);
const fileHandle = await dir.getFileHandle(filename, { create: true });
const writable = await fileHandle.createWritable();
await writable.write(text);
await writable.close();
}
export async function loadTextFile(path, storage) {
const root = await getOpfsRoot(storage);
const parts = splitOpfsPath(path);
let current = root;
for (const part of parts.slice(0, -1)) {
current = await current.getDirectoryHandle(part);
}
const fileHandle = await current.getFileHandle(parts.at(-1));
const file = await fileHandle.getFile();
return file.text();
}