feat(download): add estimation of egress bytes by fetching Content-Length via HEAD request

This commit is contained in:
Yusuf İpek
2026-04-14 13:56:27 +03:00
parent f3cdebaa81
commit 50d31ef894
+14 -1
View File
@@ -327,6 +327,19 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
return withCacheControl(response, 'private, no-store'); return withCacheControl(response, 'private, no-store');
} }
// Fetch Content-Length via HEAD so we can record egress bytes without proxying the body.
let estimatedBytes = BigInt(0);
try {
const headRes = await fetchWithTimeout(source.url, { method: 'HEAD', cache: 'no-store' });
const cl = headRes.headers.get('content-length');
if (cl && /^\d+$/.test(cl.trim())) {
const parsed = BigInt(cl.trim());
if (parsed > BigInt(0)) estimatedBytes = parsed;
}
} catch {
// Best-effort — leave estimatedBytes as 0 if HEAD fails.
}
try { try {
await db.downloadEgressEvent.create({ await db.downloadEgressEvent.create({
data: { data: {
@@ -338,7 +351,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
downloaderUserId: session?.user?.id ?? null, downloaderUserId: session?.user?.id ?? null,
source: source.sourceType === 'original' ? DownloadEgressSource.ORIGINAL : DownloadEgressSource.COMPRESSED, source: source.sourceType === 'original' ? DownloadEgressSource.ORIGINAL : DownloadEgressSource.COMPRESSED,
quality: source.quality, quality: source.quality,
estimatedBytes: BigInt(0), estimatedBytes,
}, },
}); });
} catch (egressError) { } catch (egressError) {