2018-05-15 05:55:51 -04:00
|
|
|
import { map, switchMap } from 'rxjs/operators'
|
2017-10-09 13:12:40 -04:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
2017-06-16 08:32:15 -04:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router'
|
2018-02-16 11:24:47 -05:00
|
|
|
import { LoadingBarService } from '@ngx-loading-bar/core'
|
2017-06-16 08:32:15 -04:00
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
2018-07-16 11:36:42 -04:00
|
|
|
import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
|
2017-10-09 13:12:40 -04:00
|
|
|
import { ServerService } from '../../core'
|
2017-12-20 08:29:55 -05:00
|
|
|
import { AuthService } from '../../core/auth'
|
2017-12-07 08:48:47 -05:00
|
|
|
import { FormReactive } from '../../shared'
|
2017-12-01 12:56:26 -05:00
|
|
|
import { VideoEdit } from '../../shared/video/video-edit.model'
|
2017-12-07 08:48:47 -05:00
|
|
|
import { VideoService } from '../../shared/video/video.service'
|
2018-05-16 05:33:11 -04:00
|
|
|
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
|
2018-06-04 10:21:17 -04:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2018-06-05 04:58:45 -04:00
|
|
|
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
|
2018-07-12 13:02:00 -04:00
|
|
|
import { VideoCaptionService } from '@app/shared/video-caption'
|
2018-07-16 11:36:42 -04:00
|
|
|
import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
|
2016-04-14 16:07:46 -04:00
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
@Component({
|
2017-04-10 15:15:28 -04:00
|
|
|
selector: 'my-videos-update',
|
2017-11-28 09:40:53 -05:00
|
|
|
styleUrls: [ './shared/video-edit.component.scss' ],
|
2017-04-10 15:15:28 -04:00
|
|
|
templateUrl: './video-update.component.html'
|
2016-03-14 08:50:19 -04:00
|
|
|
})
|
2017-04-10 15:15:28 -04:00
|
|
|
export class VideoUpdateComponent extends FormReactive implements OnInit {
|
2017-10-25 10:43:19 -04:00
|
|
|
video: VideoEdit
|
2016-09-09 16:16:51 -04:00
|
|
|
|
2018-02-16 11:24:47 -05:00
|
|
|
isUpdatingVideo = false
|
2018-07-16 11:36:42 -04:00
|
|
|
videoPrivacies: VideoConstant<string>[] = []
|
|
|
|
userVideoChannels: { id: number, label: string, support: string }[] = []
|
2018-06-15 10:52:15 -04:00
|
|
|
schedulePublicationPossible = false
|
2018-07-16 11:36:42 -04:00
|
|
|
videoCaptions: VideoCaptionEdit[] = []
|
2016-03-14 08:50:19 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
constructor (
|
2018-06-05 04:58:45 -04:00
|
|
|
protected formValidatorService: FormValidatorService,
|
2017-04-10 15:15:28 -04:00
|
|
|
private route: ActivatedRoute,
|
2017-01-27 10:14:11 -05:00
|
|
|
private router: Router,
|
2017-03-22 16:15:55 -04:00
|
|
|
private notificationsService: NotificationsService,
|
2017-10-09 13:12:40 -04:00
|
|
|
private serverService: ServerService,
|
2017-12-20 08:29:55 -05:00
|
|
|
private videoService: VideoService,
|
2018-02-16 11:24:47 -05:00
|
|
|
private authService: AuthService,
|
2018-05-16 05:33:11 -04:00
|
|
|
private loadingBar: LoadingBarService,
|
2018-06-04 10:21:17 -04:00
|
|
|
private videoChannelService: VideoChannelService,
|
2018-07-12 13:02:00 -04:00
|
|
|
private videoCaptionService: VideoCaptionService,
|
2018-06-04 10:21:17 -04:00
|
|
|
private i18n: I18n
|
2016-09-09 16:16:51 -04:00
|
|
|
) {
|
2017-06-16 08:32:15 -04:00
|
|
|
super()
|
2016-09-09 16:16:51 -04:00
|
|
|
}
|
2016-03-14 08:50:19 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
ngOnInit () {
|
2018-06-05 04:58:45 -04:00
|
|
|
this.buildForm({})
|
2017-04-10 15:15:28 -04:00
|
|
|
|
2017-12-20 08:29:55 -05:00
|
|
|
this.serverService.videoPrivaciesLoaded
|
2018-07-16 12:09:31 -04:00
|
|
|
.subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
|
2017-12-20 08:29:55 -05:00
|
|
|
|
2018-07-16 12:09:31 -04: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 05:55:51 -04:00
|
|
|
|
2018-07-16 12:09:31 -04:00
|
|
|
// We cannot set private a video that was not private
|
|
|
|
if (this.video.privacy !== VideoPrivacy.PRIVATE) {
|
|
|
|
this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString())
|
|
|
|
} else { // We can schedule video publication only if it it is private
|
|
|
|
this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
|
|
|
|
}
|
2017-10-31 06:52:52 -04:00
|
|
|
|
2018-07-16 12:09:31 -04:00
|
|
|
// FIXME: Angular does not detec
|
|
|
|
setTimeout(() => this.hydrateFormFromVideo())
|
|
|
|
},
|
2017-10-30 15:26:06 -04:00
|
|
|
|
2018-07-16 12:09:31 -04:00
|
|
|
err => {
|
|
|
|
console.error(err)
|
|
|
|
this.notificationsService.error(this.i18n('Error'), err.message)
|
|
|
|
}
|
|
|
|
)
|
2016-06-07 16:34:02 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
checkForm () {
|
|
|
|
this.forceCheck()
|
2017-05-05 08:29:58 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
return this.form.valid
|
2017-05-05 08:29:58 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
update () {
|
2017-05-05 08:29:58 -04:00
|
|
|
if (this.checkForm() === false) {
|
2017-06-16 08:32:15 -04:00
|
|
|
return
|
2017-05-05 08:29:58 -04:00
|
|
|
}
|
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
this.video.patch(this.form.value)
|
2017-04-10 15:15:28 -04:00
|
|
|
|
2018-02-16 11:24:47 -05:00
|
|
|
this.loadingBar.start()
|
|
|
|
this.isUpdatingVideo = true
|
2018-07-12 13:02:00 -04:00
|
|
|
|
|
|
|
// Update the video
|
2017-04-10 15:15:28 -04:00
|
|
|
this.videoService.updateVideo(this.video)
|
2018-07-12 13:02:00 -04:00
|
|
|
.pipe(
|
|
|
|
// Then update captions
|
|
|
|
switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
|
|
|
|
)
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.isUpdatingVideo = false
|
|
|
|
this.loadingBar.complete()
|
|
|
|
this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
|
|
|
|
this.router.navigate([ '/videos/watch', this.video.uuid ])
|
|
|
|
},
|
|
|
|
|
|
|
|
err => {
|
2018-07-17 08:44:19 -04:00
|
|
|
this.loadingBar.complete()
|
2018-07-12 13:02:00 -04:00
|
|
|
this.isUpdatingVideo = false
|
|
|
|
this.notificationsService.error(this.i18n('Error'), err.message)
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
)
|
2016-06-07 16:34:02 -04:00
|
|
|
|
2016-03-14 08:50:19 -04:00
|
|
|
}
|
2017-03-22 16:47:05 -04:00
|
|
|
|
2017-06-16 08:32:15 -04:00
|
|
|
private hydrateFormFromVideo () {
|
2018-06-15 10:52:15 -04:00
|
|
|
this.form.patchValue(this.video.toFormPatch())
|
2018-02-16 10:35:32 -05: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 15:15:28 -04:00
|
|
|
}
|
2016-03-14 08:50:19 -04:00
|
|
|
}
|