概述OPFS 支持来源私有的目录结构。本文提供遍历与批量维护方法以支持管理任务。遍历与批量操作const supportsOPFS = !!(navigator.storage && navigator.storage.getDirectory); async function walk(dir, base, onFile) { for await (const [name, handle] of dir.entries()) { const p = base ? base + '/' + name : name; if (handle.kind === 'file') await onFile(handle, p); else if (handle.kind === 'directory') { const sub = await dir.getDirectoryHandle(name); await walk(sub, p, onFile); } } } async function bulkDelete(prefix) { if (!supportsOPFS) return; const root = await navigator.storage.getDirectory(); await walk(root, '', async (_, path) => { if (path.startsWith(prefix)) { const seg = path.split('/'); let dir = root; for (let i = 0; i < seg.length - 1; i++) dir = await dir.getDirectoryHandle(seg[i]); await dir.removeEntry(seg[seg.length - 1]); } }); } async function moveFile(src, dst) { const root = await navigator.storage.getDirectory(); const s = src.split('/'); const d = dst.split('/'); let sd = root; for (let i = 0; i < s.length - 1; i++) sd = await sd.getDirectoryHandle(s[i]); const fh = await sd.getFileHandle(s[s.length - 1]); let dd = root; for (let i = 0; i < d.length - 1; i++) dd = await dd.getDirectoryHandle(d[i], { create: true }); const dfh = await dd.getFileHandle(d[d.length - 1], { create: true }); const w = await dfh.createWritable(); const f = await fh.getFile(); await w.write(await f.arrayBuffer()); await w.close(); await sd.removeEntry(s[s.length - 1]); }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部