Skip to content

Commit

Permalink
Merge pull request #1092 from SujithThirumalaisamy/new
Browse files Browse the repository at this point in the history
Added subtitles service routes
  • Loading branch information
hkirat authored Aug 31, 2024
2 parents 1565ed6 + 2709981 commit e1dd1cc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "VideoMetadata" ADD COLUMN "subtitle_tried" INTEGER NOT NULL DEFAULT 0;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -342,3 +343,4 @@ enum MigrationStatus {
MIGRATED
MIGRATION_ERROR
}

91 changes: 91 additions & 0 deletions src/app/api/admin/services/subtitle/route.ts
Original file line number Diff line number Diff line change
@@ -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 },
);
}
}

0 comments on commit e1dd1cc

Please sign in to comment.