Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add media file type utilities

Richie McIlroy9fcc41087110
diff --git a/apps/web/__tests__/unit/import-media-file-types.test.ts b/apps/web/__tests__/unit/import-media-file-types.test.ts
new file mode 100644
index 000000000..b03763d67
--- /dev/null
+++ b/apps/web/__tests__/unit/import-media-file-types.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from "vitest";
+import {
+ getSupportedImageContentType,
+ isSupportedMediaFile,
+ isSupportedVideoFile,
+} from "@/app/(org)/dashboard/import/media-file-types";
+
+const file = (name: string, type = "") => ({ name, type });
+
+describe("dashboard media import file matching", () => {
+ it("accepts videos from MIME type or filename extension", () => {
+ expect(isSupportedVideoFile(file("", "video/mp4"))).toBe(true);
+ expect(isSupportedVideoFile(file("clip.MOV"))).toBe(true);
+ expect(isSupportedVideoFile(file("clip.webm"))).toBe(true);
+ });
+
+ it("accepts JPEG and PNG images from MIME type or filename extension", () => {
+ expect(getSupportedImageContentType(file("", "image/png"))).toBe(
+ "image/png",
+ );
+ expect(getSupportedImageContentType(file("image.JPG"))).toBe("image/jpeg");
+ expect(getSupportedImageContentType(file("image.jpeg"))).toBe("image/jpeg");
+ });
+
+ it("rejects unsupported clipboard files", () => {
+ expect(isSupportedMediaFile(file("notes.txt", "text/plain"))).toBe(false);
+ expect(isSupportedMediaFile(file("image.gif", "image/gif"))).toBe(false);
+ expect(isSupportedMediaFile(file("archive.zip"))).toBe(false);
+ });
+});
diff --git a/apps/web/app/(org)/dashboard/import/media-file-types.ts b/apps/web/app/(org)/dashboard/import/media-file-types.ts
new file mode 100644
index 000000000..a4c1a213a
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/import/media-file-types.ts
@@ -0,0 +1,16 @@
+type MediaFileLike = Pick<File, "name" | "type">;
+
+export const isSupportedVideoFile = (file: MediaFileLike) =>
+ file.type.startsWith("video/") ||
+ /\.(mov|mp4|avi|mkv|webm|m4v)$/i.test(file.name);
+
+export const getSupportedImageContentType = (file: MediaFileLike) => {
+ if (file.type === "image/png") return "image/png";
+ if (file.type === "image/jpeg") return "image/jpeg";
+ if (/\.png$/i.test(file.name)) return "image/png";
+ if (/\.jpe?g$/i.test(file.name)) return "image/jpeg";
+ return null;
+};
+
+export const isSupportedMediaFile = (file: MediaFileLike) =>
+ getSupportedImageContentType(file) !== null || isSupportedVideoFile(file);