mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
refactor: eslint and prettier conflict will be resolved and formatted
This commit is contained in:
+128
-126
@@ -9,13 +9,15 @@ 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;
|
||||
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[];
|
||||
attempted: number;
|
||||
failed: number;
|
||||
failedKeys: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,148 +25,148 @@ export interface R2CleanupResult {
|
||||
* Accept only canonical upload URLs before deriving a storage key.
|
||||
*/
|
||||
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 (SAFE_IMAGE_PATH.test(url)) {
|
||||
const filename = url.slice(IMAGE_PATH_PREFIX.length);
|
||||
return filename ? `images/${filename}` : null;
|
||||
}
|
||||
return null;
|
||||
if (SAFE_AUDIO_PATH.test(url)) {
|
||||
const filename = url.slice(AUDIO_PATH_PREFIX.length);
|
||||
return filename ? `voice/${filename}` : null;
|
||||
} else if (SAFE_IMAGE_PATH.test(url)) {
|
||||
const filename = url.slice(IMAGE_PATH_PREFIX.length);
|
||||
return filename ? `images/${filename}` : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a list of media files from R2 (best-effort, logs failures).
|
||||
*/
|
||||
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>();
|
||||
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 {
|
||||
await r2Client.send(
|
||||
new DeleteObjectCommand({ Bucket: R2_BUCKET_NAME, Key: key })
|
||||
);
|
||||
} catch (err) {
|
||||
failedKeys.add(key);
|
||||
logError(`Failed to delete media from R2 (key: ${key}):`, err);
|
||||
}
|
||||
if (invalidUrls.length > 0) {
|
||||
console.error('Skipping non-canonical media URLs during R2 cleanup', {
|
||||
rejectedCount: invalidUrls.length,
|
||||
rejectedSamples: invalidUrls.slice(0, 10),
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
attempted: mediaKeys.length,
|
||||
failed: failedKeys.size,
|
||||
failedKeys: [...failedKeys],
|
||||
};
|
||||
await runWithConcurrency(mediaKeys, CLEANUP_DELETE_CONCURRENCY, async (key) => {
|
||||
try {
|
||||
await r2Client.send(new DeleteObjectCommand({ Bucket: R2_BUCKET_NAME, Key: key }));
|
||||
} catch (err) {
|
||||
failedKeys.add(key);
|
||||
logError(`Failed to delete media from R2 (key: ${key}):`, err);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
attempted: mediaKeys.length,
|
||||
failed: failedKeys.size,
|
||||
failedKeys: [...failedKeys],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all media URLs from comments under a given video (all versions).
|
||||
*/
|
||||
export async function collectVideoMediaUrls(videoId: string): Promise<string[]> {
|
||||
const [comments, assets] = await Promise.all([
|
||||
db.comment.findMany({
|
||||
where: {
|
||||
OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }],
|
||||
version: { videoParentId: videoId },
|
||||
},
|
||||
select: { voiceUrl: true, imageUrl: true },
|
||||
}),
|
||||
db.videoAsset.findMany({
|
||||
where: {
|
||||
videoId,
|
||||
provider: 'R2_IMAGE',
|
||||
},
|
||||
select: { sourceUrl: true },
|
||||
}),
|
||||
]);
|
||||
const urls: string[] = [];
|
||||
comments.forEach(c => {
|
||||
if (c.voiceUrl) urls.push(c.voiceUrl);
|
||||
if (c.imageUrl) urls.push(c.imageUrl);
|
||||
});
|
||||
assets.forEach((asset) => {
|
||||
if (asset.sourceUrl) urls.push(asset.sourceUrl);
|
||||
});
|
||||
return urls;
|
||||
const [comments, assets] = await Promise.all([
|
||||
db.comment.findMany({
|
||||
where: {
|
||||
OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }],
|
||||
version: { videoParentId: videoId },
|
||||
},
|
||||
select: { voiceUrl: true, imageUrl: true },
|
||||
}),
|
||||
db.videoAsset.findMany({
|
||||
where: {
|
||||
videoId,
|
||||
provider: 'R2_IMAGE',
|
||||
},
|
||||
select: { sourceUrl: true },
|
||||
}),
|
||||
]);
|
||||
const urls: string[] = [];
|
||||
comments.forEach((c) => {
|
||||
if (c.voiceUrl) urls.push(c.voiceUrl);
|
||||
if (c.imageUrl) urls.push(c.imageUrl);
|
||||
});
|
||||
assets.forEach((asset) => {
|
||||
if (asset.sourceUrl) urls.push(asset.sourceUrl);
|
||||
});
|
||||
return urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all media URLs from comments under all videos in a project.
|
||||
*/
|
||||
export async function collectProjectMediaUrls(projectId: string): Promise<string[]> {
|
||||
const [comments, assets] = await Promise.all([
|
||||
db.comment.findMany({
|
||||
where: {
|
||||
OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }],
|
||||
version: { video: { projectId } },
|
||||
},
|
||||
select: { voiceUrl: true, imageUrl: true },
|
||||
}),
|
||||
db.videoAsset.findMany({
|
||||
where: {
|
||||
provider: 'R2_IMAGE',
|
||||
video: { projectId },
|
||||
},
|
||||
select: { sourceUrl: true },
|
||||
}),
|
||||
]);
|
||||
const urls: string[] = [];
|
||||
comments.forEach(c => {
|
||||
if (c.voiceUrl) urls.push(c.voiceUrl);
|
||||
if (c.imageUrl) urls.push(c.imageUrl);
|
||||
});
|
||||
assets.forEach((asset) => {
|
||||
if (asset.sourceUrl) urls.push(asset.sourceUrl);
|
||||
});
|
||||
return urls;
|
||||
const [comments, assets] = await Promise.all([
|
||||
db.comment.findMany({
|
||||
where: {
|
||||
OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }],
|
||||
version: { video: { projectId } },
|
||||
},
|
||||
select: { voiceUrl: true, imageUrl: true },
|
||||
}),
|
||||
db.videoAsset.findMany({
|
||||
where: {
|
||||
provider: 'R2_IMAGE',
|
||||
video: { projectId },
|
||||
},
|
||||
select: { sourceUrl: true },
|
||||
}),
|
||||
]);
|
||||
const urls: string[] = [];
|
||||
comments.forEach((c) => {
|
||||
if (c.voiceUrl) urls.push(c.voiceUrl);
|
||||
if (c.imageUrl) urls.push(c.imageUrl);
|
||||
});
|
||||
assets.forEach((asset) => {
|
||||
if (asset.sourceUrl) urls.push(asset.sourceUrl);
|
||||
});
|
||||
return urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all media URLs from comments under all projects in a workspace.
|
||||
*/
|
||||
export async function collectWorkspaceMediaUrls(workspaceId: string): Promise<string[]> {
|
||||
const [comments, assets] = await Promise.all([
|
||||
db.comment.findMany({
|
||||
where: {
|
||||
OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }],
|
||||
version: { video: { project: { workspaceId } } },
|
||||
},
|
||||
select: { voiceUrl: true, imageUrl: true },
|
||||
}),
|
||||
db.videoAsset.findMany({
|
||||
where: {
|
||||
provider: 'R2_IMAGE',
|
||||
video: { project: { workspaceId } },
|
||||
},
|
||||
select: { sourceUrl: true },
|
||||
}),
|
||||
]);
|
||||
const urls: string[] = [];
|
||||
comments.forEach(c => {
|
||||
if (c.voiceUrl) urls.push(c.voiceUrl);
|
||||
if (c.imageUrl) urls.push(c.imageUrl);
|
||||
});
|
||||
assets.forEach((asset) => {
|
||||
if (asset.sourceUrl) urls.push(asset.sourceUrl);
|
||||
});
|
||||
return urls;
|
||||
const [comments, assets] = await Promise.all([
|
||||
db.comment.findMany({
|
||||
where: {
|
||||
OR: [{ voiceUrl: { not: null } }, { imageUrl: { not: null } }],
|
||||
version: { video: { project: { workspaceId } } },
|
||||
},
|
||||
select: { voiceUrl: true, imageUrl: true },
|
||||
}),
|
||||
db.videoAsset.findMany({
|
||||
where: {
|
||||
provider: 'R2_IMAGE',
|
||||
video: { project: { workspaceId } },
|
||||
},
|
||||
select: { sourceUrl: true },
|
||||
}),
|
||||
]);
|
||||
const urls: string[] = [];
|
||||
comments.forEach((c) => {
|
||||
if (c.voiceUrl) urls.push(c.voiceUrl);
|
||||
if (c.imageUrl) urls.push(c.imageUrl);
|
||||
});
|
||||
assets.forEach((asset) => {
|
||||
if (asset.sourceUrl) urls.push(asset.sourceUrl);
|
||||
});
|
||||
return urls;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -172,8 +174,8 @@ export async function collectWorkspaceMediaUrls(workspaceId: string): Promise<st
|
||||
* Call BEFORE deleting the video from the database (cascade would remove comment rows).
|
||||
*/
|
||||
export async function cleanupVideoMediaFiles(videoId: string) {
|
||||
const urls = await collectVideoMediaUrls(videoId);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
const urls = await collectVideoMediaUrls(videoId);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,8 +183,8 @@ export async function cleanupVideoMediaFiles(videoId: string) {
|
||||
* Call BEFORE deleting the project from the database.
|
||||
*/
|
||||
export async function cleanupProjectMediaFiles(projectId: string) {
|
||||
const urls = await collectProjectMediaUrls(projectId);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
const urls = await collectProjectMediaUrls(projectId);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,6 +192,6 @@ export async function cleanupProjectMediaFiles(projectId: string) {
|
||||
* Call BEFORE deleting the workspace from the database.
|
||||
*/
|
||||
export async function cleanupWorkspaceMediaFiles(workspaceId: string) {
|
||||
const urls = await collectWorkspaceMediaUrls(workspaceId);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
const urls = await collectWorkspaceMediaUrls(workspaceId);
|
||||
await deleteMediaFilesBestEffort(urls);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user