]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, HostListener, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { Notifier } from '@app/core'
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 import { VideoDetails } from '@app/shared/video/video-details.model'
16
17 @Component({
18 selector: 'my-videos-update',
19 styleUrls: [ './shared/video-edit.component.scss' ],
20 templateUrl: './video-update.component.html'
21 })
22 export class VideoUpdateComponent extends FormReactive implements OnInit {
23 video: VideoEdit
24
25 isUpdatingVideo = false
26 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
27 userVideoChannels: { id: number, label: string, support: string }[] = []
28 schedulePublicationPossible = false
29 videoCaptions: VideoCaptionEdit[] = []
30 waitTranscodingEnabled = true
31
32 private updateDone = false
33
34 constructor (
35 protected formValidatorService: FormValidatorService,
36 private route: ActivatedRoute,
37 private router: Router,
38 private notifier: Notifier,
39 private serverService: ServerService,
40 private videoService: VideoService,
41 private loadingBar: LoadingBarService,
42 private videoCaptionService: VideoCaptionService,
43 private i18n: I18n
44 ) {
45 super()
46 }
47
48 ngOnInit () {
49 this.buildForm({})
50
51 this.serverService.videoPrivaciesLoaded
52 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
53
54 this.route.data
55 .pipe(map(data => data.videoData))
56 .subscribe(({ video, videoChannels, videoCaptions }) => {
57 this.video = new VideoEdit(video)
58 this.userVideoChannels = videoChannels
59 this.videoCaptions = videoCaptions
60
61 // We cannot set private a video that was not private
62 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
63 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
64 } else { // We can schedule video publication only if it it is private
65 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
66 }
67
68 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
69
70 const videoFiles = (video as VideoDetails).files
71 if (videoFiles.length > 1) { // Already transcoded
72 this.waitTranscodingEnabled = false
73 }
74
75 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
76 setTimeout(() => this.hydrateFormFromVideo())
77 },
78
79 err => {
80 console.error(err)
81 this.notifier.error(err.message)
82 }
83 )
84 }
85
86 @HostListener('window:beforeunload', [ '$event' ])
87 onUnload (event: any) {
88 const { text, canDeactivate } = this.canDeactivate()
89
90 if (canDeactivate) return
91
92 event.returnValue = text
93 return text
94 }
95
96 canDeactivate (): { canDeactivate: boolean, text?: string } {
97 if (this.updateDone === true) return { canDeactivate: true }
98
99 const text = this.i18n('You have unsaved changes! If you leave, your changes will be lost.')
100
101 for (const caption of this.videoCaptions) {
102 if (caption.action) return { canDeactivate: false, text }
103 }
104
105 return { canDeactivate: this.formChanged === false, text }
106 }
107
108 checkForm () {
109 this.forceCheck()
110
111 return this.form.valid
112 }
113
114 update () {
115 if (this.checkForm() === false
116 || this.isUpdatingVideo === true) {
117 return
118 }
119
120 this.video.patch(this.form.value)
121
122 this.loadingBar.start()
123 this.isUpdatingVideo = true
124
125 // Update the video
126 this.videoService.updateVideo(this.video)
127 .pipe(
128 // Then update captions
129 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
130 )
131 .subscribe(
132 () => {
133 this.updateDone = true
134 this.isUpdatingVideo = false
135 this.loadingBar.complete()
136 this.notifier.success(this.i18n('Video updated.'))
137 this.router.navigate([ '/videos/watch', this.video.uuid ])
138 },
139
140 err => {
141 this.loadingBar.complete()
142 this.isUpdatingVideo = false
143 this.notifier.error(err.message)
144 console.error(err)
145 }
146 )
147 }
148
149 private hydrateFormFromVideo () {
150 this.form.patchValue(this.video.toFormPatch())
151
152 const objects = [
153 {
154 url: 'thumbnailUrl',
155 name: 'thumbnailfile'
156 },
157 {
158 url: 'previewUrl',
159 name: 'previewfile'
160 }
161 ]
162
163 for (const obj of objects) {
164 fetch(this.video[obj.url])
165 .then(response => response.blob())
166 .then(data => {
167 this.form.patchValue({
168 [ obj.name ]: data
169 })
170 })
171 }
172 }
173 }