mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +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}`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { BunnyCleanupResult } from '@/lib/bunny-stream-cleanup';
|
||||
import type { R2CleanupResult } from '@/lib/r2-cleanup';
|
||||
|
||||
interface CleanupWarningSummary {
|
||||
attempted: number;
|
||||
failed: number;
|
||||
}
|
||||
|
||||
export interface CleanupWarnings {
|
||||
bunny?: CleanupWarningSummary;
|
||||
r2?: CleanupWarningSummary;
|
||||
}
|
||||
|
||||
export function buildCleanupWarnings(input: {
|
||||
bunny?: BunnyCleanupResult;
|
||||
r2?: R2CleanupResult;
|
||||
}): CleanupWarnings | undefined {
|
||||
const warnings: CleanupWarnings = {};
|
||||
|
||||
if (input.bunny && input.bunny.failed > 0) {
|
||||
warnings.bunny = {
|
||||
attempted: input.bunny.attempted,
|
||||
failed: input.bunny.failed,
|
||||
};
|
||||
}
|
||||
|
||||
if (input.r2 && input.r2.failed > 0) {
|
||||
warnings.r2 = {
|
||||
attempted: input.r2.attempted,
|
||||
failed: input.r2.failed,
|
||||
};
|
||||
}
|
||||
|
||||
return Object.keys(warnings).length > 0 ? warnings : undefined;
|
||||
}
|
||||
|
||||
export function logCleanupWarnings(
|
||||
context: { entityType: string; entityId: string },
|
||||
input: { bunny?: BunnyCleanupResult; r2?: R2CleanupResult }
|
||||
): void {
|
||||
if (input.bunny && input.bunny.failed > 0) {
|
||||
console.error('External cleanup warning', {
|
||||
entityType: context.entityType,
|
||||
entityId: context.entityId,
|
||||
provider: 'bunny',
|
||||
operation: 'delete',
|
||||
attempted: input.bunny.attempted,
|
||||
failed: input.bunny.failed,
|
||||
failedIds: input.bunny.failedIds.slice(0, 10),
|
||||
});
|
||||
}
|
||||
|
||||
if (input.r2 && input.r2.failed > 0) {
|
||||
console.error('External cleanup warning', {
|
||||
entityType: context.entityType,
|
||||
entityId: context.entityId,
|
||||
provider: 'r2',
|
||||
operation: 'delete',
|
||||
attempted: input.r2.attempted,
|
||||
failed: input.r2.failed,
|
||||
failedKeys: input.r2.failedKeys.slice(0, 10),
|
||||
});
|
||||
}
|
||||
}
|
||||
+43
-11
@@ -8,17 +8,25 @@ const IMAGE_PATH_PREFIX = '/api/upload/image/';
|
||||
/** The path prefix for audio URLs served by the upload API. */
|
||||
const AUDIO_PATH_PREFIX = '/api/upload/audio/';
|
||||
const CLEANUP_DELETE_CONCURRENCY = 5;
|
||||
const SAFE_IMAGE_PATH = /^\/api\/upload\/image\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z0-9]+$/i;
|
||||
const SAFE_AUDIO_PATH = /^\/api\/upload\/audio\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z0-9]+$/i;
|
||||
|
||||
export interface R2CleanupResult {
|
||||
attempted: number;
|
||||
failed: number;
|
||||
failedKeys: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the R2 object key from a media URL.
|
||||
* Uses string parsing instead of regex to avoid ReDoS risk on untrusted input.
|
||||
* Accept only canonical upload URLs before deriving a storage key.
|
||||
*/
|
||||
function mediaUrlToKey(url: string): string | null {
|
||||
if (url.includes(AUDIO_PATH_PREFIX)) {
|
||||
const filename = url.slice(url.indexOf(AUDIO_PATH_PREFIX) + AUDIO_PATH_PREFIX.length);
|
||||
export function mediaUrlToKey(url: string): string | null {
|
||||
if (SAFE_AUDIO_PATH.test(url)) {
|
||||
const filename = url.slice(AUDIO_PATH_PREFIX.length);
|
||||
return filename ? `voice/${filename}` : null;
|
||||
} else if (url.includes(IMAGE_PATH_PREFIX)) {
|
||||
const filename = url.slice(url.indexOf(IMAGE_PATH_PREFIX) + IMAGE_PATH_PREFIX.length);
|
||||
} else if (SAFE_IMAGE_PATH.test(url)) {
|
||||
const filename = url.slice(IMAGE_PATH_PREFIX.length);
|
||||
return filename ? `images/${filename}` : null;
|
||||
}
|
||||
return null;
|
||||
@@ -27,8 +35,25 @@ function mediaUrlToKey(url: string): string | null {
|
||||
/**
|
||||
* Delete a list of media files from R2 (best-effort, logs failures).
|
||||
*/
|
||||
async function deleteMediaFiles(mediaUrls: string[]) {
|
||||
const mediaKeys = [...new Set(mediaUrls.map(mediaUrlToKey).filter((key): key is string => Boolean(key)))];
|
||||
export async function deleteMediaFilesBestEffort(mediaUrls: string[]): Promise<R2CleanupResult> {
|
||||
const invalidUrls: string[] = [];
|
||||
const mediaKeys = [...new Set(
|
||||
mediaUrls
|
||||
.map((url) => {
|
||||
const key = mediaUrlToKey(url);
|
||||
if (!key) invalidUrls.push(url);
|
||||
return key;
|
||||
})
|
||||
.filter((key): key is string => Boolean(key))
|
||||
)];
|
||||
const failedKeys = new Set<string>();
|
||||
|
||||
if (invalidUrls.length > 0) {
|
||||
console.error('Skipping non-canonical media URLs during R2 cleanup', {
|
||||
rejectedCount: invalidUrls.length,
|
||||
rejectedSamples: invalidUrls.slice(0, 10),
|
||||
});
|
||||
}
|
||||
|
||||
await runWithConcurrency(mediaKeys, CLEANUP_DELETE_CONCURRENCY, async (key) => {
|
||||
try {
|
||||
@@ -36,9 +61,16 @@ async function deleteMediaFiles(mediaUrls: string[]) {
|
||||
new DeleteObjectCommand({ Bucket: R2_BUCKET_NAME, Key: key })
|
||||
);
|
||||
} catch (err) {
|
||||
failedKeys.add(key);
|
||||
console.error(`Failed to delete media from R2 (key: ${key}):`, err);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
attempted: mediaKeys.length,
|
||||
failed: failedKeys.size,
|
||||
failedKeys: [...failedKeys],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +172,7 @@ export async function collectWorkspaceMediaUrls(workspaceId: string): Promise<st
|
||||
*/
|
||||
export async function cleanupVideoMediaFiles(videoId: string) {
|
||||
const urls = await collectVideoMediaUrls(videoId);
|
||||
await deleteMediaFiles(urls);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +181,7 @@ export async function cleanupVideoMediaFiles(videoId: string) {
|
||||
*/
|
||||
export async function cleanupProjectMediaFiles(projectId: string) {
|
||||
const urls = await collectProjectMediaUrls(projectId);
|
||||
await deleteMediaFiles(urls);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,5 +190,5 @@ export async function cleanupProjectMediaFiles(projectId: string) {
|
||||
*/
|
||||
export async function cleanupWorkspaceMediaFiles(workspaceId: string) {
|
||||
const urls = await collectWorkspaceMediaUrls(workspaceId);
|
||||
await deleteMediaFiles(urls);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user