Skip to content

Commit 8135eae

Browse files
committed
fix: added chunking for mass delete audio
1 parent f14e94a commit 8135eae

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/controllers/video-translation/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import config from "@/config";
88
import { generatePreSigned, deleteAudio, massDeleteAudio } from "@/s3/actions";
99
import { validateAuthToken } from "@/libs/security";
1010
import { getNavigationData, validateNavigation } from "@/libs/navigation";
11-
import { isValidId } from "@/libs/utils";
11+
import { chunks, isValidId } from "@/libs/utils";
1212
import { TranslationNotFound } from "@/errors";
1313

1414
export default new Elysia().group("/video-translation", (app) =>
@@ -207,7 +207,11 @@ export default new Elysia().group("/video-translation", (app) =>
207207
const filenames = translations
208208
.filter((translation) => translation.translated_url)
209209
.map((translation) => translation.translated_url!);
210-
await massDeleteAudio(filenames);
210+
211+
const chunkedFilenames = chunks(filenames, 900);
212+
await Promise.all(
213+
chunkedFilenames.map(async (chunk) => await massDeleteAudio(chunk)),
214+
);
211215
}
212216

213217
const count = translations?.length ?? 0;

src/libs/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export function isValidId(id: number) {
99
return id % 1 === 0 && id < 2147483647;
1010
}
1111

12+
export function chunks<T extends unknown[] = string[]>(a: T, size: number): T[] {
13+
return Array.from(Array.from({ length: Math.ceil(a.length / size) }), (_, i) =>
14+
a.slice(i * size, i * size + size),
15+
) as T[];
16+
}
17+
1218
// https://github.com/sinclairzx81/typebox/issues/1135
1319
export type TStringsToLiterals<
1420
Strings extends string[],

src/s3/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export async function deleteAudio(filename: string) {
7474
}
7575
}
7676

77+
/**
78+
* Maximum: 1000 filenames per requests
79+
*/
7780
export async function massDeleteAudio(filenames: string[]) {
7881
try {
7982
const results = await s3client.send(

0 commit comments

Comments
 (0)