2018-05-15 04:55:51 -05:00
|
|
|
import { map, switchMap } from 'rxjs/operators'
|
2019-01-14 07:55:43 -06:00
|
|
|
import { Component, HostListener, OnInit } from '@angular/core'
|
2017-06-16 07:32:15 -05:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router'
|
2018-12-19 09:04:34 -06:00
|
|
|
import { Notifier } from '@app/core'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
|
|
|
|
import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
|
|
|
|
import { LoadingBarService } from '@ngx-loading-bar/core'
|
2018-06-04 09:21:17 -05:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2019-06-07 08:37:36 -05:00
|
|
|
import { VideoPrivacy } from '@shared/models'
|
2016-04-14 15:07:46 -05:00
|
|
|
|
2016-03-14 07:50:19 -05:00
|
|
|
@Component({
|
2017-04-10 14:15:28 -05:00
|
|
|
selector: 'my-videos-update',
|
2017-11-28 08:40:53 -06:00
|
|
|
styleUrls: [ './shared/video-edit.component.scss' ],
|
2017-04-10 14:15:28 -05:00
|
|
|
templateUrl: './video-update.component.html'
|
2016-03-14 07:50:19 -05:00
|
|
|
})
|
2017-04-10 14:15:28 -05:00
|
|
|
export class VideoUpdateComponent extends FormReactive implements OnInit {
|
2017-10-25 09:43:19 -05:00
|
|
|
video: VideoEdit
|
2016-09-09 15:16:51 -05:00
|
|
|
|
2018-02-16 10:24:47 -06:00
|
|
|
isUpdatingVideo = false
|
2020-08-04 17:50:07 -05:00
|
|
|
userVideoChannels: { id: number, label: string, support: string, avatar?: string }[] = []
|
2018-06-15 09:52:15 -05:00
|
|
|
schedulePublicationPossible = false
|
2018-07-16 10:36:42 -05:00
|
|
|
videoCaptions: VideoCaptionEdit[] = []
|
2018-12-11 07:52:50 -06:00
|
|
|
waitTranscodingEnabled = true
|
2016-03-14 07:50:19 -05:00
|
|
|
|
2018-07-25 03:28:43 -05:00
|
|
|
private updateDone = false
|
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
constructor (
|
2018-06-05 03:58:45 -05:00
|
|
|
protected formValidatorService: FormValidatorService,
|
2017-04-10 14:15:28 -05:00
|
|
|
private route: ActivatedRoute,
|
2017-01-27 09:14:11 -06:00
|
|
|
private router: Router,
|
2018-12-19 09:04:34 -06:00
|
|
|
private notifier: Notifier,
|
2017-12-20 07:29:55 -06:00
|
|
|
private videoService: VideoService,
|
2018-05-16 04:33:11 -05:00
|
|
|
private loadingBar: LoadingBarService,
|
2018-07-12 12:02:00 -05:00
|
|
|
private videoCaptionService: VideoCaptionService,
|
2018-06-04 09:21:17 -05:00
|
|
|
private i18n: I18n
|
2016-09-09 15:16:51 -05:00
|
|
|
) {
|
2017-06-16 07:32:15 -05:00
|
|
|
super()
|
2016-09-09 15:16:51 -05:00
|
|
|
}
|
2016-03-14 07:50:19 -05:00
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
ngOnInit () {
|
2018-06-05 03:58:45 -05:00
|
|
|
this.buildForm({})
|
2017-04-10 14:15:28 -05:00
|
|
|
|
2018-07-16 11:09:31 -05:00
|
|
|
this.route.data
|
|
|
|
.pipe(map(data => data.videoData))
|
|
|
|
.subscribe(({ video, videoChannels, videoCaptions }) => {
|
|
|
|
this.video = new VideoEdit(video)
|
|
|
|
this.userVideoChannels = videoChannels
|
|
|
|
this.videoCaptions = videoCaptions
|
2018-05-15 04:55:51 -05:00
|
|
|
|
2019-06-07 08:37:36 -05:00
|
|
|
this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
|
|
|
|
|
2019-11-21 10:01:24 -06:00
|
|
|
const videoFiles = (video as VideoDetails).getFiles()
|
2018-12-11 07:52:50 -06:00
|
|
|
if (videoFiles.length > 1) { // Already transcoded
|
|
|
|
this.waitTranscodingEnabled = false
|
|
|
|
}
|
|
|
|
|
2018-07-25 03:28:43 -05:00
|
|
|
// FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
|
2018-07-16 11:09:31 -05:00
|
|
|
setTimeout(() => this.hydrateFormFromVideo())
|
|
|
|
},
|
2017-10-30 14:26:06 -05:00
|
|
|
|
2018-07-16 11:09:31 -05:00
|
|
|
err => {
|
|
|
|
console.error(err)
|
2018-12-19 09:04:34 -06:00
|
|
|
this.notifier.error(err.message)
|
2018-07-16 11:09:31 -05:00
|
|
|
}
|
|
|
|
)
|
2016-06-07 15:34:02 -05:00
|
|
|
}
|
|
|
|
|
2019-01-14 07:55:43 -06:00
|
|
|
@HostListener('window:beforeunload', [ '$event' ])
|
|
|
|
onUnload (event: any) {
|
|
|
|
const { text, canDeactivate } = this.canDeactivate()
|
|
|
|
|
|
|
|
if (canDeactivate) return
|
|
|
|
|
|
|
|
event.returnValue = text
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
canDeactivate (): { canDeactivate: boolean, text?: string } {
|
2018-07-25 03:28:43 -05:00
|
|
|
if (this.updateDone === true) return { canDeactivate: true }
|
|
|
|
|
2019-01-14 07:55:43 -06:00
|
|
|
const text = this.i18n('You have unsaved changes! If you leave, your changes will be lost.')
|
|
|
|
|
2018-07-25 03:28:43 -05:00
|
|
|
for (const caption of this.videoCaptions) {
|
2019-01-14 07:55:43 -06:00
|
|
|
if (caption.action) return { canDeactivate: false, text }
|
2018-07-25 03:28:43 -05:00
|
|
|
}
|
|
|
|
|
2019-01-14 07:55:43 -06:00
|
|
|
return { canDeactivate: this.formChanged === false, text }
|
2018-07-25 03:28:43 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
checkForm () {
|
|
|
|
this.forceCheck()
|
2017-05-05 07:29:58 -05:00
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
return this.form.valid
|
2017-05-05 07:29:58 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
update () {
|
2018-09-28 19:42:45 -05:00
|
|
|
if (this.checkForm() === false
|
|
|
|
|| this.isUpdatingVideo === true) {
|
2017-06-16 07:32:15 -05:00
|
|
|
return
|
2017-05-05 07:29:58 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
this.video.patch(this.form.value)
|
2017-04-10 14:15:28 -05:00
|
|
|
|
2020-08-06 09:14:58 -05:00
|
|
|
this.loadingBar.useRef().start()
|
2018-02-16 10:24:47 -06:00
|
|
|
this.isUpdatingVideo = true
|
2018-07-12 12:02:00 -05:00
|
|
|
|
|
|
|
// Update the video
|
2017-04-10 14:15:28 -05:00
|
|
|
this.videoService.updateVideo(this.video)
|
2018-07-12 12:02:00 -05:00
|
|
|
.pipe(
|
|
|
|
// Then update captions
|
|
|
|
switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
|
|
|
|
)
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
2018-07-25 03:28:43 -05:00
|
|
|
this.updateDone = true
|
2018-07-12 12:02:00 -05:00
|
|
|
this.isUpdatingVideo = false
|
2020-08-06 09:14:58 -05:00
|
|
|
this.loadingBar.useRef().complete()
|
2018-12-19 09:04:34 -06:00
|
|
|
this.notifier.success(this.i18n('Video updated.'))
|
2018-07-12 12:02:00 -05:00
|
|
|
this.router.navigate([ '/videos/watch', this.video.uuid ])
|
|
|
|
},
|
|
|
|
|
|
|
|
err => {
|
2020-08-06 09:14:58 -05:00
|
|
|
this.loadingBar.useRef().complete()
|
2018-07-12 12:02:00 -05:00
|
|
|
this.isUpdatingVideo = false
|
2018-12-19 09:04:34 -06:00
|
|
|
this.notifier.error(err.message)
|
2018-07-12 12:02:00 -05:00
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
)
|
2016-03-14 07:50:19 -05:00
|
|
|
}
|
2017-03-22 15:47:05 -05:00
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
private hydrateFormFromVideo () {
|
2018-06-15 09:52:15 -05:00
|
|
|
this.form.patchValue(this.video.toFormPatch())
|
2018-02-16 09:35:32 -06:00
|
|
|
|
|
|
|
const objects = [
|
|
|
|
{
|
|
|
|
url: 'thumbnailUrl',
|
|
|
|
name: 'thumbnailfile'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: 'previewUrl',
|
|
|
|
name: 'previewfile'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const obj of objects) {
|
|
|
|
fetch(this.video[obj.url])
|
|
|
|
.then(response => response.blob())
|
|
|
|
.then(data => {
|
|
|
|
this.form.patchValue({
|
|
|
|
[ obj.name ]: data
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-04-10 14:15:28 -05:00
|
|
|
}
|
2016-03-14 07:50:19 -05:00
|
|
|
}
|