]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { map, switchMap } from 'rxjs/operators' |
db7af09b | 2 | import { Component, OnInit } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
68e24d72 | 4 | import { LoadingBarService } from '@ngx-loading-bar/core' |
f8b2c1b4 | 5 | import { Notifier } from '@app/core' |
ef4c78da | 6 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' |
db7af09b | 7 | import { ServerService } from '../../core' |
4cc66133 | 8 | import { FormReactive } from '../../shared' |
202f6b6c | 9 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 10 | import { VideoService } from '../../shared/video/video.service' |
b1d40cff | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 12 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
40e87e9e | 13 | import { VideoCaptionService } from '@app/shared/video-caption' |
ef4c78da | 14 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model' |
14e2014a | 15 | import { VideoDetails } from '@app/shared/video/video-details.model' |
1553e15d | 16 | |
dc8bc31b | 17 | @Component({ |
d8e689b8 | 18 | selector: 'my-videos-update', |
80958c78 | 19 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 20 | templateUrl: './video-update.component.html' |
dc8bc31b | 21 | }) |
d8e689b8 | 22 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 23 | video: VideoEdit |
4b2f33f3 | 24 | |
68e24d72 | 25 | isUpdatingVideo = false |
8cd7faaa | 26 | videoPrivacies: VideoConstant<VideoPrivacy>[] = [] |
ef4c78da | 27 | userVideoChannels: { id: number, label: string, support: string }[] = [] |
bbe0f064 | 28 | schedulePublicationPossible = false |
ef4c78da | 29 | videoCaptions: VideoCaptionEdit[] = [] |
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, |
db7af09b | 39 | private serverService: ServerService, |
15a7387d | 40 | private videoService: VideoService, |
6200d8d9 | 41 | private loadingBar: LoadingBarService, |
40e87e9e | 42 | private videoCaptionService: VideoCaptionService, |
b1d40cff | 43 | private i18n: I18n |
4b2f33f3 | 44 | ) { |
df98563e | 45 | super() |
4b2f33f3 | 46 | } |
dc8bc31b | 47 | |
df98563e | 48 | ngOnInit () { |
d18d6478 | 49 | this.buildForm({}) |
d8e689b8 | 50 | |
15a7387d | 51 | this.serverService.videoPrivaciesLoaded |
308c4275 | 52 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 53 | |
308c4275 C |
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 | |
db400f44 | 60 | |
308c4275 C |
61 | // We cannot set private a video that was not private |
62 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { | |
8cd7faaa | 63 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE) |
308c4275 C |
64 | } else { // We can schedule video publication only if it it is private |
65 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
66 | } | |
fd45e8f4 | 67 | |
8cd7faaa C |
68 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies) |
69 | ||
14e2014a C |
70 | const videoFiles = (video as VideoDetails).files |
71 | if (videoFiles.length > 1) { // Already transcoded | |
72 | this.waitTranscodingEnabled = false | |
73 | } | |
74 | ||
772d5642 | 75 | // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout |
308c4275 C |
76 | setTimeout(() => this.hydrateFormFromVideo()) |
77 | }, | |
2de96f4d | 78 | |
308c4275 C |
79 | err => { |
80 | console.error(err) | |
f8b2c1b4 | 81 | this.notifier.error(err.message) |
308c4275 C |
82 | } |
83 | ) | |
e822fdae C |
84 | } |
85 | ||
772d5642 C |
86 | canDeactivate () { |
87 | if (this.updateDone === true) return { canDeactivate: true } | |
88 | ||
89 | for (const caption of this.videoCaptions) { | |
90 | if (caption.action) return { canDeactivate: false } | |
91 | } | |
92 | ||
93 | return { canDeactivate: this.formChanged === false } | |
94 | } | |
95 | ||
df98563e C |
96 | checkForm () { |
97 | this.forceCheck() | |
c24ac1c1 | 98 | |
df98563e | 99 | return this.form.valid |
c24ac1c1 C |
100 | } |
101 | ||
df98563e | 102 | update () { |
8c2b9756 RK |
103 | if (this.checkForm() === false |
104 | || this.isUpdatingVideo === true) { | |
df98563e | 105 | return |
c24ac1c1 C |
106 | } |
107 | ||
df98563e | 108 | this.video.patch(this.form.value) |
d8e689b8 | 109 | |
68e24d72 C |
110 | this.loadingBar.start() |
111 | this.isUpdatingVideo = true | |
40e87e9e C |
112 | |
113 | // Update the video | |
d8e689b8 | 114 | this.videoService.updateVideo(this.video) |
40e87e9e C |
115 | .pipe( |
116 | // Then update captions | |
117 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
118 | ) | |
119 | .subscribe( | |
120 | () => { | |
772d5642 | 121 | this.updateDone = true |
40e87e9e C |
122 | this.isUpdatingVideo = false |
123 | this.loadingBar.complete() | |
f8b2c1b4 | 124 | this.notifier.success(this.i18n('Video updated.')) |
40e87e9e C |
125 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
126 | }, | |
127 | ||
128 | err => { | |
0f7fedc3 | 129 | this.loadingBar.complete() |
40e87e9e | 130 | this.isUpdatingVideo = false |
f8b2c1b4 | 131 | this.notifier.error(err.message) |
40e87e9e C |
132 | console.error(err) |
133 | } | |
134 | ) | |
dc8bc31b | 135 | } |
e54163c2 | 136 | |
df98563e | 137 | private hydrateFormFromVideo () { |
bbe0f064 | 138 | this.form.patchValue(this.video.toFormPatch()) |
6de36768 C |
139 | |
140 | const objects = [ | |
141 | { | |
142 | url: 'thumbnailUrl', | |
143 | name: 'thumbnailfile' | |
144 | }, | |
145 | { | |
146 | url: 'previewUrl', | |
147 | name: 'previewfile' | |
148 | } | |
149 | ] | |
150 | ||
151 | for (const obj of objects) { | |
152 | fetch(this.video[obj.url]) | |
153 | .then(response => response.blob()) | |
154 | .then(data => { | |
155 | this.form.patchValue({ | |
156 | [ obj.name ]: data | |
157 | }) | |
158 | }) | |
159 | } | |
d8e689b8 | 160 | } |
dc8bc31b | 161 | } |