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