Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: preserve browser audio derivatives across storage operations

Richie McIlroybb51e84c8269
diff --git a/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts b/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts
index c15c16203..6af2ded70 100644
--- a/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts
@@ -89,6 +89,8 @@ let video: {
outputKey?: string;
thumbnailKey?: string;
previewKey?: string;
+ audioLevelOutputKey?: string;
+ audioLevelSourceKey?: string;
};
metadata: Record<string, unknown>;
};
@@ -219,6 +221,29 @@ beforeEach(() => {
afterEach(() => vi.unstubAllGlobals());
describe("edited recording publication", () => {
+ it.each(["edit", "reprocess", "replace"])(
+ "clears a browser audio derivative after %s",
+ async (operation) => {
+ video.source = {
+ type: "webMP4",
+ audioLevelSourceKey: "user/video/result.mp4",
+ audioLevelOutputKey:
+ "user/video/.recording/outputs/audio-quality-v3/test.mp4",
+ };
+ if (operation === "edit")
+ await saveEditResultAndComplete(
+ "video",
+ "user/video/edit-original.mp4",
+ editSpec,
+ editSpec,
+ metadata,
+ );
+ else if (operation === "reprocess")
+ await saveMetadataAndComplete("video", metadata);
+ else await invalidateVideoCache("video");
+ expect(video.source).toEqual({ type: "webMP4" });
+ },
+ );
it("verifies the newly rendered canonical output instead of the old published immutable recording", async () => {
await verifyRenderedEditOutput("video", "user", editSpec, metadata);
expect(mocks.access).toHaveBeenCalledWith(video, {
diff --git a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
index 7cba42a54..76a118a90 100644
--- a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
+++ b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
@@ -898,6 +898,27 @@ describe("recording storage lifecycle", () => {
},
]);
});
+ it("duplicates enhanced browser playback without retaining foreign audio pointers", async () => {
+ const audioKey = `${prefix}.recording/outputs/audio-quality-v3/selected.mp4`;
+ const database = databaseFixture(
+ recording({
+ type: "webMP4",
+ audioLevelSourceKey: `${prefix}result.mp4`,
+ audioLevelOutputKey: audioKey,
+ }),
+ );
+ const storage = await storageFixture([
+ [audioKey, "enhanced"],
+ [`${prefix}result.mp4`, "original"],
+ ]);
+ const result = await runVideoOperation("duplicate");
+ expect(Exit.isSuccess(result)).toBe(true);
+ expect(storage.objects.get(`${newPrefix}result.mp4`)).toBe("enhanced");
+ expect(database.rows.get("duplicate-video")?.source).toEqual({
+ type: "webMP4",
+ });
+ expect(storage.objects.get(`${prefix}result.mp4`)).toBe("original");
+ });
it("does not duplicate source inventories, raw fragments, or comment attachments across pages", async () => {
const database = databaseFixture();
diff --git a/apps/web/__tests__/unit/storage-object-verification.test.ts b/apps/web/__tests__/unit/storage-object-verification.test.ts
index ff9cc66b1..733073402 100644
--- a/apps/web/__tests__/unit/storage-object-verification.test.ts
+++ b/apps/web/__tests__/unit/storage-object-verification.test.ts
@@ -329,20 +329,27 @@ 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(["desktopMP4", "webMP4"])(
+ "allows only a %s audio derivative bound to the current original",
+ async (type) => {
+ const source =
+ type === "webMP4"
+ ? "owner/video/result.mp4"
+ : "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,
+ 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",
diff --git a/apps/web/actions/admin/replace-video.ts b/apps/web/actions/admin/replace-video.ts
index 46d68589f..0a6879c9c 100644
--- a/apps/web/actions/admin/replace-video.ts
+++ b/apps/web/actions/admin/replace-video.ts
@@ -94,7 +94,7 @@ export async function invalidateVideoCache(videoId: string) {
metadata,
source:
lockedVideo.source.type === "webMP4"
- ? lockedVideo.source
+ ? { type: "webMP4" }
: { type: "desktopMP4" },
})
.where(eq(videos.id, video.id));
diff --git a/apps/web/app/api/storage/object/route.ts b/apps/web/app/api/storage/object/route.ts
index 4543ddb7d..8572f092a 100644
--- a/apps/web/app/api/storage/object/route.ts
+++ b/apps/web/app/api/storage/object/route.ts
@@ -165,13 +165,13 @@ export async function GET(request: NextRequest) {
isInternalRecordingKey(key) &&
!(tokenPayload?.videoId === videoIdParam && tokenPayload.key === key) &&
!(
- video.source.type === "desktopMP4" &&
- [
- key === video.source.outputKey,
- key === Video.getAudioLevelOutputKey(video),
- key === video.source.thumbnailKey,
- key === video.source.previewKey,
- ].some(Boolean)
+ key === Video.getAudioLevelOutputKey(video) ||
+ (video.source.type === "desktopMP4" &&
+ [
+ key === video.source.outputKey,
+ key === video.source.thumbnailKey,
+ key === video.source.previewKey,
+ ].some(Boolean))
)
) {
return yield* Effect.fail("not-found" as const);
diff --git a/apps/web/workflows/admin-reprocess-video.ts b/apps/web/workflows/admin-reprocess-video.ts
index 73d8fa860..8baa0d6d0 100644
--- a/apps/web/workflows/admin-reprocess-video.ts
+++ b/apps/web/workflows/admin-reprocess-video.ts
@@ -425,8 +425,9 @@ export async function saveMetadataAndComplete(
height: metadata.height,
fps: metadata.fps,
metadata: nextMetadata,
- ...(lockedVideo.source.type === "desktopMP4"
- ? { source: { type: "desktopMP4" as const } }
+ ...(lockedVideo.source.type === "desktopMP4" ||
+ lockedVideo.source.type === "webMP4"
+ ? { source: { type: lockedVideo.source.type } }
: {}),
...(duration === undefined ? {} : { duration }),
})
diff --git a/apps/web/workflows/edit-video.ts b/apps/web/workflows/edit-video.ts
index 37a1df012..6bc9e5241 100644
--- a/apps/web/workflows/edit-video.ts
+++ b/apps/web/workflows/edit-video.ts
@@ -871,8 +871,9 @@ export async function saveEditResultAndComplete(
height: metadata.height,
fps: metadata.fps,
metadata: nextMetadata,
- ...(lockedVideo.source.type === "desktopMP4"
- ? { source: { type: "desktopMP4" as const } }
+ ...(lockedVideo.source.type === "desktopMP4" ||
+ lockedVideo.source.type === "webMP4"
+ ? { source: { type: lockedVideo.source.type } }
: {}),
// Derivable captions keep the transcription COMPLETE; only legacy
// videos without a stored word transcript get re-transcribed.
diff --git a/packages/database/schema.ts b/packages/database/schema.ts
index 41cbc3c9e..443f1149d 100644
--- a/packages/database/schema.ts
+++ b/packages/database/schema.ts
@@ -433,7 +433,11 @@ export const videos = mysqlTable(
previewKey?: string;
}
| { type: "desktopSegments" }
- | { type: "webMP4" }
+ | {
+ type: "webMP4";
+ audioLevelOutputKey?: string;
+ audioLevelSourceKey?: string;
+ }
>()
.notNull()
.default({ type: "MediaConvert" }),
diff --git a/packages/web-backend/src/Storage/recording-output.ts b/packages/web-backend/src/Storage/recording-output.ts
index 4d2ad803f..8f73c114c 100644
--- a/packages/web-backend/src/Storage/recording-output.ts
+++ b/packages/web-backend/src/Storage/recording-output.ts
@@ -51,7 +51,11 @@ export function resolveRecordingObjectKey(video: RecordingVideo, key: string) {
}
export function getPublishedRecordingCopyKeys(video: RecordingVideo) {
- if (!getPublishedRecordingOutputKey(video)) return [];
+ if (
+ !getPublishedRecordingOutputKey(video) &&
+ !Video.getAudioLevelOutputKey(video)
+ )
+ return [];
const prefix = `${video.ownerId}/${video.id}/`;
return [
`${prefix}result.mp4`,
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index c5e8351a4..1c95bfda4 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -414,7 +414,14 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
{
...video,
source:
- publishedKeys.size > 0 ? { type: "desktopMP4" } : video.source,
+ publishedKeys.size > 0
+ ? {
+ type:
+ video.source.type === "webMP4"
+ ? "webMP4"
+ : "desktopMP4",
+ }
+ : video.source,
metadata: Option.map(video.metadata, (metadata) => {
const copied = { ...metadata };
delete copied.desktopRecordingUpload;
diff --git a/packages/web-domain/src/Video.ts b/packages/web-domain/src/Video.ts
index 62dd8c772..0ddb4d568 100644
--- a/packages/web-domain/src/Video.ts
+++ b/packages/web-domain/src/Video.ts
@@ -174,15 +174,23 @@ export function getAudioLevelOutputKey(video: {
};
}) {
const { source } = video;
+ const originalKey =
+ source.type === "webMP4"
+ ? `${video.ownerId}/${video.id}/result.mp4`
+ : getRetainedRecordingOutputKey(
+ video.ownerId,
+ video.id,
+ source.outputKey,
+ );
const key = getRetainedRecordingOutputKey(
video.ownerId,
video.id,
source.audioLevelOutputKey,
);
if (
- source.type !== "desktopMP4" ||
- !getRetainedRecordingOutputKey(video.ownerId, video.id, source.outputKey) ||
- source.audioLevelSourceKey !== source.outputKey ||
+ !["desktopMP4", "webMP4"].includes(source.type) ||
+ !originalKey ||
+ source.audioLevelSourceKey !== originalKey ||
!key?.startsWith(
`${video.ownerId}/${video.id}/.recording/outputs/audio-quality-v3/`,
)