From 32fdbea79a73d85a22928334202c9dcd21cd0a9e Mon Sep 17 00:00:00 2001 From: Androtim <143963999+Androtim@users.noreply.github.com> Date: Fri, 3 Jul 2026 00:34:19 +0200 Subject: [PATCH] feat(client): sweep OPFS parts abandoned for over a week MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Partial transfers persist on disk so resume works across reloads, but parts from transfers that never finish stayed forever. On app start, delete part-files untouched for 7 days — anything actively written or recently paused has a fresh lastModified and survives. Co-Authored-By: Claude Fable 5 --- client/src/App.jsx | 4 ++++ client/src/lib/p2p/transfer.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/client/src/App.jsx b/client/src/App.jsx index fd24a58e..ce09af6f 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -14,8 +14,12 @@ import Messages from './pages/Messages' import Profile from './pages/Profile' import Leaderboard from './pages/Leaderboard' import Layout from './components/Layout' +import { sweepOrphanedTransfers } from './lib/p2p/transfer' import { Gem, AlertTriangle, RefreshCw } from 'lucide-react' +// Reclaim disk from transfers abandoned more than a week ago. +sweepOrphanedTransfers() + function ProtectedRoute({ children }) { const { user, loading } = useAuth() diff --git a/client/src/lib/p2p/transfer.js b/client/src/lib/p2p/transfer.js index 72d87808..9ed88670 100644 --- a/client/src/lib/p2p/transfer.js +++ b/client/src/lib/p2p/transfer.js @@ -117,6 +117,31 @@ export async function cleanupTransfer(transferId, fileCount = 32) { } } +const ORPHAN_AGE_MS = 7 * 24 * 60 * 60 * 1000 + +/** + * Delete part-files nothing has touched in a week. Resume depends on parts + * surviving reloads, so only age qualifies one as abandoned — anything being + * written has a fresh lastModified and is left alone. + */ +export async function sweepOrphanedTransfers(maxAgeMs = ORPHAN_AGE_MS) { + if (!opfsSupported()) return 0 + try { + const root = await navigator.storage.getDirectory() + const dir = await root.getDirectoryHandle('beacon-transfers') + const stale = [] + for await (const [name, handle] of dir.entries()) { + if (handle.kind !== 'file' || !name.endsWith('.part')) continue + const file = await handle.getFile() + if (Date.now() - file.lastModified > maxAgeMs) stale.push(name) + } + for (const name of stale) await dir.removeEntry(name).catch(() => {}) + return stale.length + } catch { + return 0 // directory doesn't exist yet, or OPFS is unavailable + } +} + // ---------- Sender ---------- /**