]>
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' |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
63c4db6d | 6 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
db7af09b | 7 | import { ServerService } from '../../core' |
15a7387d | 8 | import { AuthService } from '../../core/auth' |
4cc66133 | 9 | import { FormReactive } from '../../shared' |
202f6b6c | 10 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 11 | import { VideoService } from '../../shared/video/video.service' |
6200d8d9 | 12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
b1d40cff | 13 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 14 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
40e87e9e | 15 | import { VideoCaptionService } from '@app/shared/video-caption' |
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 |
ff249f49 | 26 | videoPrivacies = [] |
15a7387d | 27 | userVideoChannels = [] |
bbe0f064 | 28 | schedulePublicationPossible = false |
40e87e9e | 29 | videoCaptions = [] |
dc8bc31b | 30 | |
df98563e | 31 | constructor ( |
d18d6478 | 32 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 33 | private route: ActivatedRoute, |
7ddd02c9 | 34 | private router: Router, |
6e07c3de | 35 | private notificationsService: NotificationsService, |
db7af09b | 36 | private serverService: ServerService, |
15a7387d | 37 | private videoService: VideoService, |
68e24d72 | 38 | private authService: AuthService, |
6200d8d9 | 39 | private loadingBar: LoadingBarService, |
b1d40cff | 40 | private videoChannelService: VideoChannelService, |
40e87e9e | 41 | private videoCaptionService: VideoCaptionService, |
b1d40cff | 42 | private i18n: I18n |
4b2f33f3 | 43 | ) { |
df98563e | 44 | super() |
4b2f33f3 | 45 | } |
dc8bc31b | 46 | |
df98563e | 47 | ngOnInit () { |
d18d6478 | 48 | this.buildForm({}) |
d8e689b8 | 49 | |
15a7387d | 50 | this.serverService.videoPrivaciesLoaded |
6de36768 | 51 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 52 | |
db400f44 | 53 | const uuid: string = this.route.snapshot.params[ 'uuid' ] |
2de96f4d | 54 | this.videoService.getVideo(uuid) |
db400f44 C |
55 | .pipe( |
56 | switchMap(video => { | |
57 | return this.videoService | |
58 | .loadCompleteDescription(video.descriptionPath) | |
59 | .pipe(map(description => Object.assign(video, { description }))) | |
6200d8d9 C |
60 | }), |
61 | switchMap(video => { | |
62 | return this.videoChannelService | |
ad9e39fb | 63 | .listAccountVideoChannels(video.account) |
6200d8d9 C |
64 | .pipe( |
65 | map(result => result.data), | |
74af5145 | 66 | map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))), |
6200d8d9 C |
67 | map(videoChannels => ({ video, videoChannels })) |
68 | ) | |
40e87e9e C |
69 | }), |
70 | switchMap(({ video, videoChannels }) => { | |
71 | return this.videoCaptionService | |
72 | .listCaptions(video.id) | |
73 | .pipe( | |
74 | map(result => result.data), | |
75 | map(videoCaptions => ({ video, videoChannels, videoCaptions })) | |
76 | ) | |
db400f44 C |
77 | }) |
78 | ) | |
79 | .subscribe( | |
40e87e9e | 80 | ({ video, videoChannels, videoCaptions }) => { |
db400f44 | 81 | this.video = new VideoEdit(video) |
6200d8d9 | 82 | this.userVideoChannels = videoChannels |
40e87e9e | 83 | this.videoCaptions = videoCaptions |
db400f44 C |
84 | |
85 | // We cannot set private a video that was not private | |
bbe0f064 | 86 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { |
329d9086 | 87 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString()) |
bbe0f064 C |
88 | } else { // We can schedule video publication only if it it is private |
89 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
fd45e8f4 C |
90 | } |
91 | ||
db400f44 C |
92 | this.hydrateFormFromVideo() |
93 | }, | |
2de96f4d | 94 | |
db400f44 C |
95 | err => { |
96 | console.error(err) | |
b1d40cff | 97 | this.notificationsService.error(this.i18n('Error'), err.message) |
db400f44 C |
98 | } |
99 | ) | |
e822fdae C |
100 | } |
101 | ||
df98563e C |
102 | checkForm () { |
103 | this.forceCheck() | |
c24ac1c1 | 104 | |
df98563e | 105 | return this.form.valid |
c24ac1c1 C |
106 | } |
107 | ||
df98563e | 108 | update () { |
c24ac1c1 | 109 | if (this.checkForm() === false) { |
df98563e | 110 | return |
c24ac1c1 C |
111 | } |
112 | ||
df98563e | 113 | this.video.patch(this.form.value) |
d8e689b8 | 114 | |
68e24d72 C |
115 | this.loadingBar.start() |
116 | this.isUpdatingVideo = true | |
40e87e9e C |
117 | |
118 | // Update the video | |
d8e689b8 | 119 | this.videoService.updateVideo(this.video) |
40e87e9e C |
120 | .pipe( |
121 | // Then update captions | |
122 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
123 | ) | |
124 | .subscribe( | |
125 | () => { | |
126 | this.isUpdatingVideo = false | |
127 | this.loadingBar.complete() | |
128 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) | |
129 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
130 | }, | |
131 | ||
132 | err => { | |
133 | this.isUpdatingVideo = false | |
134 | this.notificationsService.error(this.i18n('Error'), err.message) | |
135 | console.error(err) | |
136 | } | |
137 | ) | |
e822fdae | 138 | |
dc8bc31b | 139 | } |
e54163c2 | 140 | |
df98563e | 141 | private hydrateFormFromVideo () { |
bbe0f064 | 142 | this.form.patchValue(this.video.toFormPatch()) |
6de36768 C |
143 | |
144 | const objects = [ | |
145 | { | |
146 | url: 'thumbnailUrl', | |
147 | name: 'thumbnailfile' | |
148 | }, | |
149 | { | |
150 | url: 'previewUrl', | |
151 | name: 'previewfile' | |
152 | } | |
153 | ] | |
154 | ||
155 | for (const obj of objects) { | |
156 | fetch(this.video[obj.url]) | |
157 | .then(response => response.blob()) | |
158 | .then(data => { | |
159 | this.form.patchValue({ | |
160 | [ obj.name ]: data | |
161 | }) | |
162 | }) | |
163 | } | |
d8e689b8 | 164 | } |
dc8bc31b | 165 | } |