diff --git a/prisma/migrations/20240830223934_add_subtitle_tries/migration.sql b/prisma/migrations/20240830223934_add_subtitle_tries/migration.sql new file mode 100644 index 000000000..1388d2732 --- /dev/null +++ b/prisma/migrations/20240830223934_add_subtitle_tries/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "VideoMetadata" ADD COLUMN "subtitle_tried" INTEGER NOT NULL DEFAULT 0; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e614cf77e..f21792668 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -112,6 +112,7 @@ model VideoMetadata { video_360p_3 String? // Link to 360p quality video variant 3 video_360p_4 String? // Link to 360p quality video variant 4 subtitles String? // Link to subtitles file + subtitle_tried Int @default(0) //Count of subtitle transcoding tries segments Json? content Content @relation(fields: [contentId], references: [id]) slides String? // link to slides @@ -342,3 +343,4 @@ enum MigrationStatus { MIGRATED MIGRATION_ERROR } + diff --git a/src/app/api/admin/services/subtitle/route.ts b/src/app/api/admin/services/subtitle/route.ts new file mode 100644 index 000000000..08391f64f --- /dev/null +++ b/src/app/api/admin/services/subtitle/route.ts @@ -0,0 +1,91 @@ +import db from '@/db'; +import { NextRequest, NextResponse } from 'next/server'; +import { z } from 'zod'; + +const patchVideoSchema = z.object({ + id: z.number(), + subtitleUrl: z.string(), +}); + +export async function GET(req: NextRequest) { + const authKey = req.headers.get('Authorization'); + + if (authKey !== process.env.SUBTITLE_SECRET) + return NextResponse.json({ message: 'Unauthorized' }, { status: 403 }); + + try { + const video = await db.videoMetadata.findFirst({ + where: { + subtitles: null, + }, + orderBy: [ + { + subtitle_tried: 'desc', + }, + { + contentId: 'desc', + }, + ], + include: { + content: { + include: { + courses: true, + }, + }, + }, + }); + if (!video) + return NextResponse.json( + { message: 'No more video to process' }, + { status: 404 }, + ); + + await db.videoMetadata.update({ + where: { + id: video?.id, + }, + data: { + subtitle_tried: { + increment: 1, + }, + }, + }); + return NextResponse.json(video); + } catch (error) { + console.log(error); + return NextResponse.json( + { message: 'Error fetching video' }, + { status: 500 }, + ); + } +} + +export async function PATCH(req: NextRequest) { + const authKey = req.headers.get('Authorization'); + + if (authKey !== process.env.SUBTITLE_SECRET) + return NextResponse.json({ message: 'Unauthorized' }, { status: 403 }); + + const requestBody = await req.json(); + const { success } = patchVideoSchema.safeParse(requestBody); + + if (!success) + return NextResponse.json({ message: 'Invalid request' }, { status: 400 }); + + try { + const video = await db.videoMetadata.update({ + where: { + id: requestBody.id, + }, + data: { + subtitles: requestBody.subtitleUrl, + }, + }); + return NextResponse.json(video); + } catch (error) { + return NextResponse.json( + { message: 'Error updating subtitle' }, + { status: 500 }, + ); + } +}