Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Support webMP4 downloads and file extensions

Richie McIlroyd58e9a819c29
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index 5906832b0..f58cf8d01 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -19,6 +19,16 @@ const formatDate = (date: Date) => date.toISOString().slice(0, 10);
const formatDateTime = (date: Date) =>
date.toISOString().slice(0, 19).replace("T", " ");
const buildPathname = (videoId: Video.VideoId) => `/s/${videoId}`;
+const getFileExtensionFromKey = (fileKey: string) => {
+ const fileName = fileKey.split("/").at(-1) ?? "";
+ const extension = fileName.split(".").at(-1)?.toLowerCase();
+
+ if (!extension || extension === fileName.toLowerCase()) {
+ return null;
+ }
+
+ return extension;
+};
type UploadProgressUpdateInput = Schema.Type<
typeof Video.UploadProgressUpdateInput
@@ -479,8 +489,48 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
const [video] = maybeVideo.value;
const [bucket] = yield* s3Buckets.getBucketAccess(video.bucketId);
-
const src = Video.Video.getSource(video);
+
+ if (src instanceof Video.Mp4Source && video.source.type === "webMP4") {
+ const mp4Head = yield* bucket
+ .headObject(src.getFileKey())
+ .pipe(Effect.option);
+
+ if (
+ Option.isSome(mp4Head) &&
+ (mp4Head.value.ContentLength ?? 0) > 0
+ ) {
+ const downloadUrl = yield* bucket.getSignedObjectUrl(
+ src.getFileKey(),
+ );
+
+ return Option.some({
+ fileName: `${video.name}.mp4`,
+ downloadUrl,
+ });
+ }
+
+ const [upload] = yield* db.use((db) =>
+ db
+ .select({ rawFileKey: Db.videoUploads.rawFileKey })
+ .from(Db.videoUploads)
+ .where(Dz.eq(Db.videoUploads.videoId, video.id)),
+ );
+
+ if (upload?.rawFileKey) {
+ const downloadUrl = yield* bucket.getSignedObjectUrl(
+ upload.rawFileKey,
+ );
+ const extension =
+ getFileExtensionFromKey(upload.rawFileKey) ?? "mp4";
+
+ return Option.some({
+ fileName: `${video.name}.${extension}`,
+ downloadUrl,
+ });
+ }
+ }
+
if (!src) return Option.none();
if (!(src instanceof Video.Mp4Source)) return Option.none();