Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Add og image to Cap video

Richie McIlroy1e5ffe3e4dfe
diff --git a/apps/web/app/api/video/og/route.tsx b/apps/web/app/api/video/og/route.tsx
new file mode 100644
index 000000000..d034df0f9
--- /dev/null
+++ b/apps/web/app/api/video/og/route.tsx
@@ -0,0 +1,164 @@
+import { db } from "@cap/database";
+import { videos } from "@cap/database/schema";
+import { eq } from "drizzle-orm";
+import { ImageResponse } from "next/og";
+import { NextRequest } from "next/server";
+import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
+import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
+import { Play } from "lucide-react";
+
+export const runtime = "edge";
+
+export async function GET(req: NextRequest) {
+ const videoId = req.nextUrl.searchParams.get("videoId") as string;
+ const query = await db.select().from(videos).where(eq(videos.id, videoId));
+
+ type FileKey = {
+ type: "screen";
+ key: string;
+ };
+
+ type ResponseObject = {
+ screen: string | null;
+ };
+
+ if (query.length === 0 || query?.[0]?.public === false) {
+ return new ImageResponse(
+ (
+ <div
+ style={{
+ width: "100%",
+ height: "100%",
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ background:
+ "radial-gradient(90.01% 80.01% at 53.53% 49.99%,#d3e5ff 30.65%,#82c6f1 88.48%,#fff 100%)",
+ }}
+ >
+ <h1 style={{ fontSize: "40px" }}>Video not found</h1>
+ </div>
+ ),
+ {
+ width: 1200,
+ height: 630,
+ }
+ );
+ }
+
+ const video = query[0];
+
+ const s3Client = new S3Client({
+ region: process.env.CAP_AWS_REGION || "",
+ credentials: {
+ accessKeyId: process.env.CAP_AWS_ACCESS_KEY || "",
+ secretAccessKey: process.env.CAP_AWS_SECRET_KEY || "",
+ },
+ });
+
+ const bucket = process.env.CAP_AWS_BUCKET;
+ const fileKeys: FileKey[] = [
+ {
+ type: "screen",
+ key: `${video.ownerId}/${video.id}/screenshot/screen-capture.jpg`,
+ },
+ ];
+
+ const responseObject: ResponseObject = {
+ screen: null,
+ };
+
+ await Promise.all(
+ fileKeys.map(async ({ type, key }) => {
+ try {
+ const url = await getSignedUrl(
+ s3Client,
+ new GetObjectCommand({ Bucket: bucket, Key: key }),
+ { expiresIn: 3600 }
+ );
+ responseObject[type] = url;
+ } catch (error) {
+ console.error("Error generating URL for:", key, error);
+ responseObject[type] = null;
+ }
+ })
+ );
+
+ return new ImageResponse(
+ (
+ <div
+ style={{
+ width: "100%",
+ height: "100%",
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ background:
+ "radial-gradient(90.01% 80.01% at 53.53% 49.99%,#d3e5ff 30.65%,#82c6f1 88.48%,#fff 100%)",
+ }}
+ >
+ <div
+ style={{
+ width: "85%",
+ height: "85%",
+ display: "flex",
+ borderRadius: "10px",
+ overflow: "hidden",
+ position: "relative",
+ background: "#000",
+ }}
+ >
+ <div
+ style={{
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ width: "200px",
+ height: "200px",
+ position: "absolute",
+ top: "50%",
+ left: "50%",
+ transform: "translate(-50%, -50%)",
+ zIndex: 10,
+ }}
+ >
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ style={{
+ width: "100%",
+ height: "100%",
+ color: "#ffffff",
+ display: "flex",
+ }}
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ >
+ <polygon points="6 3 20 12 6 21 6 3"></polygon>
+ </svg>
+ </div>
+ {responseObject.screen && (
+ <img
+ style={{
+ width: "100%",
+ height: "100%",
+ position: "absolute",
+ objectFit: "cover",
+ opacity: 0.4,
+ zIndex: 1,
+ }}
+ src={responseObject.screen}
+ />
+ )}
+ </div>
+ </div>
+ ),
+ {
+ width: 1200,
+ height: 630,
+ }
+ );
+}
diff --git a/apps/web/app/share/[videoId]/_components/ShareVideo.tsx b/apps/web/app/share/[videoId]/_components/ShareVideo.tsx
index dfb55fcba..3b00bdbfb 100644
--- a/apps/web/app/share/[videoId]/_components/ShareVideo.tsx
+++ b/apps/web/app/share/[videoId]/_components/ShareVideo.tsx
@@ -30,9 +30,6 @@ export const ShareVideo = ({
user: typeof userSelectProps | null;
comments: (typeof commentsSchema.$inferSelect)[];
}) => {
- console.log("comments:");
- console.log(comments);
-
const videoRef = useRef<HTMLVideoElement>(null);
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
@@ -291,35 +288,33 @@ export const ShareVideo = ({
if (comment.timestamp === null) return null;
return (
- <>
+ <div
+ key={comment.id}
+ className="absolute z-10 text-[16px]"
+ style={{
+ left: `${(comment.timestamp / longestDuration) * 100}%`,
+ }}
+ data-tooltip-id={comment.id}
+ data-tooltip-content={`${
+ comment.type === "text"
+ ? "User: " + comment.content
+ : comment.authorId === "anonymous"
+ ? "Anonymous"
+ : "User"
+ }`}
+ >
<Tooltip id={comment.id} />
- <div
- key={comment.id}
- className="absolute z-10 text-[16px]"
- style={{
- left: `${(comment.timestamp / longestDuration) * 100}%`,
- }}
- data-tooltip-id={comment.id}
- data-tooltip-content={`${
- comment.type === "text"
- ? "User: " + comment.content
- : comment.authorId === "anonymous"
- ? "Anonymous"
- : "User"
- }`}
- >
- <span>
- {comment.type === "text" ? (
- <MessageSquare
- fill="#646464"
- className="w-auto h-[22px] text-white"
- />
- ) : (
- comment.content
- )}
- </span>
- </div>
- </>
+ <span>
+ {comment.type === "text" ? (
+ <MessageSquare
+ fill="#646464"
+ className="w-auto h-[22px] text-white"
+ />
+ ) : (
+ comment.content
+ )}
+ </span>
+ </div>
);
})}
</div>
diff --git a/apps/web/app/share/[videoId]/page.tsx b/apps/web/app/share/[videoId]/page.tsx
index b34684abb..f5b022619 100644
--- a/apps/web/app/share/[videoId]/page.tsx
+++ b/apps/web/app/share/[videoId]/page.tsx
@@ -4,11 +4,43 @@ import { db } from "@cap/database";
import { eq } from "drizzle-orm";
import { videos, comments } from "@cap/database/schema";
import { getCurrentUser, userSelectProps } from "@cap/database/auth/session";
+import type { Metadata, ResolvingMetadata } from "next";
type Props = {
params: { [key: string]: string | string[] | undefined };
};
+export async function generateMetadata(
+ { params }: Props,
+ parent: ResolvingMetadata
+): Promise<Metadata> {
+ const videoId = params.videoId as string;
+ const query = await db.select().from(videos).where(eq(videos.id, videoId));
+ const video = query[0];
+
+ if (video.public === false) {
+ return {
+ title: "Cap: This video is private",
+ description: "This video is private and cannot be shared.",
+ openGraph: {
+ images: [
+ `${process.env.NEXT_PUBLIC_URL}/api/video/og?videoId=${videoId}`,
+ ],
+ },
+ };
+ }
+
+ return {
+ title: "Cap: " + video.name,
+ description: "Watch this video on Cap",
+ openGraph: {
+ images: [
+ `${process.env.NEXT_PUBLIC_URL}/api/video/og?videoId=${videoId}`,
+ ],
+ },
+ };
+}
+
export default async function ShareVideoPage(props: Props) {
const params = props.params;
const videoId = params.videoId as string;