Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added subtitles service routes #1092

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 },
);
}
}
Loading