Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: use published edit thumbnails across video surfaces

Richie McIlroy9f79ea025592
diff --git a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
index 7cba42a54..a89bf3ad9 100644
--- a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
+++ b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
@@ -797,6 +797,29 @@ afterEach(() => {
});
describe("recording storage lifecycle", () => {
+ it.each(["desktopMP4", "webMP4"] as const)(
+ "uses the published %s thumbnail without discovering older objects",
+ async (type) => {
+ const editThumbnail = `${prefix}.recording/outputs/edit-11111111-1111-4111-8111-111111111111/thumbnail.jpg`;
+ databaseFixture(recording({ type, thumbnailKey: editThumbnail }));
+ const storage = await storageFixture([
+ [editThumbnail, "current-thumbnail"],
+ [`${prefix}screenshot/screen-capture.jpg`, "old-thumbnail"],
+ ]);
+ const thumbnail = await Effect.runPromise(
+ Effect.flatMap(Videos, (videos) =>
+ videos.getThumbnailURL(videoId),
+ ).pipe(
+ Effect.provide(Videos.Default),
+ Effect.provideService(CurrentUser, currentUser),
+ ),
+ );
+ expect(Option.getOrNull(thumbnail)).toContain(editThumbnail);
+ expect(
+ storage.requests.filter(({ operation }) => operation === "list"),
+ ).toEqual([]);
+ },
+ );
it.each([
"committing",
"queued",
diff --git a/apps/web/__tests__/unit/thumbnail-published-output.test.ts b/apps/web/__tests__/unit/thumbnail-published-output.test.ts
new file mode 100644
index 000000000..0ab450839
--- /dev/null
+++ b/apps/web/__tests__/unit/thumbnail-published-output.test.ts
@@ -0,0 +1,91 @@
+import { Effect, Exit } from "effect";
+import { NextRequest } from "next/server";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ policy: vi.fn(),
+ access: vi.fn(),
+ list: vi.fn(),
+ sign: vi.fn(),
+}));
+vi.mock("@cap/database", () => ({ db: vi.fn() }));
+vi.mock("@cap/web-backend", () => ({
+ Storage: { getAccessForVideo: mocks.access },
+ VideosPolicy: {},
+ provideOptionalAuth: (value: unknown) => value,
+ findScreenshotObjectKey: (objects: { Key?: string }[]) =>
+ objects.find((object) => object.Key?.endsWith("screen-capture.jpg"))?.Key ??
+ null,
+}));
+vi.mock("@/lib/server", async () => ({
+ runPromise: (await import("effect")).Effect.runPromise,
+ runPromiseExit: mocks.policy,
+}));
+vi.mock("@/lib/video-storage", () => ({
+ decodeStorageVideo: (video: unknown) => video,
+}));
+vi.mock("@/utils/helpers", () => ({ getHeaders: () => ({}) }));
+
+import { GET } from "@/app/api/thumbnail/route";
+
+const prefix = "owner/video/";
+const thumbnailKey = `${prefix}.recording/outputs/edit-11111111-1111-4111-8111-111111111111/thumbnail.jpg`;
+const request = () =>
+ new NextRequest("https://cap.test/api/thumbnail?videoId=video");
+beforeEach(() => {
+ vi.resetAllMocks();
+ mocks.access.mockReturnValue(
+ Effect.succeed([
+ {
+ listObjects: mocks.list,
+ getSignedObjectUrl: mocks.sign,
+ },
+ ]),
+ );
+ mocks.list.mockReturnValue(
+ Effect.succeed({
+ Contents: [{ Key: `${prefix}screenshot/screen-capture.jpg` }],
+ }),
+ );
+ mocks.sign.mockImplementation((key: string) =>
+ Effect.succeed(`https://storage.test/${key}`),
+ );
+});
+
+describe("published edit thumbnails", () => {
+ it.each(["desktopMP4", "webMP4"])(
+ "serves the current %s thumbnail without scanning older screenshots",
+ async (type) => {
+ mocks.policy.mockResolvedValue(
+ Exit.succeed([
+ { id: "video", ownerId: "owner", source: { type, thumbnailKey } },
+ ]),
+ );
+ const response = await GET(request());
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({
+ screen: `https://storage.test/${thumbnailKey}`,
+ });
+ expect(mocks.list).not.toHaveBeenCalled();
+ },
+ );
+ it("keeps legacy thumbnail discovery when no published key exists", async () => {
+ mocks.policy.mockResolvedValue(
+ Exit.succeed([
+ { id: "video", ownerId: "owner", source: { type: "webMP4" } },
+ ]),
+ );
+ const response = await GET(request());
+ expect(response.status).toBe(200);
+ expect(mocks.list).toHaveBeenCalledOnce();
+ expect(mocks.sign).toHaveBeenCalledWith(
+ `${prefix}screenshot/screen-capture.jpg`,
+ );
+ });
+ it("checks viewing access before resolving an internal thumbnail", async () => {
+ mocks.policy.mockResolvedValue(Exit.fail("Forbidden"));
+ expect((await GET(request())).status).toBe(404);
+ expect(mocks.access).not.toHaveBeenCalled();
+ expect(mocks.sign).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/actions/videos/get-og-image.tsx b/apps/web/actions/videos/get-og-image.tsx
index 3117bd610..96a7a56ad 100644
--- a/apps/web/actions/videos/get-og-image.tsx
+++ b/apps/web/actions/videos/get-og-image.tsx
@@ -1,6 +1,7 @@
import { db } from "@cap/database";
import { users, videos } from "@cap/database/schema";
import { findScreenshotObjectKey, Storage } from "@cap/web-backend";
+import { getPublishedRecordingThumbnailKey } from "@cap/web-backend/src/Storage/recording-output";
import type { Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect } from "effect";
@@ -26,6 +27,11 @@ export async function generateVideoOgImage(videoId: Video.VideoId) {
const [bucket] = yield* Storage.getAccessForVideo(
decodeStorageVideo(video),
);
+ const publishedThumbnail = getPublishedRecordingThumbnailKey(video);
+ if (publishedThumbnail) {
+ screenshotUrl = yield* bucket.getSignedObjectUrl(publishedThumbnail);
+ return;
+ }
const listResponse = yield* bucket.listObjects({
prefix: `${video.ownerId}/${video.id}/`,
});
diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts
index 2442530d5..5159960a7 100644
--- a/apps/web/app/api/mobile/[...route]/route.ts
+++ b/apps/web/app/api/mobile/[...route]/route.ts
@@ -17,6 +17,7 @@ import {
Videos,
VideosRepo,
} from "@cap/web-backend";
+import { getPublishedRecordingThumbnailKey } from "@cap/web-backend/src/Storage/recording-output";
import {
Comment,
CurrentUser,
@@ -389,6 +390,10 @@ const getMobileThumbnailUrl = Effect.fn("Mobile.getThumbnailUrl")(function* (
const [video] = maybeVideo.value;
const [bucket] = yield* storage.getAccessForVideo(video);
+ const publishedThumbnail = getPublishedRecordingThumbnailKey(video);
+ if (publishedThumbnail) {
+ return yield* bucket.getSignedObjectUrl(publishedThumbnail);
+ }
const response = yield* bucket.listObjects({
prefix: `${video.ownerId}/${video.id}/`,
});
diff --git a/apps/web/app/api/thumbnail/route.ts b/apps/web/app/api/thumbnail/route.ts
index 927f9f166..6778a1984 100644
--- a/apps/web/app/api/thumbnail/route.ts
+++ b/apps/web/app/api/thumbnail/route.ts
@@ -6,6 +6,7 @@ import {
Storage,
VideosPolicy,
} from "@cap/web-backend";
+import { getPublishedRecordingThumbnailKey } from "@cap/web-backend/src/Storage/recording-output";
import { Policy, Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect, Exit } from "effect";
@@ -71,12 +72,14 @@ export async function GET(request: NextRequest) {
try {
const [bucket] = await Storage.getAccessForVideo(video).pipe(runPromise);
- const listResponse = await bucket
- .listObjects({ prefix: prefix })
- .pipe(runPromise);
- const contents = listResponse.Contents || [];
-
- const thumbnailKey = findScreenshotObjectKey(contents);
+ let thumbnailKey = getPublishedRecordingThumbnailKey(video);
+ if (!thumbnailKey) {
+ const listResponse = await bucket
+ .listObjects({ prefix })
+ .pipe(runPromise);
+ thumbnailKey =
+ findScreenshotObjectKey(listResponse.Contents || []) ?? undefined;
+ }
if (!thumbnailKey)
return new Response(
diff --git a/packages/web-backend/src/Storage/recording-output.ts b/packages/web-backend/src/Storage/recording-output.ts
index 6258f7a83..7bf74fc5c 100644
--- a/packages/web-backend/src/Storage/recording-output.ts
+++ b/packages/web-backend/src/Storage/recording-output.ts
@@ -56,3 +56,9 @@ export function getPublishedRecordingCopyKeys(video: RecordingVideo) {
.filter((key) => resolveRecordingObjectKey(video, key) !== key),
];
}
+
+export function getPublishedRecordingThumbnailKey(video: RecordingVideo) {
+ const canonical = `${video.ownerId}/${video.id}/screenshot/screen-capture.jpg`;
+ const resolved = resolveRecordingObjectKey(video, canonical);
+ return resolved === canonical ? undefined : resolved;
+}
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index c5e8351a4..bc99c10ab 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -17,6 +17,7 @@ import { Database } from "../Database.ts";
import { Storage as StorageService } from "../Storage/index.ts";
import {
getPublishedRecordingCopyKeys,
+ getPublishedRecordingThumbnailKey,
isInternalRecordingKey,
} from "../Storage/recording-output.ts";
import { Tinybird } from "../Tinybird/index.ts";
@@ -781,6 +782,12 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
const [video] = maybeVideo.value;
const [bucket] = yield* storage.getAccessForVideo(video);
+ const publishedThumbnail = getPublishedRecordingThumbnailKey(video);
+ if (publishedThumbnail) {
+ return Option.some(
+ yield* bucket.getSignedObjectUrl(publishedThumbnail),
+ );
+ }
const listResponse = yield* bucket.listObjects({
prefix: `${video.ownerId}/${video.id}/`,
});