Add the on-device IndexedDB file registry and hash saves while downloading
This commit is contained in:
@@ -189,7 +189,7 @@ async function opfsDownload(videoId, { mux = false } = {}) {
|
||||
let workerError = null;
|
||||
if (typeof window.OPFS.downloadVideo === 'function' && typeof Worker !== 'undefined') {
|
||||
const w = await window.OPFS.downloadVideo(videoId, url);
|
||||
if (w.ok) return { ok: true, cached: true };
|
||||
if (w.ok) return { ok: true, cached: true, sha256: w.sha256 || null, expectedSha: w.expectedSha || null, size: w.size || 0 };
|
||||
workerError = w.error || null;
|
||||
}
|
||||
|
||||
@@ -270,12 +270,14 @@ async function opfsList() {
|
||||
async function opfsDelete(videoId) {
|
||||
if (!window.OPFS || !window.OPFS.isSupported()) return { ok: true };
|
||||
try { await window.OPFS.deleteVideo(videoId); } catch { /* ignore */ }
|
||||
if (window.DeviceDB) { await window.DeviceDB.deleteFile(videoId); if (window.P2PClient) window.P2PClient.changed(); }
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
async function opfsClear() {
|
||||
if (!window.OPFS || !window.OPFS.isSupported()) return { ok: true };
|
||||
try { await window.OPFS.clearAll(); } catch { /* ignore */ }
|
||||
if (window.DeviceDB) { await window.DeviceDB.clear(); if (window.P2PClient) window.P2PClient.changed(); }
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
@@ -1259,6 +1261,18 @@ async function warmOfflineThumbs() {
|
||||
}
|
||||
}
|
||||
|
||||
// This device's record of what it holds and each file's content id
|
||||
// (docs/p2p-architecture.md flow 2). The P2P client reports these.
|
||||
function recordDeviceFile(id, res) {
|
||||
if (!WEB || !window.DeviceDB) return;
|
||||
const cid = res && window.Sha256 && window.Sha256.isHex(res.sha256) ? res.sha256 : null;
|
||||
const state = cid ? (res.expectedSha === cid ? 'verified' : 'unverified') : 'unhashed';
|
||||
const now = Date.now();
|
||||
window.DeviceDB.putFile({ videoId: id, cid, size: (res && res.size) || 0, savedAt: now, lastCheckedAt: now, state })
|
||||
.then(() => { if (window.P2PClient) window.P2PClient.changed(); })
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
// Download a video into the permanent offline cache. Safe to call repeatedly.
|
||||
async function preload(video, { quiet = false, mux = false } = {}) {
|
||||
const id = video.id;
|
||||
@@ -1282,6 +1296,7 @@ async function preload(video, { quiet = false, mux = false } = {}) {
|
||||
if (res && res.ok && res.cached) {
|
||||
cachedIds.add(id);
|
||||
cacheMutations++;
|
||||
recordDeviceFile(id, res);
|
||||
warmThumb(thumbUrlFor(id, video));
|
||||
if (!quiet) toast(`Saved “${video.title}” ✓`);
|
||||
} else if (!quiet) {
|
||||
|
||||
Reference in New Issue
Block a user