feat: harden Bunny upload flow, migrate Bunny playback to hls.js, and add Bunny storage admin stats

This commit is contained in:
Yusuf İpek
2026-02-22 13:03:45 +03:00
parent 10164069dd
commit e30b4a5b19
17 changed files with 1445 additions and 285 deletions
+177 -35
View File
@@ -1,28 +1,133 @@
import { unstable_cache } from 'next/cache';
import { db } from '@/lib/db';
import { r2Client, R2_BUCKET_NAME } from '@/lib/r2';
import { ListObjectsV2Command } from '@aws-sdk/client-s3';
import { ListObjectsV2Command, type ListObjectsV2CommandInput } from '@aws-sdk/client-s3';
const BUNNY_API_BASE = 'https://video.bunnycdn.com';
const STORAGE_CACHE_SECONDS = 600;
interface BunnyStorageStats {
totalBytes: number;
byVideoId: Record<string, number>;
}
function getBunnyConfig(): { apiKey: string; libraryId: string } {
const apiKey = process.env.BUNNY_STREAM_API_KEY;
const libraryId = process.env.BUNNY_STREAM_LIBRARY_ID || process.env.NEXT_PUBLIC_BUNNY_STREAM_LIBRARY_ID;
if (!apiKey || !libraryId) {
throw new Error('Missing Bunny Stream credentials.');
}
return { apiKey, libraryId };
}
function toRecord(value: unknown): Record<string, unknown> | null {
if (!value || typeof value !== 'object') return null;
return value as Record<string, unknown>;
}
function parseBunnyVideoStorageBytes(item: unknown): number {
const record = toRecord(item);
if (!record) return 0;
const candidates = ['storageSize', 'storage', 'size'];
for (const key of candidates) {
const value = record[key];
if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
return value;
}
}
return 0;
}
function parseBunnyVideoGuid(item: unknown): string | null {
const record = toRecord(item);
if (!record) return null;
const value = record.guid;
return typeof value === 'string' && value.length > 0 ? value : null;
}
async function listAllR2FileSizes(): Promise<Map<string, number>> {
const fileSizes = new Map<string, number>();
let isTruncated = true;
let continuationToken: string | undefined;
while (isTruncated) {
const commandParams: ListObjectsV2CommandInput = { Bucket: R2_BUCKET_NAME };
if (continuationToken) {
commandParams.ContinuationToken = continuationToken;
}
const data = await r2Client.send(new ListObjectsV2Command(commandParams));
if (data.Contents) {
for (const item of data.Contents) {
if (item.Key) fileSizes.set(item.Key, item.Size || 0);
}
}
isTruncated = data.IsTruncated ?? false;
continuationToken = data.NextContinuationToken;
}
return fileSizes;
}
async function fetchBunnyStorageStats(): Promise<BunnyStorageStats> {
const { apiKey, libraryId } = getBunnyConfig();
const byVideoId: Record<string, number> = {};
let totalBytes = 0;
let page = 1;
const itemsPerPage = 100;
while (page <= 200) {
const response = await fetch(
`${BUNNY_API_BASE}/library/${libraryId}/videos?page=${page}&itemsPerPage=${itemsPerPage}`,
{ headers: { AccessKey: apiKey }, cache: 'no-store' }
);
if (!response.ok) {
throw new Error(`Bunny API failed (${response.status})`);
}
const json = await response.json();
const record = toRecord(json);
if (!record) break;
const rawItems = Array.isArray(record.items)
? record.items
: (Array.isArray(record.Items) ? record.Items : []);
if (rawItems.length === 0) break;
for (const rawItem of rawItems) {
const guid = parseBunnyVideoGuid(rawItem);
if (!guid) continue;
const storageBytes = parseBunnyVideoStorageBytes(rawItem);
byVideoId[guid] = storageBytes;
totalBytes += storageBytes;
}
const totalItems = typeof record.totalItems === 'number'
? record.totalItems
: (typeof record.TotalItems === 'number' ? record.TotalItems : null);
if (totalItems !== null && page * itemsPerPage >= totalItems) {
break;
}
page += 1;
}
return { totalBytes, byVideoId };
}
// Cache for 10 minutes (600 seconds)
export const getCachedTotalStorage = unstable_cache(
async () => {
let totalStorageBytes = 0;
try {
let isTruncated = true;
let continuationToken: string | undefined = undefined;
while (isTruncated) {
const commandParams: any = { Bucket: R2_BUCKET_NAME };
if (continuationToken) commandParams.ContinuationToken = continuationToken;
const data = await r2Client.send(new ListObjectsV2Command(commandParams));
if (data.Contents) {
for (const item of data.Contents) {
totalStorageBytes += item.Size || 0;
}
}
isTruncated = data.IsTruncated ?? false;
continuationToken = data.NextContinuationToken;
const fileSizes = await listAllR2FileSizes();
for (const size of fileSizes.values()) {
totalStorageBytes += size;
}
} catch (err) {
console.error('Failed to fetch total storage stats:', err);
@@ -31,7 +136,60 @@ export const getCachedTotalStorage = unstable_cache(
return totalStorageBytes;
},
['admin-total-storage'],
{ revalidate: 600 }
{ revalidate: STORAGE_CACHE_SECONDS }
);
export const getCachedBunnyStorageStats = unstable_cache(
async () => {
try {
return await fetchBunnyStorageStats();
} catch (err) {
console.error('Failed to fetch Bunny storage stats:', err);
return { totalBytes: -1, byVideoId: {} } as BunnyStorageStats;
}
},
['admin-bunny-storage'],
{ revalidate: STORAGE_CACHE_SECONDS }
);
export const getCachedUserBunnyStorage = unstable_cache(
async () => {
const perUserStorage: Record<string, number> = {};
try {
const bunnyStats = await getCachedBunnyStorageStats();
if (bunnyStats.totalBytes < 0) return perUserStorage;
const bunnyVersions = await db.videoVersion.findMany({
where: { providerId: 'bunny' },
select: {
videoId: true,
video: {
select: {
project: {
select: { ownerId: true },
},
},
},
},
});
const seenVideoIds = new Set<string>();
for (const version of bunnyVersions) {
const ownerId = version.video.project.ownerId;
const dedupeKey = `${ownerId}:${version.videoId}`;
if (seenVideoIds.has(dedupeKey)) continue;
seenVideoIds.add(dedupeKey);
const size = bunnyStats.byVideoId[version.videoId] || 0;
perUserStorage[ownerId] = (perUserStorage[ownerId] || 0) + size;
}
} catch (err) {
console.error('Failed to calculate per-user Bunny storage:', err);
}
return perUserStorage;
},
['admin-user-bunny-storage'],
{ revalidate: STORAGE_CACHE_SECONDS }
);
export const getCachedUserMediaStorage = unstable_cache(
@@ -39,23 +197,7 @@ export const getCachedUserMediaStorage = unstable_cache(
// Return a plain object so it maps cleanly out of unstable_cache across requests
const userStorage: Record<string, { total: number, voice: number, image: number }> = {};
try {
const fileSizes = new Map<string, number>();
let isTruncated = true;
let continuationToken: string | undefined = undefined;
while (isTruncated) {
const commandParams: any = { Bucket: R2_BUCKET_NAME };
if (continuationToken) commandParams.ContinuationToken = continuationToken;
const data = await r2Client.send(new ListObjectsV2Command(commandParams));
if (data.Contents) {
for (const item of data.Contents) {
if (item.Key) fileSizes.set(item.Key, item.Size || 0);
}
}
isTruncated = data.IsTruncated ?? false;
continuationToken = data.NextContinuationToken;
}
const fileSizes = await listAllR2FileSizes();
const mediaComments = await db.comment.findMany({
where: { OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }], authorId: { not: null } },
@@ -93,5 +235,5 @@ export const getCachedUserMediaStorage = unstable_cache(
return userStorage;
},
['admin-user-media-storage'],
{ revalidate: 600 }
{ revalidate: STORAGE_CACHE_SECONDS }
);
+55
View File
@@ -0,0 +1,55 @@
interface BunnyVideoRef {
providerId: string;
videoId: string;
}
const BUNNY_API_BASE = 'https://video.bunnycdn.com';
const BUNNY_VIDEO_ID_PATTERN = /^[A-Za-z0-9_-]{8,128}$/;
function getBunnyConfig(): { apiKey: string; libraryId: string } {
const apiKey = process.env.BUNNY_STREAM_API_KEY;
const libraryId = process.env.BUNNY_STREAM_LIBRARY_ID || process.env.NEXT_PUBLIC_BUNNY_STREAM_LIBRARY_ID;
if (!apiKey || !libraryId) {
throw new Error('Bunny cleanup failed: missing BUNNY_STREAM_API_KEY or BUNNY_STREAM_LIBRARY_ID.');
}
return { apiKey, libraryId };
}
export async function cleanupBunnyStreamVideos(videoRefs: BunnyVideoRef[]): Promise<void> {
const normalizeVideoId = (value: string): string | null => {
const trimmed = value.trim();
return BUNNY_VIDEO_ID_PATTERN.test(trimmed) ? trimmed : null;
};
const bunnyVideoIds = [...new Set(
videoRefs
.filter((ref) => ref.providerId === 'bunny' && Boolean(ref.videoId))
.map((ref) => normalizeVideoId(ref.videoId))
.filter((videoId): videoId is string => Boolean(videoId))
)];
if (bunnyVideoIds.length === 0) return;
const { apiKey, libraryId } = getBunnyConfig();
for (const bunnyVideoId of bunnyVideoIds) {
const response = await fetch(`${BUNNY_API_BASE}/library/${libraryId}/videos/${encodeURIComponent(bunnyVideoId)}`, {
method: 'DELETE',
headers: {
AccessKey: apiKey,
},
});
// Treat not-found as already deleted.
if (response.status === 404) continue;
if (!response.ok) {
const body = await response.text().catch(() => '');
throw new Error(
`Bunny cleanup failed for video ${bunnyVideoId}: ${response.status} ${body.slice(0, 300)}`
);
}
}
}
+95
View File
@@ -0,0 +1,95 @@
import crypto from 'crypto';
const BUNNY_UPLOAD_TOKEN_TYPE = 'bunny-upload';
const DEFAULT_TOKEN_TTL_SECONDS = 60 * 60;
interface BunnyUploadTokenPayload {
typ: typeof BUNNY_UPLOAD_TOKEN_TYPE;
uid: string;
pid: string;
vid: string;
iat: number;
exp: number;
}
interface BunnyUploadTokenSubject {
userId: string;
projectId: string;
videoId: string;
}
function getBunnyUploadTokenSecret(): string {
const secret = process.env.BUNNY_UPLOAD_TOKEN_SECRET || process.env.NEXTAUTH_SECRET;
if (!secret) {
throw new Error('Missing BUNNY_UPLOAD_TOKEN_SECRET or NEXTAUTH_SECRET.');
}
return secret;
}
function signPayload(payload: string, secret: string): string {
return crypto.createHmac('sha256', secret).update(payload).digest('base64url');
}
function isValidPayload(value: unknown): value is BunnyUploadTokenPayload {
if (!value || typeof value !== 'object') return false;
const payload = value as Partial<BunnyUploadTokenPayload>;
return payload.typ === BUNNY_UPLOAD_TOKEN_TYPE
&& typeof payload.uid === 'string'
&& typeof payload.pid === 'string'
&& typeof payload.vid === 'string'
&& typeof payload.iat === 'number'
&& Number.isFinite(payload.iat)
&& typeof payload.exp === 'number'
&& Number.isFinite(payload.exp);
}
export function createBunnyUploadToken(
subject: BunnyUploadTokenSubject,
ttlSeconds = DEFAULT_TOKEN_TTL_SECONDS
): string {
const now = Math.floor(Date.now() / 1000);
const payload: BunnyUploadTokenPayload = {
typ: BUNNY_UPLOAD_TOKEN_TYPE,
uid: subject.userId,
pid: subject.projectId,
vid: subject.videoId,
iat: now,
exp: now + ttlSeconds,
};
const encodedPayload = Buffer.from(JSON.stringify(payload), 'utf8').toString('base64url');
const signature = signPayload(encodedPayload, getBunnyUploadTokenSecret());
return `${encodedPayload}.${signature}`;
}
export function verifyBunnyUploadToken(token: string, subject: BunnyUploadTokenSubject): boolean {
try {
const parts = token.split('.');
if (parts.length !== 2) return false;
const [encodedPayload, providedSignature] = parts;
if (!encodedPayload || !providedSignature) return false;
const expectedSignature = signPayload(encodedPayload, getBunnyUploadTokenSecret());
const providedBuffer = Buffer.from(providedSignature, 'utf8');
const expectedBuffer = Buffer.from(expectedSignature, 'utf8');
if (providedBuffer.length !== expectedBuffer.length) return false;
if (!crypto.timingSafeEqual(providedBuffer, expectedBuffer)) return false;
const payloadJson = Buffer.from(encodedPayload, 'base64url').toString('utf8');
const payloadUnknown: unknown = JSON.parse(payloadJson);
if (!isValidPayload(payloadUnknown)) return false;
const payload = payloadUnknown;
const now = Math.floor(Date.now() / 1000);
if (payload.exp < now) return false;
return payload.uid === subject.userId
&& payload.pid === subject.projectId
&& payload.vid === subject.videoId;
} catch {
return false;
}
}