@@ -69,6 +69,7 @@ export class FeedCreatePostComponent implements OnInit {
69
69
postImageSrc = null ;
70
70
71
71
postVideoSrc = null ;
72
+ assetId = '' ;
72
73
videoUploadPercentage = null ;
73
74
74
75
showEmbedURL = false ;
@@ -325,64 +326,36 @@ export class FeedCreatePostComponent implements OnInit {
325
326
) ;
326
327
}
327
328
328
- uploadVideo ( file : File ) : void {
329
- if ( file . size > 4 * ( 1024 * 1024 * 1024 ) ) {
329
+ async uploadVideo ( file : File ) : Promise < void > {
330
+ if ( file . size > 65 * 1024 * 1024 ) {
330
331
this . globalVars . _alertError (
331
- 'File is too large. Please choose a file less than 4GB '
332
+ 'File is too large. Please choose a file less than 65MB '
332
333
) ;
333
334
return ;
334
335
}
335
- let upload : tus . Upload ;
336
- let mediaId = '' ;
337
- const comp : FeedCreatePostComponent = this ;
338
- const options = {
339
- endpoint : this . backendApi . _makeRequestURL (
340
- environment . uploadVideoHostname ,
341
- BackendRoutes . RoutePathUploadVideo
342
- ) ,
343
- chunkSize : 50 * 1024 * 1024 , // Required a minimum chunk size of 5MB, here we use 50MB.
344
- uploadSize : file . size ,
345
- onError : function ( error ) {
346
- comp . globalVars . _alertError ( error . message ) ;
347
- upload . abort ( true ) . then ( ( ) => {
348
- throw error ;
349
- } ) ;
350
- } ,
351
- onProgress : function ( bytesUploaded , bytesTotal ) {
352
- comp . videoUploadPercentage = (
353
- ( bytesUploaded / bytesTotal ) *
354
- 100
355
- ) . toFixed ( 2 ) ;
356
- } ,
357
- onSuccess : function ( ) {
358
- // Construct the url for the video based on the videoId and use the iframe url.
359
- comp . postVideoSrc = `https://iframe.videodelivery.net/${ mediaId } ` ;
360
- comp . postImageSrc = null ;
361
- comp . videoUploadPercentage = null ;
362
- comp . pollForReadyToStream ( ) ;
363
- } ,
364
- onAfterResponse : function ( req , res ) {
365
- return new Promise ( ( resolve ) => {
366
- // The stream-media-id header is the video Id in Cloudflare's system that we'll need to locate the video for streaming.
367
- let mediaIdHeader = res . getHeader ( 'stream-media-id' ) ;
368
- if ( mediaIdHeader ) {
369
- mediaId = mediaIdHeader ;
370
- }
371
- resolve ( res ) ;
372
- } ) ;
373
- } ,
374
- } ;
375
- // Clear the interval used for polling cloudflare to check if a video is ready to stream.
376
- if ( this . videoStreamInterval != null ) {
377
- clearInterval ( this . videoStreamInterval ) ;
336
+ // Set this so that the video upload progress bar shows up.
337
+ this . postVideoSrc = 'https://lvpr.tv' ;
338
+ let tusEndpoint , asset ;
339
+ try {
340
+ ( { tusEndpoint, asset } = await this . backendApi
341
+ . UploadVideo (
342
+ environment . uploadVideoHostname ,
343
+ file ,
344
+ this . globalVars . loggedInUser . PublicKeyBase58Check
345
+ )
346
+ . toPromise ( ) ) ;
347
+ } catch ( e ) {
348
+ this . postVideoSrc = '' ;
349
+ this . globalVars . _alertError ( JSON . stringify ( e . error . error ) ) ;
350
+ return ;
378
351
}
379
- // Reset the postVideoSrc and readyToStream values.
380
- this . postVideoSrc = null ;
381
- this . readyToStream = false ;
382
- // Create and start the upload.
383
- upload = new tus . Upload ( file , options ) ;
384
- upload . start ( ) ;
385
- return ;
352
+
353
+ this . postVideoSrc = `https://lvpr.tv/?v= ${ asset . playbackId } ` ;
354
+ this . assetId = asset . id ;
355
+ this . postImageSrc = '' ;
356
+ this . videoUploadPercentage = null ;
357
+
358
+ this . pollForReadyToStream ( ) ;
386
359
}
387
360
388
361
pollForReadyToStream ( ) : void {
@@ -395,19 +368,26 @@ export class FeedCreatePostComponent implements OnInit {
395
368
return ;
396
369
}
397
370
this . streamService
398
- . checkVideoStatusByURL ( this . postVideoSrc )
399
- . subscribe ( ( [ readyToStream , exitPolling ] ) => {
371
+ . checkVideoStatusByURL ( this . assetId )
372
+ . then ( ( [ readyToStream , exitPolling , failed ] ) => {
400
373
if ( readyToStream ) {
401
374
this . readyToStream = true ;
402
375
clearInterval ( this . videoStreamInterval ) ;
403
376
return ;
404
377
}
378
+ if ( failed ) {
379
+ clearInterval ( this . videoStreamInterval ) ;
380
+ this . postVideoSrc = '' ;
381
+ this . globalVars . _alertError (
382
+ 'Video failed to upload. Please try again.'
383
+ ) ;
384
+ }
405
385
if ( exitPolling ) {
406
386
clearInterval ( this . videoStreamInterval ) ;
407
387
return ;
408
388
}
409
- } )
410
- . add ( ( ) => attempts ++ ) ;
389
+ } ) ;
390
+ attempts ++ ;
411
391
} , timeoutMillis ) ;
412
392
}
413
393
}
0 commit comments