Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: resolve validated audio derivatives while retaining originals

Richie McIlroy3627391a8d9c
diff --git a/apps/web/__tests__/unit/storage-object-verification.test.ts b/apps/web/__tests__/unit/storage-object-verification.test.ts
index 2f1320222..ff9cc66b1 100644
--- a/apps/web/__tests__/unit/storage-object-verification.test.ts
+++ b/apps/web/__tests__/unit/storage-object-verification.test.ts
@@ -15,6 +15,8 @@ const mocks = vi.hoisted(() => ({
source: { type: "desktopMP4" } as {
type: string;
outputKey?: string;
+ audioLevelSourceKey?: string;
+ audioLevelOutputKey?: string;
thumbnailKey?: string;
previewKey?: string;
},
@@ -327,6 +329,21 @@ describe("recording verification object reads", () => {
expect(mocks.read).not.toHaveBeenCalled();
});
+ it("allows only an audio derivative bound to the current original", async () => {
+ const source = "owner/video/.recording/outputs/generation/original.mp4";
+ const key = "owner/video/.recording/outputs/audio-quality-v3/published.mp4";
+ mocks.token = null;
+ mocks.video.source = {
+ type: "desktopMP4",
+ outputKey: source,
+ audioLevelSourceKey: source,
+ audioLevelOutputKey: key,
+ };
+ expect((await request({}, key)).status).toBe(206);
+ mocks.video.source.audioLevelSourceKey = "stale";
+ expect((await request({}, key)).status).toBe(404);
+ });
+
it.each(["outputKey", "thumbnailKey", "previewKey"] as const)(
"allows viewers to read only the published %s",
async (field) => {
diff --git a/apps/web/app/api/storage/object/route.ts b/apps/web/app/api/storage/object/route.ts
index 184b774f9..4543ddb7d 100644
--- a/apps/web/app/api/storage/object/route.ts
+++ b/apps/web/app/api/storage/object/route.ts
@@ -168,6 +168,7 @@ export async function GET(request: NextRequest) {
video.source.type === "desktopMP4" &&
[
key === video.source.outputKey,
+ key === Video.getAudioLevelOutputKey(video),
key === video.source.thumbnailKey,
key === video.source.previewKey,
].some(Boolean)
diff --git a/packages/database/schema.ts b/packages/database/schema.ts
index e54a4d60d..41cbc3c9e 100644
--- a/packages/database/schema.ts
+++ b/packages/database/schema.ts
@@ -427,6 +427,8 @@ export const videos = mysqlTable(
| {
type: "desktopMP4";
outputKey?: string;
+ audioLevelOutputKey?: string;
+ audioLevelSourceKey?: string;
thumbnailKey?: string;
previewKey?: string;
}
diff --git a/packages/web-backend/src/Storage/recording-output.ts b/packages/web-backend/src/Storage/recording-output.ts
index fb91708aa..4d2ad803f 100644
--- a/packages/web-backend/src/Storage/recording-output.ts
+++ b/packages/web-backend/src/Storage/recording-output.ts
@@ -6,6 +6,8 @@ type RecordingVideo = {
source: {
type: string;
outputKey?: string;
+ audioLevelSourceKey?: string;
+ audioLevelOutputKey?: string;
thumbnailKey?: string;
previewKey?: string;
};
@@ -42,7 +44,9 @@ export function resolveRecordingObjectKey(video: RecordingVideo, key: string) {
return asset;
}
return key === `${video.ownerId}/${video.id}/result.mp4`
- ? (getPublishedRecordingOutputKey(video) ?? key)
+ ? (Video.getAudioLevelOutputKey(video) ??
+ getPublishedRecordingOutputKey(video) ??
+ key)
: key;
}
diff --git a/packages/web-domain/src/Video.ts b/packages/web-domain/src/Video.ts
index e6f6e0fb8..62dd8c772 100644
--- a/packages/web-domain/src/Video.ts
+++ b/packages/web-domain/src/Video.ts
@@ -32,6 +32,8 @@ export class Video extends Schema.Class<Video>("Video")({
public: Schema.Boolean,
source: Schema.Struct({
outputKey: Schema.optional(Schema.String),
+ audioLevelOutputKey: Schema.optional(Schema.String),
+ audioLevelSourceKey: Schema.optional(Schema.String),
thumbnailKey: Schema.optional(Schema.String),
previewKey: Schema.optional(Schema.String),
type: Schema.Literal(
@@ -81,7 +83,7 @@ export class Video extends Schema.Class<Video>("Video")({
return new Mp4Source({
videoId: self.id,
ownerId: self.ownerId,
- outputKey: self.source.outputKey,
+ outputKey: getAudioLevelOutputKey(self) ?? self.source.outputKey,
});
}
}
@@ -161,6 +163,34 @@ export function getRetainedRecordingOutputKey(
return key;
}
+export function getAudioLevelOutputKey(video: {
+ id: string;
+ ownerId: string;
+ source: {
+ type: string;
+ outputKey?: string;
+ audioLevelSourceKey?: string;
+ audioLevelOutputKey?: string;
+ };
+}) {
+ const { source } = video;
+ const key = getRetainedRecordingOutputKey(
+ video.ownerId,
+ video.id,
+ source.audioLevelOutputKey,
+ );
+ if (
+ source.type !== "desktopMP4" ||
+ !getRetainedRecordingOutputKey(video.ownerId, video.id, source.outputKey) ||
+ source.audioLevelSourceKey !== source.outputKey ||
+ !key?.startsWith(
+ `${video.ownerId}/${video.id}/.recording/outputs/audio-quality-v3/`,
+ )
+ )
+ return undefined;
+ return key;
+}
+
export class Mp4Source extends Schema.TaggedClass<Mp4Source>()("Mp4Source", {
videoId: Schema.String,
ownerId: Schema.String,