Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Current playlist url route

Richie McIlroyd9ef0b85ca13
diff --git a/apps/web/app/api/video/playlistUrl/route.ts b/apps/web/app/api/video/playlistUrl/route.ts
new file mode 100644
index 000000000..7786c4be1
--- /dev/null
+++ b/apps/web/app/api/video/playlistUrl/route.ts
@@ -0,0 +1,72 @@
+import { type NextRequest } from "next/server";
+import { db } from "@cap/database";
+import { videos } from "@cap/database/schema";
+import { eq } from "drizzle-orm";
+import { getHeaders } from "@/utils/helpers";
+
+export const revalidate = 0;
+
+export async function OPTIONS(request: NextRequest) {
+ const origin = request.headers.get("origin") as string;
+
+ return new Response(null, {
+ status: 200,
+ headers: getHeaders(origin),
+ });
+}
+
+export async function GET(request: NextRequest) {
+ const searchParams = request.nextUrl.searchParams;
+ const userId = searchParams.get("userId") || "";
+ const videoId = searchParams.get("videoId") || "";
+ const origin = request.headers.get("origin") as string;
+
+ if (!userId || !videoId) {
+ return new Response(
+ JSON.stringify({
+ error: true,
+ message: "userId or videoId not supplied",
+ }),
+ {
+ status: 401,
+ headers: getHeaders(origin),
+ }
+ );
+ }
+
+ const query = await db.select().from(videos).where(eq(videos.id, videoId));
+
+ if (query.length === 0) {
+ return new Response(
+ JSON.stringify({ error: true, message: "Video does not exist" }),
+ {
+ status: 401,
+ headers: getHeaders(origin),
+ }
+ );
+ }
+
+ const video = query[0];
+
+ if (video.jobStatus === "COMPLETE") {
+ const playlistUrl = `https://v.cap.so/${video.ownerId}/${video.id}/output/video_recording_000_output.m3u8`;
+ return new Response(
+ JSON.stringify({ playlistOne: playlistUrl, playlistTwo: null }),
+ {
+ status: 200,
+ headers: getHeaders(origin),
+ }
+ );
+ }
+
+ return new Response(
+ JSON.stringify({
+ playlistOne: `${process.env.NEXT_PUBLIC_URL}/api/playlist?userId=${video.ownerId}&videoId=${video.id}&videoType=video`,
+ playlistTwo: `${process.env.NEXT_PUBLIC_URL}/api/playlist?userId=${video.ownerId}&videoId=${video.id}&videoType=audio`,
+ }),
+ {
+ status: 200,
+ headers: getHeaders(origin),
+ }
+ );
+}