mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat: make media cleanup best-effort with warning summaries and enforce video/workspace management access
This commit is contained in:
+73
-38
@@ -1,10 +1,16 @@
|
||||
import { runWithConcurrency } from '@/lib/async-pool';
|
||||
|
||||
interface BunnyVideoRef {
|
||||
export interface BunnyVideoRef {
|
||||
providerId: string;
|
||||
videoId: string;
|
||||
}
|
||||
|
||||
export interface BunnyCleanupResult {
|
||||
attempted: number;
|
||||
failed: number;
|
||||
failedIds: string[];
|
||||
}
|
||||
|
||||
const BUNNY_API_BASE = 'https://video.bunnycdn.com';
|
||||
const BUNNY_VIDEO_ID_PATTERN = /^[A-Za-z0-9_-]{8,128}$/;
|
||||
const BUNNY_DELETE_CONCURRENCY = 5;
|
||||
@@ -20,50 +26,79 @@ function getBunnyConfig(): { apiKey: string; libraryId: string } {
|
||||
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;
|
||||
};
|
||||
function 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))
|
||||
)];
|
||||
function getUniqueBunnyVideoIds(videoRefs: BunnyVideoRef[]): string[] {
|
||||
return [
|
||||
...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;
|
||||
export async function cleanupBunnyStreamVideosBestEffort(videoRefs: BunnyVideoRef[]): Promise<BunnyCleanupResult> {
|
||||
const bunnyVideoIds = getUniqueBunnyVideoIds(videoRefs);
|
||||
if (bunnyVideoIds.length === 0) {
|
||||
return {
|
||||
attempted: 0,
|
||||
failed: 0,
|
||||
failedIds: [],
|
||||
};
|
||||
}
|
||||
|
||||
const { apiKey, libraryId } = getBunnyConfig();
|
||||
const failures: Array<{ videoId: string; status: number; bodySnippet: string }> = [];
|
||||
let apiKey: string;
|
||||
let libraryId: string;
|
||||
try {
|
||||
const config = getBunnyConfig();
|
||||
apiKey = config.apiKey;
|
||||
libraryId = config.libraryId;
|
||||
} catch {
|
||||
return {
|
||||
attempted: bunnyVideoIds.length,
|
||||
failed: bunnyVideoIds.length,
|
||||
failedIds: bunnyVideoIds,
|
||||
};
|
||||
}
|
||||
|
||||
const failedIds = new Set<string>();
|
||||
|
||||
await runWithConcurrency(bunnyVideoIds, BUNNY_DELETE_CONCURRENCY, async (bunnyVideoId) => {
|
||||
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) return;
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await response.text().catch(() => '');
|
||||
failures.push({
|
||||
videoId: bunnyVideoId,
|
||||
status: response.status,
|
||||
bodySnippet: body.slice(0, 300),
|
||||
try {
|
||||
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) return;
|
||||
|
||||
if (!response.ok) {
|
||||
failedIds.add(bunnyVideoId);
|
||||
}
|
||||
} catch {
|
||||
failedIds.add(bunnyVideoId);
|
||||
}
|
||||
});
|
||||
|
||||
if (failures.length > 0) {
|
||||
const preview = failures
|
||||
.slice(0, 3)
|
||||
.map((failure) => `${failure.videoId} (${failure.status})`)
|
||||
.join(', ');
|
||||
throw new Error(`Bunny cleanup failed for ${failures.length} video(s): ${preview}`);
|
||||
}
|
||||
return {
|
||||
attempted: bunnyVideoIds.length,
|
||||
failed: failedIds.size,
|
||||
failedIds: [...failedIds],
|
||||
};
|
||||
}
|
||||
|
||||
export async function cleanupBunnyStreamVideos(videoRefs: BunnyVideoRef[]): Promise<void> {
|
||||
const result = await cleanupBunnyStreamVideosBestEffort(videoRefs);
|
||||
if (result.failed === 0) return;
|
||||
|
||||
const preview = result.failedIds.slice(0, 3).join(', ');
|
||||
throw new Error(`Bunny cleanup failed for ${result.failed} video(s): ${preview}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user