Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
25 changes: 25 additions & 0 deletions client/src/lib/p2p/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------

/**
Expand Down
Loading