]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
store uploaded video filename (#4885)
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-update.component.ts
CommitLineData
f8c00564 1import { of } from 'rxjs'
0ba9696c 2import { switchMap } from 'rxjs/operators'
21e493d4 3import { SelectChannelItem } from 'src/types/select-options-item.model'
674a66bb 4import { Component, HostListener, OnInit } from '@angular/core'
df98563e 5import { ActivatedRoute, Router } from '@angular/router'
f8b2c1b4 6import { Notifier } from '@app/core'
21e493d4 7import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
d4a8e7a6 8import { Video, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
f8c00564 9import { LiveVideoService } from '@app/shared/shared-video-live'
67ed6552 10import { LoadingBarService } from '@ngx-loading-bar/core'
b5b68755 11import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
c6c0fa6c 12import { hydrateFormFromVideo } from './shared/video-edit-utils'
2e401e85 13import { VideoSource } from '@shared/models/videos/video-source'
1553e15d 14
dc8bc31b 15@Component({
d8e689b8 16 selector: 'my-videos-update',
80958c78 17 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 18 templateUrl: './video-update.component.html'
dc8bc31b 19})
d8e689b8 20export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 21 video: VideoEdit
ddb62a85 22 videoDetails: VideoDetails
2e401e85 23 videoSource: VideoSource
c6c0fa6c
C
24 userVideoChannels: SelectChannelItem[] = []
25 videoCaptions: VideoCaptionEdit[] = []
a5cf76af 26 liveVideo: LiveVideo
4b2f33f3 27
68e24d72 28 isUpdatingVideo = false
4afec735 29 forbidScheduledPublication = false
14e2014a 30 waitTranscodingEnabled = true
dc8bc31b 31
772d5642
C
32 private updateDone = false
33
df98563e 34 constructor (
d18d6478 35 protected formValidatorService: FormValidatorService,
d8e689b8 36 private route: ActivatedRoute,
7ddd02c9 37 private router: Router,
f8b2c1b4 38 private notifier: Notifier,
15a7387d 39 private videoService: VideoService,
6200d8d9 40 private loadingBar: LoadingBarService,
b5b68755
C
41 private videoCaptionService: VideoCaptionService,
42 private liveVideoService: LiveVideoService
9df52d66 43 ) {
df98563e 44 super()
4b2f33f3 45 }
dc8bc31b 46
df98563e 47 ngOnInit () {
d18d6478 48 this.buildForm({})
d8e689b8 49
0ba9696c 50 const { videoData } = this.route.snapshot.data
2e401e85 51 const { video, videoChannels, videoCaptions, videoSource, liveVideo } = videoData
0ba9696c
C
52
53 this.video = new VideoEdit(video)
54 this.videoDetails = video
55
56 this.userVideoChannels = videoChannels
57 this.videoCaptions = videoCaptions
2e401e85 58 this.videoSource = videoSource
0ba9696c
C
59 this.liveVideo = liveVideo
60
4afec735 61 this.forbidScheduledPublication = this.video.privacy !== VideoPrivacy.PRIVATE
0ba9696c
C
62 }
63
64 onFormBuilt () {
65 hydrateFormFromVideo(this.form, this.video, true)
66
67 if (this.liveVideo) {
68 this.form.patchValue({
69 saveReplay: this.liveVideo.saveReplay,
f443a746 70 latencyMode: this.liveVideo.latencyMode,
0ba9696c
C
71 permanentLive: this.liveVideo.permanentLive
72 })
73 }
e822fdae
C
74 }
75
674a66bb
C
76 @HostListener('window:beforeunload', [ '$event' ])
77 onUnload (event: any) {
78 const { text, canDeactivate } = this.canDeactivate()
79
80 if (canDeactivate) return
81
82 event.returnValue = text
83 return text
84 }
85
86 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
87 if (this.updateDone === true) return { canDeactivate: true }
88
66357162 89 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 90
772d5642 91 for (const caption of this.videoCaptions) {
674a66bb 92 if (caption.action) return { canDeactivate: false, text }
772d5642
C
93 }
94
674a66bb 95 return { canDeactivate: this.formChanged === false, text }
772d5642
C
96 }
97
ddb62a85
C
98 isWaitTranscodingEnabled () {
99 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
100 return false
101 }
102
103 if (this.liveVideo && this.form.value['saveReplay'] !== true) {
104 return false
105 }
106
107 return true
108 }
109
cc4bf76c
C
110 async update () {
111 await this.waitPendingCheck()
112 this.forceCheck()
113
114 if (!this.form.valid || this.isUpdatingVideo === true) {
df98563e 115 return
c24ac1c1
C
116 }
117
df98563e 118 this.video.patch(this.form.value)
d8e689b8 119
a02b93ce 120 this.loadingBar.useRef().start()
68e24d72 121 this.isUpdatingVideo = true
40e87e9e
C
122
123 // Update the video
d8e689b8 124 this.videoService.updateVideo(this.video)
40e87e9e
C
125 .pipe(
126 // Then update captions
b5b68755
C
127 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
128
129 switchMap(() => {
130 if (!this.liveVideo) return of(undefined)
131
fd0fdc46 132 const liveVideoUpdate: LiveVideoUpdate = {
5d84d717 133 saveReplay: !!this.form.value.saveReplay,
f443a746
C
134 permanentLive: !!this.form.value.permanentLive,
135 latencyMode: this.form.value.latencyMode
fd0fdc46
C
136 }
137
fd0fdc46
C
138 // Don't update live attributes if they did not change
139 const liveChanged = Object.keys(liveVideoUpdate)
140 .some(key => this.liveVideo[key] !== liveVideoUpdate[key])
141 if (!liveChanged) return of(undefined)
142
b5b68755
C
143 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
144 })
40e87e9e 145 )
1378c0d3
C
146 .subscribe({
147 next: () => {
772d5642 148 this.updateDone = true
40e87e9e 149 this.isUpdatingVideo = false
a02b93ce 150 this.loadingBar.useRef().complete()
66357162 151 this.notifier.success($localize`Video updated.`)
d4a8e7a6 152 this.router.navigateByUrl(Video.buildWatchUrl(this.video))
40e87e9e
C
153 },
154
1378c0d3 155 error: err => {
a02b93ce 156 this.loadingBar.useRef().complete()
40e87e9e 157 this.isUpdatingVideo = false
f8b2c1b4 158 this.notifier.error(err.message)
40e87e9e
C
159 console.error(err)
160 }
1378c0d3 161 })
dc8bc31b 162 }
e54163c2 163
7294aab0
C
164 hydratePluginFieldsFromVideo () {
165 if (!this.video.pluginData) return
166
167 this.form.patchValue({
168 pluginData: this.video.pluginData
169 })
170 }
d4a8e7a6
C
171
172 getVideoUrl () {
173 return Video.buildWatchUrl(this.videoDetails)
174 }
dc8bc31b 175}