P0(宿主): - 备份 ZIP 新增 plugin-files/<插件id>/<文件名> 通道,恢复时写回沙箱; 此前只备份 JSON ⇒ 换机后「提示词都在、封面全是灰块」,而状态行写着「已恢复」 - IWBStorageFile.pluginFiles 增 readBytes / listAll(不 mkdir、只读字节) - IWBPluginData 增 listFiles / readFile / restoreFiles:清单一次给出、字节逐条读写 (插件文件单插件上限 64MB,攒数组会 OOM) - 校验和口径统一:images / imagesData / pluginFiles / pluginFilesData 全部排除; JSON 兜底导出按自身内容重算 checksum,避免「换个打包形态就报校验不一致」 - 备份二次确认的体积/张数把插件文件计入;恢复摘要显示「含 N 个插件文件(封面等)」 - tests/plugin-data-test:新增插件文件清单/读字节/写回 三条行为断言 P1(插件 promptlib 1.11.0,随包同步): - 封面保存/上传即落成文件(JSON 只留文件名)、孤儿封面可回收 (启动后一次 + 删词条后一次 + 设置页手动入口;空集合/非插件数据来源一律不删) - 四处内联 djb2 哈希合并为 coverFileName,同图不再存两份 - 样式散值归零(4px→6px、margin 13px→12px),插件尺度棘轮基线保持 0
40 lines
2.3 KiB
JavaScript
40 lines
2.3 KiB
JavaScript
;(function (global) {
|
|
'use strict'
|
|
const result = global.IWBStorageResult
|
|
const create = (options) => {
|
|
const config = options || {}
|
|
const migrations = config.migrations || global.IWBStorageMigrations
|
|
const checksum = config.checksum
|
|
const payload = (data) => {
|
|
const copy = JSON.parse(JSON.stringify(data || {}))
|
|
// 与 app.js 的 backupChecksumPayload 同口径:字节载荷(图片 / 插件文件)不进校验和
|
|
delete copy.checksum; delete copy.imagesData; delete copy.images; delete copy.pluginFiles; delete copy.pluginFilesData
|
|
return copy
|
|
}
|
|
return {
|
|
async validate(data) {
|
|
if (!data || typeof data !== 'object' || Array.isArray(data) || data.kind !== 'backup' || !data.items || typeof data.items !== 'object' || Array.isArray(data.items)) return result.fail(result.error('validation', 'invalid-backup', '不是有效的备份文件', false))
|
|
const migrate = migrations && migrations.migrateBackup ? migrations.migrateBackup(data) : { ok: true, value: data }
|
|
if (!migrate.ok) return result.fail(result.error(migrate.code === 'unsupported-schema' ? 'unsupported' : 'validation', migrate.code || 'invalid-backup', migrate.error || '备份数据格式无效', false))
|
|
if (data.checksum && typeof checksum === 'function') {
|
|
const actual = await checksum(JSON.stringify(payload(data)))
|
|
if (actual !== data.checksum) return result.fail(result.error('validation', 'checksum-mismatch', '备份校验和不匹配', false))
|
|
}
|
|
return result.ok(migrate.value)
|
|
},
|
|
async parse(input) {
|
|
try {
|
|
const data = typeof input === 'string' ? JSON.parse(input) : input
|
|
return this.validate(data)
|
|
} catch (e) { return result.fail(result.error('validation', 'invalid-backup-json', '备份文件解析失败', false)) }
|
|
},
|
|
async export(items, options) {
|
|
const core = { app: 'image-workbench', kind: 'backup', backupVersion: 3, schemaVersion: config.schemaVersion || 2, at: Date.now(), items: items || {} }
|
|
if (typeof checksum === 'function') core.checksum = await checksum(JSON.stringify(payload(core)))
|
|
return result.ok(core)
|
|
},
|
|
}
|
|
}
|
|
global.IWBStorageBackup = { create }
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|