Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add Cap share URL oEmbed builder

Richie McIlroy320a4e2d2a2c
diff --git a/apps/web/__tests__/unit/oembed.test.ts b/apps/web/__tests__/unit/oembed.test.ts
new file mode 100644
index 000000000..eba9f7021
--- /dev/null
+++ b/apps/web/__tests__/unit/oembed.test.ts
@@ -0,0 +1,89 @@
+import { Video } from "@cap/web-domain";
+import { describe, expect, it } from "vitest";
+import { buildOEmbedVideo, parseCapShareUrl } from "@/lib/oembed";
+import type { PublicShareVideo } from "@/lib/public-share-video";
+
+const video: PublicShareVideo = {
+ id: Video.VideoId.make("abc123"),
+ name: "Cap walkthrough",
+ ownerId: "owner123",
+ ownerName: "Richie",
+ sourceType: "desktopMP4",
+ width: 1920,
+ height: 1080,
+ duration: 64.4,
+};
+
+describe("Cap oEmbed", () => {
+ it.each([
+ ["https://cap.so/s/abc123", "abc123"],
+ ["https://www.cap.so/s/abc123?t=3", "abc123"],
+ ["https://cap.link/abc123", "abc123"],
+ ["https://www.cap.link/abc123?foo=bar", "abc123"],
+ ])("parses supported share URL %s", (url, expected) => {
+ expect(parseCapShareUrl(url)).toBe(expected);
+ });
+
+ it.each([
+ "http://cap.so/s/abc123",
+ "https://example.com/s/abc123",
+ "https://cap.so/embed/abc123",
+ "https://cap.link/video/demo.mp4",
+ "https://cap.so/s/abc123/extra",
+ "not-a-url",
+ ])("rejects unsupported URL %s", (url) => {
+ expect(parseCapShareUrl(url)).toBeNull();
+ });
+
+ it("returns a responsive video response with preserved dimensions", () => {
+ const result = buildOEmbedVideo({
+ video,
+ webUrl: "https://cap.so",
+ maxWidth: "640",
+ maxHeight: "400",
+ });
+
+ expect(result).toMatchObject({
+ version: "1.0",
+ type: "video",
+ provider_name: "Cap",
+ title: "Cap walkthrough",
+ author_name: "Richie",
+ cache_age: 300,
+ width: 640,
+ height: 360,
+ duration: 64,
+ });
+ expect(result.html).toContain('src="https://cap.so/embed/abc123"');
+ expect(result.html).toContain(
+ 'style="border:0;width:100%;aspect-ratio:640/360"',
+ );
+ expect(result.thumbnail_url).toBeUndefined();
+ });
+
+ it("never exceeds either requested dimension", () => {
+ const result = buildOEmbedVideo({
+ video,
+ webUrl: "https://cap.so",
+ maxWidth: "1000",
+ maxHeight: "300",
+ });
+
+ expect(result.width).toBe(533);
+ expect(result.height).toBe(300);
+ expect(result.thumbnail_url).toBeUndefined();
+ });
+
+ it("includes the full thumbnail when no smaller dimensions are requested", () => {
+ const result = buildOEmbedVideo({
+ video,
+ webUrl: "https://cap.so",
+ });
+
+ expect(result.thumbnail_url).toBe(
+ "https://cap.so/api/video/og?videoId=abc123",
+ );
+ expect(result.thumbnail_width).toBe(1200);
+ expect(result.thumbnail_height).toBe(630);
+ });
+});
diff --git a/apps/web/lib/oembed.ts b/apps/web/lib/oembed.ts
new file mode 100644
index 000000000..d6d72965a
--- /dev/null
+++ b/apps/web/lib/oembed.ts
@@ -0,0 +1,131 @@
+import type { PublicShareVideo } from "@/lib/public-share-video";
+
+const DEFAULT_WIDTH = 800;
+const DEFAULT_HEIGHT = 450;
+const MAX_DIMENSION = 1920;
+
+export type OEmbedVideo = {
+ version: "1.0";
+ type: "video";
+ provider_name: "Cap";
+ provider_url: string;
+ title: string;
+ author_name?: string;
+ cache_age: number;
+ html: string;
+ width: number;
+ height: number;
+ thumbnail_url?: string;
+ thumbnail_width?: number;
+ thumbnail_height?: number;
+ duration?: number;
+};
+
+const parsePositiveDimension = (value: string | undefined) => {
+ if (value === undefined) return undefined;
+ const number = Number(value);
+ if (!Number.isFinite(number) || number <= 0) return undefined;
+ return Math.min(Math.floor(number), MAX_DIMENSION);
+};
+
+const scaleDimensions = ({
+ sourceWidth,
+ sourceHeight,
+ maxWidth,
+ maxHeight,
+}: {
+ sourceWidth: number;
+ sourceHeight: number;
+ maxWidth?: number;
+ maxHeight?: number;
+}) => {
+ const widthScale = maxWidth ? maxWidth / sourceWidth : 1;
+ const heightScale = maxHeight ? maxHeight / sourceHeight : 1;
+ const scale = Math.min(1, widthScale, heightScale);
+
+ return {
+ width: Math.max(1, Math.round(sourceWidth * scale)),
+ height: Math.max(1, Math.round(sourceHeight * scale)),
+ };
+};
+
+export const parseCapShareUrl = (value: string) => {
+ let url: URL;
+ try {
+ url = new URL(value);
+ } catch {
+ return null;
+ }
+
+ if (url.protocol !== "https:") return null;
+ const hostname = url.hostname.toLowerCase().replace(/^www\./, "");
+ const segments = url.pathname.split("/").filter(Boolean);
+ let videoId: string | undefined;
+
+ if (hostname === "cap.so" && segments.length === 2 && segments[0] === "s") {
+ videoId = segments[1];
+ }
+ if (
+ hostname === "cap.link" &&
+ segments.length === 1 &&
+ segments[0] !== "video"
+ ) {
+ videoId = segments[0];
+ }
+
+ if (!videoId || !/^[A-Za-z0-9_-]{1,128}$/.test(videoId)) return null;
+ return videoId;
+};
+
+export const buildOEmbedVideo = ({
+ video,
+ webUrl,
+ maxWidth,
+ maxHeight,
+}: {
+ video: PublicShareVideo;
+ webUrl: string;
+ maxWidth?: string;
+ maxHeight?: string;
+}): OEmbedVideo => {
+ const parsedMaxWidth = parsePositiveDimension(maxWidth);
+ const parsedMaxHeight = parsePositiveDimension(maxHeight);
+ const sourceWidth =
+ video.width && video.width > 0 ? video.width : DEFAULT_WIDTH;
+ const sourceHeight =
+ video.height && video.height > 0 ? video.height : DEFAULT_HEIGHT;
+ const dimensions = scaleDimensions({
+ sourceWidth,
+ sourceHeight,
+ maxWidth: parsedMaxWidth,
+ maxHeight: parsedMaxHeight,
+ });
+ const embedUrl = new URL(`/embed/${video.id}`, webUrl).toString();
+ const thumbnailUrl = new URL("/api/video/og", webUrl);
+ thumbnailUrl.searchParams.set("videoId", video.id);
+ const html = `<iframe src="${embedUrl}" width="${dimensions.width}" height="${dimensions.height}" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen title="Cap video player" style="border:0;width:100%;aspect-ratio:${dimensions.width}/${dimensions.height}" loading="lazy"></iframe>`;
+
+ return {
+ version: "1.0",
+ type: "video",
+ provider_name: "Cap",
+ provider_url: new URL("/", webUrl).toString(),
+ title: video.name,
+ ...(video.ownerName ? { author_name: video.ownerName } : {}),
+ cache_age: 300,
+ html,
+ width: dimensions.width,
+ height: dimensions.height,
+ ...((parsedMaxWidth === undefined || parsedMaxWidth >= 1200) &&
+ (parsedMaxHeight === undefined || parsedMaxHeight >= 630)
+ ? {
+ thumbnail_url: thumbnailUrl.toString(),
+ thumbnail_width: 1200,
+ thumbnail_height: 630,
+ }
+ : {}),
+ ...(video.duration && video.duration > 0
+ ? { duration: Math.round(video.duration) }
+ : {}),
+ };
+};