Skip to content

Commit d58e9a8

Browse files
committed
Support webMP4 downloads and file extensions
1 parent 2afac1f commit d58e9a8

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

packages/web-backend/src/Videos/index.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ const formatDate = (date: Date) => date.toISOString().slice(0, 10);
1919
const formatDateTime = (date: Date) =>
2020
date.toISOString().slice(0, 19).replace("T", " ");
2121
const buildPathname = (videoId: Video.VideoId) => `/s/${videoId}`;
22+
const getFileExtensionFromKey = (fileKey: string) => {
23+
const fileName = fileKey.split("/").at(-1) ?? "";
24+
const extension = fileName.split(".").at(-1)?.toLowerCase();
25+
26+
if (!extension || extension === fileName.toLowerCase()) {
27+
return null;
28+
}
29+
30+
return extension;
31+
};
2232

2333
type UploadProgressUpdateInput = Schema.Type<
2434
typeof Video.UploadProgressUpdateInput
@@ -479,8 +489,48 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
479489
const [video] = maybeVideo.value;
480490

481491
const [bucket] = yield* s3Buckets.getBucketAccess(video.bucketId);
482-
483492
const src = Video.Video.getSource(video);
493+
494+
if (src instanceof Video.Mp4Source && video.source.type === "webMP4") {
495+
const mp4Head = yield* bucket
496+
.headObject(src.getFileKey())
497+
.pipe(Effect.option);
498+
499+
if (
500+
Option.isSome(mp4Head) &&
501+
(mp4Head.value.ContentLength ?? 0) > 0
502+
) {
503+
const downloadUrl = yield* bucket.getSignedObjectUrl(
504+
src.getFileKey(),
505+
);
506+
507+
return Option.some({
508+
fileName: `${video.name}.mp4`,
509+
downloadUrl,
510+
});
511+
}
512+
513+
const [upload] = yield* db.use((db) =>
514+
db
515+
.select({ rawFileKey: Db.videoUploads.rawFileKey })
516+
.from(Db.videoUploads)
517+
.where(Dz.eq(Db.videoUploads.videoId, video.id)),
518+
);
519+
520+
if (upload?.rawFileKey) {
521+
const downloadUrl = yield* bucket.getSignedObjectUrl(
522+
upload.rawFileKey,
523+
);
524+
const extension =
525+
getFileExtensionFromKey(upload.rawFileKey) ?? "mp4";
526+
527+
return Option.some({
528+
fileName: `${video.name}.${extension}`,
529+
downloadUrl,
530+
});
531+
}
532+
}
533+
484534
if (!src) return Option.none();
485535
if (!(src instanceof Video.Mp4Source)) return Option.none();
486536

0 commit comments

Comments
 (0)