Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web-backend): add findScreenshotObjectKey helper

Richie McIlroyd46215feca4a
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index be3fae1ba..64482e7f2 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -19,6 +19,58 @@ const formatDate = (date: Date) => date.toISOString().slice(0, 10);
const formatDateTime = (date: Date) =>
date.toISOString().slice(0, 19).replace("T", " ");
const buildPathname = (videoId: Video.VideoId) => `/s/${videoId}`;
+const SCREENSHOT_OBJECT_KEY_SUFFIXES = [
+ "screenshot/screen-capture.png",
+ "screenshot/screen-capture.jpg",
+ "screenshot/screen-capture.jpeg",
+ "screen-capture.jpg",
+ "screen-capture.jpeg",
+];
+type ScreenshotObject = {
+ Key?: string | null;
+ LastModified?: Date | string | number | null;
+};
+type ScreenshotCandidate = {
+ key: string;
+ suffixIndex: number;
+ lastModified: number | null;
+};
+const getScreenshotObjectTime = (value: ScreenshotObject["LastModified"]) => {
+ if (value == null) return null;
+ const time =
+ value instanceof Date ? value.getTime() : new Date(value).getTime();
+ return Number.isFinite(time) ? time : null;
+};
+export const findScreenshotObjectKey = (
+ contents: ReadonlyArray<ScreenshotObject>,
+) => {
+ const candidates = contents
+ .map((item): ScreenshotCandidate | null => {
+ const key = item.Key;
+ if (!key) return null;
+ const suffixIndex = SCREENSHOT_OBJECT_KEY_SUFFIXES.findIndex((suffix) =>
+ key.endsWith(suffix),
+ );
+ if (suffixIndex < 0) return null;
+ return {
+ key,
+ suffixIndex,
+ lastModified: getScreenshotObjectTime(item.LastModified),
+ };
+ })
+ .filter((item): item is ScreenshotCandidate => item !== null)
+ .sort((a, b) => {
+ if (a.lastModified !== null || b.lastModified !== null) {
+ const timeDiff =
+ (b.lastModified ?? Number.NEGATIVE_INFINITY) -
+ (a.lastModified ?? Number.NEGATIVE_INFINITY);
+ if (timeDiff !== 0) return timeDiff;
+ }
+ return a.suffixIndex - b.suffixIndex;
+ });
+
+ return candidates[0]?.key ?? null;
+};
const getFileExtensionFromKey = (fileKey: string) => {
const fileName = fileKey.split("/").at(-1) ?? "";
const extension = fileName.split(".").at(-1)?.toLowerCase();
@@ -506,6 +558,29 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
const [video] = maybeVideo.value;
const [bucket] = yield* storage.getAccessForVideo(video);
+ const [videoRow] = yield* db.use((db) =>
+ db
+ .select({ isScreenshot: Db.videos.isScreenshot })
+ .from(Db.videos)
+ .where(Dz.eq(Db.videos.id, videoId)),
+ );
+
+ if (videoRow?.isScreenshot) {
+ const listResponse = yield* bucket.listObjects({
+ prefix: `${video.ownerId}/${video.id}/`,
+ });
+ const screenshotKey = findScreenshotObjectKey(
+ listResponse.Contents || [],
+ );
+ if (!screenshotKey) return Option.none();
+ const extension = getFileExtensionFromKey(screenshotKey) ?? "jpg";
+ const downloadUrl = yield* bucket.getSignedObjectUrl(screenshotKey);
+ return Option.some({
+ fileName: `${video.name}.${extension}`,
+ downloadUrl,
+ });
+ }
+
const src = Video.Video.getSource(video);
if (src instanceof Video.Mp4Source && video.source.type === "webMP4") {
@@ -569,9 +644,7 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
prefix: `${video.ownerId}/${video.id}/`,
});
const contents = listResponse.Contents || [];
- const thumbnailKey = contents.find((item) =>
- item.Key?.endsWith("screen-capture.jpg"),
- )?.Key;
+ const thumbnailKey = findScreenshotObjectKey(contents);
if (!thumbnailKey) return Option.none();
const url = yield* bucket.getSignedObjectUrl(thumbnailKey);
return Option.some(url);
diff --git a/packages/web-backend/src/index.ts b/packages/web-backend/src/index.ts
index 7cd60bc8f..87263a4a8 100644
--- a/packages/web-backend/src/index.ts
+++ b/packages/web-backend/src/index.ts
@@ -35,7 +35,7 @@ export {
type ViewerSettingKey,
type ViewerSettings,
} from "./Videos/EffectiveVideoRules.ts";
-export { Videos } from "./Videos/index.ts";
+export { findScreenshotObjectKey, Videos } from "./Videos/index.ts";
export {
buildCanView,
VideosPolicy,