-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1092 from SujithThirumalaisamy/new
Added subtitles service routes
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20240830223934_add_subtitle_tries/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); | ||
} | ||
} |