From 792f153b856e2c5ba2ab9a6f892a9bba9149aad1 Mon Sep 17 00:00:00 2001 From: SujithThirumalaisamy Date: Sat, 31 Aug 2024 22:24:40 +0530 Subject: [PATCH 1/4] Added subtitle services routes --- .../migration.sql | 2 + prisma/schema.prisma | 2 + src/app/api/admin/services/subtitle/route.ts | 91 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 prisma/migrations/20240830223934_add_subtitle_tries/migration.sql create mode 100644 src/app/api/admin/services/subtitle/route.ts 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..209c1a268 --- /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 }, + ); + } +} From 802ad90ff605be73550876132e80e208071c5e9a Mon Sep 17 00:00:00 2001 From: SujithThirumalaisamy Date: Sat, 31 Aug 2024 22:25:16 +0530 Subject: [PATCH 2/4] Added subtitle services routes --- src/actions/payoutMethods/schema.ts | 8 ++++---- src/app/api/admin/services/subtitle/route.ts | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/actions/payoutMethods/schema.ts b/src/actions/payoutMethods/schema.ts index 461a934aa..0bb9d7c36 100644 --- a/src/actions/payoutMethods/schema.ts +++ b/src/actions/payoutMethods/schema.ts @@ -3,13 +3,13 @@ import { z } from 'zod'; export const payoutMethodSchema = z.object({ upiId: z .string() - .refine((value) => (/^[0-9A-Za-z._-]{2,256}@[A-Za-z]{2,64}$/).test(value), { + .refine((value) => /^[0-9A-Za-z._-]{2,256}@[A-Za-z]{2,64}$/.test(value), { message: 'Enter a valid UPI address', }) .optional(), solanaAddress: z .string() - .refine((value) => (/^[A-Za-z0-9]{44}$/).test(value), { + .refine((value) => /^[A-Za-z0-9]{44}$/.test(value), { message: 'Enter a valid Solana address', }) .optional(), @@ -18,13 +18,13 @@ export const payoutMethodSchema = z.object({ export const upiIdInsertSchema = z.object({ upiId: z .string() - .refine((value) => (/^[0-9A_Za-z._-]{2,256}@[A_Za-z]{2,64}$/).test(value), { + .refine((value) => /^[0-9A_Za-z._-]{2,256}@[A_Za-z]{2,64}$/.test(value), { message: 'Invalid UPI address', }), }); export const solanaAddressInsertSchema = z.object({ - solanaAddress: z.string().refine((value) => (/^[A-Za-z0-9]{44}$/).test(value), { + solanaAddress: z.string().refine((value) => /^[A-Za-z0-9]{44}$/.test(value), { message: 'Invalid Solana address', }), }); diff --git a/src/app/api/admin/services/subtitle/route.ts b/src/app/api/admin/services/subtitle/route.ts index 209c1a268..08391f64f 100644 --- a/src/app/api/admin/services/subtitle/route.ts +++ b/src/app/api/admin/services/subtitle/route.ts @@ -29,10 +29,10 @@ export async function GET(req: NextRequest) { include: { content: { include: { - courses: true - } + courses: true, + }, }, - } + }, }); if (!video) return NextResponse.json( From ab67d8e3557b80f6aa26e797b47b2d723e53215d Mon Sep 17 00:00:00 2001 From: Sujith Thirumalaisamy <108384868+SujithThirumalaisamy@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:31:47 +0530 Subject: [PATCH 3/4] Revert lint --- src/actions/payoutMethods/schema.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/actions/payoutMethods/schema.ts b/src/actions/payoutMethods/schema.ts index 0bb9d7c36..03ba4645e 100644 --- a/src/actions/payoutMethods/schema.ts +++ b/src/actions/payoutMethods/schema.ts @@ -3,7 +3,7 @@ import { z } from 'zod'; export const payoutMethodSchema = z.object({ upiId: z .string() - .refine((value) => /^[0-9A-Za-z._-]{2,256}@[A-Za-z]{2,64}$/.test(value), { + .refine((value) => (/^[0-9A-Za-z._-]{2,256}@[A-Za-z]{2,64}$/).test(value), { message: 'Enter a valid UPI address', }) .optional(), @@ -18,13 +18,13 @@ export const payoutMethodSchema = z.object({ export const upiIdInsertSchema = z.object({ upiId: z .string() - .refine((value) => /^[0-9A_Za-z._-]{2,256}@[A_Za-z]{2,64}$/.test(value), { + .refine((value) => (/^[0-9A_Za-z._-]{2,256}@[A_Za-z]{2,64}$/).test(value), { message: 'Invalid UPI address', }), }); export const solanaAddressInsertSchema = z.object({ - solanaAddress: z.string().refine((value) => /^[A-Za-z0-9]{44}$/.test(value), { + solanaAddress: z.string().refine((value) => (/^[A-Za-z0-9]{44}$/).test(value), { message: 'Invalid Solana address', }), }); From 270998102ea5fe707d6ef6498f9ded085a3d2040 Mon Sep 17 00:00:00 2001 From: Sujith Thirumalaisamy <108384868+SujithThirumalaisamy@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:32:16 +0530 Subject: [PATCH 4/4] Revert lint --- src/actions/payoutMethods/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/actions/payoutMethods/schema.ts b/src/actions/payoutMethods/schema.ts index 03ba4645e..461a934aa 100644 --- a/src/actions/payoutMethods/schema.ts +++ b/src/actions/payoutMethods/schema.ts @@ -9,7 +9,7 @@ export const payoutMethodSchema = z.object({ .optional(), solanaAddress: z .string() - .refine((value) => /^[A-Za-z0-9]{44}$/.test(value), { + .refine((value) => (/^[A-Za-z0-9]{44}$/).test(value), { message: 'Enter a valid Solana address', }) .optional(),