Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apps/media-server/src/__tests__/lib/loom-import-limit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, test } from "bun:test";
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import { execFileSync } from "node:child_process";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
Expand Down Expand Up @@ -69,6 +69,29 @@ describe("Loom import file size", () => {
expect(cancelled).toBe(true);
});

test.each([
["bounded Loom downloads", 16, 30 * 60 * 1000],
["ordinary downloads", undefined, 10 * 60 * 1000],
])("keeps the expected timeout for %s", async (_, maxBytes, timeoutMs) => {
const timeoutSpy = spyOn(AbortSignal, "timeout");
stubFetch(async () => new Response(new Uint8Array(16)));
try {
const result = await downloadVideoToTemp(
"https://source.test/video.mp4",
".mp4",
undefined,
maxBytes,
);
try {
expect(timeoutSpy).toHaveBeenCalledWith(timeoutMs);
} finally {
await result.cleanup();
}
} finally {
timeoutSpy.mockRestore();
}
});

test("enforces the import limit when parallel downloads are enabled", async () => {
process.env.MEDIA_SERVER_DOWNLOAD_CONCURRENCY = "4";
stubFetch(async () => new Response(new Uint8Array(17)));
Expand Down
8 changes: 5 additions & 3 deletions apps/media-server/src/lib/media-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const MAX_PROCESS_TIMEOUT_MS = 2 * 60 * 60 * 1000;
// flat 10-minute budget is enough for typical short recordings but not for a
// long (e.g. 60+ minute) manifest with 1000+ segments.
const STREAMING_DOWNLOAD_TIMEOUT_PER_SECOND_MS = 5_000;
const MAX_STREAMING_DOWNLOAD_TIMEOUT_MS = 30 * 60 * 1000;
const MAX_DOWNLOAD_TIMEOUT_MS = 30 * 60 * 1000;
const THUMBNAIL_TIMEOUT_MS = 60_000;
const PREVIEW_GIF_TIMEOUT_MS = 30_000;
const PROBE_H264_LEVEL_TIMEOUT_MS = 10_000;
Expand Down Expand Up @@ -1196,7 +1196,7 @@ function getStreamingDownloadTimeoutMs(durationSeconds: number | null): number {
if (!durationSeconds) return DOWNLOAD_TIMEOUT_MS;

return Math.min(
MAX_STREAMING_DOWNLOAD_TIMEOUT_MS,
MAX_DOWNLOAD_TIMEOUT_MS,
Math.max(
DOWNLOAD_TIMEOUT_MS,
Math.ceil(durationSeconds * STREAMING_DOWNLOAD_TIMEOUT_PER_SECOND_MS),
Expand Down Expand Up @@ -1294,7 +1294,9 @@ export async function downloadVideoToTemp(
);

try {
const timeoutSignal = AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS);
const timeoutSignal = AbortSignal.timeout(
maxBytes === undefined ? DOWNLOAD_TIMEOUT_MS : MAX_DOWNLOAD_TIMEOUT_MS,
);
const combinedSignal = abortSignal
? AbortSignal.any([abortSignal, timeoutSignal])
: timeoutSignal;
Expand Down
43 changes: 43 additions & 0 deletions apps/web/__tests__/unit/loom-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,49 @@ describe("importFromLoom", () => {
expect(valuesMock).not.toHaveBeenCalled();
});

it.each([
["long titles", "a".repeat(383), "a".repeat(255)],
["Unicode titles", "🎥".repeat(260), "🎥".repeat(255)],
["short titles", "Imported 🎥 video", "Imported 🎥 video"],
])(
"fits %s into the destination without changing the import",
async (_, title, expected) => {
valuesMock.mockImplementation(async (value: { name?: string }) => {
if (value.name && Array.from(value.name).length > 255) {
throw new Error("Data too long for column name");
}
});
vi.mocked(fetch).mockImplementation(async (input) => {
const url = input.toString();
if (url.includes("/transcoded-url")) {
return Response.json({ url: "https://cdn.loom.com/video.mp4" });
}
if (url === "https://www.loom.com/graphql") {
return Response.json({ data: { getVideo: { name: title } } });
}
if (url.includes("/v1/oembed")) {
return Response.json({ duration: 42, width: 1920, height: 1080 });
}
throw new Error(`Unexpected fetch: ${url}`);
});
const { importFromLoom } = await import("@/actions/loom");
expect(
await importFromLoom({
loomUrl: "https://www.loom.com/share/loom-abc1234567",
orgId: "org-1" as Organisation.OrganisationId,
}),
).toEqual({ success: true, videoId: "video-123" });
expect(valuesMock).toHaveBeenCalledWith(
expect.objectContaining({
name: expected,
ownerId: "user-123",
orgId: "org-1",
}),
);
expect(startMock).toHaveBeenCalledTimes(1);
},
);

it("does not call the Vercel firewall from the Loom import server action", async () => {
checkRateLimitMock.mockResolvedValueOnce({ rateLimited: true });

Expand Down
8 changes: 6 additions & 2 deletions apps/web/actions/loom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface LoomCsvImportResult {

const MAX_LOOM_CSV_ROWS = 500;
const MAX_LOOM_SPACE_NAME_LENGTH = 255;
const MAX_LOOM_VIDEO_NAME_LENGTH = 255;
const LOOM_CSV_LIMIT_ERROR = `CSV imports are limited to ${MAX_LOOM_CSV_ROWS} rows at a time. Contact support to raise this limit.`;
const LOOM_CSV_PERMISSION_ERROR =
"Only organization admins and owners can import Loom videos from a CSV.";
Expand Down Expand Up @@ -526,9 +527,12 @@ async function importLoomVideoForOwner({
const writable = writableResult.value;

const videoId = Video.VideoId.make(nanoId());
const name =
const name = Array.from(
videoName ||
`Loom Import - ${new Date().toLocaleDateString("en-US", { day: "numeric", month: "long", year: "numeric" })}`;
`Loom Import - ${new Date().toLocaleDateString("en-US", { day: "numeric", month: "long", year: "numeric" })}`,
)
.slice(0, MAX_LOOM_VIDEO_NAME_LENGTH)
.join("");

const claimedAt = new Date(Math.floor(Date.now() / 1000) * 1000);
await db().transaction(async (tx) => {
Expand Down
Loading