Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(media-server): fail imported originals whose recovered output is incomplete

Richie McIlroy73089a70fd10
diff --git a/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts b/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts
index 93a90f66a..1ed4b1930 100644
--- a/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts
@@ -30,6 +30,7 @@ describe("raw recording input validation", () => {
const corrupt = join(directory, "corrupt.webm");
const cleanIvf = join(directory, "clean.ivf");
const cleanMp4 = join(directory, "clean.mp4");
+ const truncated = join(directory, "truncated.webm");
beforeAll(() => {
execFileSync("ffmpeg", [
@@ -90,6 +91,11 @@ describe("raw recording input validation", () => {
output,
]);
}
+ const cleanBytes = readFileSync(clean);
+ writeFileSync(
+ truncated,
+ cleanBytes.subarray(0, Math.floor(cleanBytes.length / 2)),
+ );
execFileSync("ffmpeg", [
"-v",
"error",
@@ -119,11 +125,26 @@ describe("raw recording input validation", () => {
expect(readFileSync(corrupt).length).toBeGreaterThan(0);
});
- for (const [name, source, expectedPhase, extension, importedOriginal] of [
- ["clean", clean, "complete", ".webm", false],
- ["damaged", corrupt, "error", ".webm", false],
- ["imported-damaged", corrupt, "complete", ".webm", true],
- ["fragmented-mp4", cleanMp4, "complete", ".mp4", false],
+ for (const [
+ name,
+ source,
+ expectedPhase,
+ extension,
+ importedOriginal,
+ expectedError,
+ ] of [
+ ["clean", clean, "complete", ".webm", false, null],
+ ["damaged", corrupt, "error", ".webm", false, "damaged or unreadable"],
+ ["imported-damaged", corrupt, "complete", ".webm", true, null],
+ [
+ "imported-truncated",
+ truncated,
+ "error",
+ ".webm",
+ true,
+ "could not be fully recovered",
+ ],
+ ["fragmented-mp4", cleanMp4, "complete", ".mp4", false, null],
] as const) {
test(`processing route handles ${name} source before conversion`, async () => {
spyOn(jobManager, "canAcceptNewVideoProcess").mockReturnValue(true);
@@ -171,9 +192,9 @@ describe("raw recording input validation", () => {
await Bun.sleep(10);
}
expect(getJob(jobId)?.phase).toBe(expectedPhase);
- if (expectedPhase === "error") {
- expect(getJob(jobId)?.error).toContain("damaged or unreadable");
- expect(process).not.toHaveBeenCalled();
+ if (expectedError) {
+ expect(getJob(jobId)?.error).toContain(expectedError);
+ expect(process).toHaveBeenCalledTimes(importedOriginal ? 1 : 0);
expect(upload).not.toHaveBeenCalled();
} else {
expect(process).toHaveBeenCalledTimes(1);
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index de7585238..baf27cac4 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -197,6 +197,13 @@ const processSchema = z.object({
importedOriginal: z.boolean().optional(),
});
+export function isCompleteRecovery(
+ outputDuration: number,
+ inputDuration: number,
+): boolean {
+ return outputDuration >= inputDuration - Math.max(1, inputDuration * 0.01);
+}
+
const editRangeSchema = z
.object({
start: z.number().min(0),
@@ -1644,6 +1651,15 @@ async function processVideoAsync(
lastResortRepairFile = processed.lastResortRepairFile;
const outputPath = processed.outputFile.path;
updateJob(jobId, { outputTempFile: processed.outputFile });
+ if (options.importedOriginal) {
+ const recovered = await withJobHeartbeat(jobId, () =>
+ probeVideoFile(outputPath),
+ );
+ if (!isCompleteRecovery(recovered.duration, metadata.duration))
+ throw new Error(
+ "The imported original could not be fully recovered. The original has been preserved.",
+ );
+ }
if (
options.maxImportBytes &&
file(outputPath).size > options.maxImportBytes