Skip to content

Commit 4367d91

Browse files
committed
feat(video uploader): Add maxVideoDuration option
1 parent fdff6a2 commit 4367d91

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/video-uploader.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AbstractUploader, CommonOptions, DEFAULT_CHUNK_SIZE, MAX_CHUNK_SIZE, MI
33
interface UploadOptions {
44
file: File;
55
chunkSize?: number;
6+
maxVideoDuration?: number;
67
}
78

89
export interface VideoUploaderOptionsWithUploadToken extends CommonOptions, UploadOptions, WithUploadToken { }
@@ -24,6 +25,7 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
2425
private chunksCount: number;
2526
private fileSize: number;
2627
private fileName: string;
28+
private maxVideoDuration?: number;
2729

2830
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken | VideoUploaderOptionsWithApiKey) {
2931
super(options);
@@ -42,10 +44,21 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
4244
this.fileName = options.videoName || this.file.name;
4345

4446
this.chunksCount = Math.ceil(this.fileSize / this.chunkSize);
47+
this.maxVideoDuration = options.maxVideoDuration;
4548
}
4649

4750

4851
public async upload(): Promise<VideoUploadResponse> {
52+
if (this.maxVideoDuration !== null && !document) {
53+
throw Error('document is undefined. Impossible to use the maxVideoDuration option. Remove it and try again.')
54+
}
55+
let tooLong: boolean = false;
56+
if (this.maxVideoDuration !== null) {
57+
tooLong = await this.checkVideoDuration();
58+
}
59+
if (tooLong) {
60+
throw Error(`The video submitted is too long.`);
61+
}
4962
let res: VideoUploadResponse;
5063
for(let i = 0; i < this.chunksCount; i++) {
5164
res = await this.uploadCurrentChunk(i);
@@ -54,6 +67,18 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
5467
return res!;
5568
}
5669

70+
private async checkVideoDuration(): Promise<boolean> {
71+
return new Promise(resolve => {
72+
const video = document.createElement('video');
73+
video.preload = 'metadata';
74+
video.onloadedmetadata = () => {
75+
window.URL.revokeObjectURL(video.src);
76+
resolve(video.duration > this.maxVideoDuration!)
77+
}
78+
video.src = URL.createObjectURL(this.file);
79+
})
80+
}
81+
5782
private uploadCurrentChunk(chunkNumber: number): Promise<VideoUploadResponse> {
5883
const firstByte = chunkNumber * this.chunkSize;
5984
const computedLastByte = (chunkNumber + 1) * this.chunkSize;

0 commit comments

Comments
 (0)