Restore an evicted server copy from an online holder

This commit is contained in:
Claude
2026-09-30 07:58:42 +00:00
parent 6d3cefa874
commit 60a28fc593
8 changed files with 112 additions and 7 deletions

View File

@@ -53,7 +53,7 @@ import { P2P } from './p2p-config.js';
import * as p2pDb from './p2p-db.js';
import { registerP2pRoutes } from './p2p-routes.js';
import { registerIntakeRoutes } from './p2p-intake.js';
import { createP2pHub, holdersPayload } from './p2p-hub.js';
import { createP2pHub, holdersPayload, createRehydrator } from './p2p-hub.js';
import { sha256Range } from './hash.js';
import * as innertube from './innertube.js';
import QRCode from 'qrcode';
@@ -711,6 +711,16 @@ app.get('/api/streams', async (c) => {
},
});
} catch (err) {
// The source failed. If a device holds a verified copy, ask it to send one
// to the server so the video comes back (P2P flow 8).
let restoring = false;
try { restoring = await p2pRehydrate(videoId); } catch { /* best effort */ }
if (restoring) {
return c.json({
ok: false, restoring: true,
error: 'This video is unavailable at the source — a device that has it is sending a copy to the server. Try again in a minute.',
}, 503);
}
return c.json({ ok: false, error: err.message }, 500);
}
});
@@ -1989,6 +1999,14 @@ const intake = registerIntakeRoutes(app, {
validateMedia: (path, expected, opts) => validateMedia(path, expected,
{ ...opts, ffmpeg: FFMPEG, ffprobe: process.env.FFPROBE_PATH || 'ffprobe' }),
adopt: (videoId, path, info) => media.adoptFile(videoId, path, info),
serverHasCid: async (cid) => !!(await fileForCid(cid)),
});
// Source gone + server copy evicted → ask one online holder to send it back
// through intake (docs/p2p-architecture.md flow 8).
const p2pRehydrate = createRehydrator({
p2pDb, hub: p2pHub, enabled: () => P2P.enabled,
hasServerCopy: async (id) => !!(await media.getReady(id).catch(() => null)),
});
// ============================================================================