feat(backup): add verified offline migration
This commit is contained in:
1 parent
cc9be25d2d
commit
ede7f9713c
11 files changed
+174
-21
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "life-os",
|
||||
"version": "1.19.0",
|
||||
"version": "1.20.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
const DRAFT_KEY = 'lifeos_today_draft_v1';
|
||||
const localDay = () => { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`; };
|
||||
const escapeHtml = value => String(value || '').replace(/[&<>]/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[char]));
|
||||
const APP_VERSION = '1.19.0';
|
||||
const APP_VERSION = '1.20.0';
|
||||
window.LifeOSVersion = APP_VERSION;
|
||||
const appIds = ['today', 'timeline', 'museum', 'insights', 'profile', 'tasks', 'calendar', 'system', 'browser', 'terminal', 'database'];
|
||||
const t = (key, values) => LifeI18n.t(key, values);
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/* Portable Life OS backup format. Schema v3 carries every device-local app record. */
|
||||
(() => {
|
||||
const BACKUP_SCHEMA = 3;
|
||||
const SAFETY_KEY = 'lifeos_restore_safety_v1';
|
||||
const excluded = /^(lifeos_(?:cloud_|precloud_|sync_conflict_|storage_mode_|local_cache_revision_|restore_safety_))/;
|
||||
const portableKey = key => typeof key === 'string' && key.startsWith('lifeos_') && !excluded.test(key);
|
||||
const records = () => Object.fromEntries(
|
||||
Array.from({ length: localStorage.length }, (_, index) => localStorage.key(index))
|
||||
.filter(portableKey)
|
||||
.sort()
|
||||
.map(key => [key, localStorage.getItem(key)])
|
||||
);
|
||||
const canonical = value => JSON.stringify(Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b))));
|
||||
const sha256 = async value => {
|
||||
const bytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
|
||||
return Array.from(new Uint8Array(bytes), byte => byte.toString(16).padStart(2, '0')).join('');
|
||||
};
|
||||
const json = (key, fallback) => { try { return JSON.parse(localStorage.getItem(key) || 'null') ?? fallback; } catch { return fallback; } };
|
||||
const safeDay = () => new Date().toISOString().slice(0, 10);
|
||||
|
||||
async function createBackup() {
|
||||
const stored = records();
|
||||
return {
|
||||
app: 'Life OS',
|
||||
version: window.LifeOSVersion || '1.20.0',
|
||||
schemaVersion: BACKUP_SCHEMA,
|
||||
exportedAt: new Date().toISOString(),
|
||||
profile: json('lifeos_profile', {}),
|
||||
memories: json('lifeos_memories', []),
|
||||
museum: json('lifeos_museum', []),
|
||||
settings: json('lifeos_settings', {}),
|
||||
system: json('lifeos_system', { schemaVersion: 2 }),
|
||||
desktopLayout: json('lifeos_window_layout_v1', null),
|
||||
widgetLayout: json('lifeos_widget_layout_v1', null),
|
||||
records: stored,
|
||||
recordCount: Object.keys(stored).length,
|
||||
checksum: { algorithm: 'SHA-256', value: await sha256(canonical(stored)) }
|
||||
};
|
||||
}
|
||||
|
||||
function download(data, filename) {
|
||||
const url = URL.createObjectURL(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }));
|
||||
const link = document.createElement('a');
|
||||
link.href = url; link.download = filename; link.click();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
|
||||
async function valid(data) {
|
||||
if (!data || data.app !== 'Life OS' || ![1, 2, 3].includes(data.schemaVersion) || !Array.isArray(data.memories) || !Array.isArray(data.museum)) return false;
|
||||
if (data.schemaVersion < BACKUP_SCHEMA) return true;
|
||||
if (!data.records || Array.isArray(data.records) || typeof data.records !== 'object') return false;
|
||||
const entries = Object.entries(data.records);
|
||||
if (entries.some(([key, value]) => !portableKey(key) || typeof value !== 'string')) return false;
|
||||
if (data.recordCount !== entries.length || data.checksum?.algorithm !== 'SHA-256' || !/^[a-f0-9]{64}$/.test(data.checksum?.value || '')) return false;
|
||||
return (await sha256(canonical(data.records))) === data.checksum.value;
|
||||
}
|
||||
|
||||
window.exportData = async () => {
|
||||
const state = settings(); state.lastBackup = new Date().toISOString(); save('lifeos_settings', state);
|
||||
download(await createBackup(), `life-os-backup-${safeDay()}.json`);
|
||||
window.renderAll?.(); window.toast?.('feedback.backup');
|
||||
};
|
||||
|
||||
window.readImport = async input => {
|
||||
const file = input.files?.[0];
|
||||
input.value = '';
|
||||
if (!file || file.size > 32 * 1024 * 1024) return window.toast?.('feedback.invalid');
|
||||
try {
|
||||
const data = JSON.parse(await file.text());
|
||||
if (!(await valid(data))) throw new Error('invalid backup');
|
||||
importData = data;
|
||||
window.importPreview();
|
||||
} catch {
|
||||
window.toast?.('feedback.invalid');
|
||||
}
|
||||
};
|
||||
|
||||
const previousApplyImport = window.applyImport;
|
||||
window.applyImport = async mode => {
|
||||
const data = importData;
|
||||
if (mode !== 'replace' || data?.schemaVersion !== BACKUP_SCHEMA) return previousApplyImport(mode);
|
||||
const safety = await createBackup();
|
||||
localStorage.setItem(SAFETY_KEY, JSON.stringify(safety));
|
||||
download(safety, `life-os-before-restore-${safeDay()}.json`);
|
||||
for (const key of Array.from({ length: localStorage.length }, (_, index) => localStorage.key(index)).filter(portableKey)) localStorage.removeItem(key);
|
||||
for (const [key, value] of Object.entries(data.records)) localStorage.setItem(key, value);
|
||||
document.querySelector('#modal').innerHTML = '';
|
||||
window.toast?.('feedback.restoring');
|
||||
setTimeout(() => {
|
||||
window.renderIcons?.(); window.renderAll?.(); window.applyLanguage?.(); window.applyDesktopPreferences?.();
|
||||
window.LifeOSLocalDB?.refresh?.(); window.toast?.('feedback.restored');
|
||||
}, 500);
|
||||
};
|
||||
|
||||
window.LifeOSBackup = { schemaVersion: BACKUP_SCHEMA, create: createBackup, validate: valid, portableKey };
|
||||
})();
|
||||
Loaded 3 of 11 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user