@@ -3,6 +3,7 @@ import { AbstractUploader, CommonOptions, DEFAULT_CHUNK_SIZE, MAX_CHUNK_SIZE, MI
3
3
interface UploadOptions {
4
4
file : File ;
5
5
chunkSize ?: number ;
6
+ maxVideoDuration ?: number ;
6
7
}
7
8
8
9
export interface VideoUploaderOptionsWithUploadToken extends CommonOptions , UploadOptions , WithUploadToken { }
@@ -24,6 +25,7 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
24
25
private chunksCount : number ;
25
26
private fileSize : number ;
26
27
private fileName : string ;
28
+ private maxVideoDuration ?: number ;
27
29
28
30
constructor ( options : VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken | VideoUploaderOptionsWithApiKey ) {
29
31
super ( options ) ;
@@ -42,10 +44,21 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
42
44
this . fileName = options . videoName || this . file . name ;
43
45
44
46
this . chunksCount = Math . ceil ( this . fileSize / this . chunkSize ) ;
47
+ this . maxVideoDuration = options . maxVideoDuration ;
45
48
}
46
49
47
50
48
51
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
+ }
49
62
let res : VideoUploadResponse ;
50
63
for ( let i = 0 ; i < this . chunksCount ; i ++ ) {
51
64
res = await this . uploadCurrentChunk ( i ) ;
@@ -54,6 +67,18 @@ export class VideoUploader extends AbstractUploader<UploadProgressEvent> {
54
67
return res ! ;
55
68
}
56
69
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
+
57
82
private uploadCurrentChunk ( chunkNumber : number ) : Promise < VideoUploadResponse > {
58
83
const firstByte = chunkNumber * this . chunkSize ;
59
84
const computedLastByte = ( chunkNumber + 1 ) * this . chunkSize ;
0 commit comments