]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Fix adding captions to a video
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { VideoEdit } from '../../shared/video/video-edit.model'
10 import { VideoService } from '../../shared/video/video.service'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
15
16 @Component({
17 selector: 'my-videos-update',
18 styleUrls: [ './shared/video-edit.component.scss' ],
19 templateUrl: './video-update.component.html'
20 })
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
22 video: VideoEdit
23
24 isUpdatingVideo = false
25 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
26 userVideoChannels: { id: number, label: string, support: string }[] = []
27 schedulePublicationPossible = false
28 videoCaptions: VideoCaptionEdit[] = []
29
30 private updateDone = false
31
32 constructor (
33 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private serverService: ServerService,
38 private videoService: VideoService,
39 private loadingBar: LoadingBarService,
40 private videoCaptionService: VideoCaptionService,
41 private i18n: I18n
42 ) {
43 super()
44 }
45
46 ngOnInit () {
47 this.buildForm({})
48
49 this.serverService.videoPrivaciesLoaded
50 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
51
52 this.route.data
53 .pipe(map(data => data.videoData))
54 .subscribe(({ video, videoChannels, videoCaptions }) => {
55 this.video = new VideoEdit(video)
56 this.userVideoChannels = videoChannels
57 this.videoCaptions = videoCaptions
58
59 // We cannot set private a video that was not private
60 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
61 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
62 } else { // We can schedule video publication only if it it is private
63 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
64 }
65
66 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
67
68 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
69 setTimeout(() => this.hydrateFormFromVideo())
70 },
71
72 err => {
73 console.error(err)
74 this.notificationsService.error(this.i18n('Error'), err.message)
75 }
76 )
77 }
78
79 canDeactivate () {
80 if (this.updateDone === true) return { canDeactivate: true }
81
82 for (const caption of this.videoCaptions) {
83 if (caption.action) return { canDeactivate: false }
84 }
85
86 return { canDeactivate: this.formChanged === false }
87 }
88
89 checkForm () {
90 this.forceCheck()
91
92 return this.form.valid
93 }
94
95 update () {
96 if (this.checkForm() === false
97 || this.isUpdatingVideo === true) {
98 return
99 }
100
101 this.video.patch(this.form.value)
102
103 this.loadingBar.start()
104 this.isUpdatingVideo = true
105
106 // Update the video
107 this.videoService.updateVideo(this.video)
108 .pipe(
109 // Then update captions
110 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
111 )
112 .subscribe(
113 () => {
114 this.updateDone = true
115 this.isUpdatingVideo = false
116 this.loadingBar.complete()
117 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
118 this.router.navigate([ '/videos/watch', this.video.uuid ])
119 },
120
121 err => {
122 this.loadingBar.complete()
123 this.isUpdatingVideo = false
124 this.notificationsService.error(this.i18n('Error'), err.message)
125 console.error(err)
126 }
127 )
128 }
129
130 private hydrateFormFromVideo () {
131 this.form.patchValue(this.video.toFormPatch())
132
133 const objects = [
134 {
135 url: 'thumbnailUrl',
136 name: 'thumbnailfile'
137 },
138 {
139 url: 'previewUrl',
140 name: 'previewfile'
141 }
142 ]
143
144 for (const obj of objects) {
145 fetch(this.video[obj.url])
146 .then(response => response.blob())
147 .then(data => {
148 this.form.patchValue({
149 [ obj.name ]: data
150 })
151 })
152 }
153 }
154 }