]>
Commit | Line | Data |
---|---|---|
1 | import { map, switchMap } from 'rxjs/operators' | |
2 | import { Component, OnInit } from '@angular/core' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
4 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
5 | import { NotificationsService } from 'angular2-notifications' | |
6 | import { VideoPrivacy } from '../../../../../shared/models/videos' | |
7 | import { ServerService } from '../../core' | |
8 | import { AuthService } from '../../core/auth' | |
9 | import { FormReactive } from '../../shared' | |
10 | import { VideoEdit } from '../../shared/video/video-edit.model' | |
11 | import { VideoService } from '../../shared/video/video.service' | |
12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
13 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
14 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
15 | import { VideoCaptionService } from '@app/shared/video-caption' | |
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 = [] | |
27 | userVideoChannels = [] | |
28 | schedulePublicationPossible = false | |
29 | videoCaptions = [] | |
30 | ||
31 | constructor ( | |
32 | protected formValidatorService: FormValidatorService, | |
33 | private route: ActivatedRoute, | |
34 | private router: Router, | |
35 | private notificationsService: NotificationsService, | |
36 | private serverService: ServerService, | |
37 | private videoService: VideoService, | |
38 | private authService: AuthService, | |
39 | private loadingBar: LoadingBarService, | |
40 | private videoChannelService: VideoChannelService, | |
41 | private videoCaptionService: VideoCaptionService, | |
42 | private i18n: I18n | |
43 | ) { | |
44 | super() | |
45 | } | |
46 | ||
47 | ngOnInit () { | |
48 | this.buildForm({}) | |
49 | ||
50 | this.serverService.videoPrivaciesLoaded | |
51 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) | |
52 | ||
53 | const uuid: string = this.route.snapshot.params[ 'uuid' ] | |
54 | this.videoService.getVideo(uuid) | |
55 | .pipe( | |
56 | switchMap(video => { | |
57 | return this.videoService | |
58 | .loadCompleteDescription(video.descriptionPath) | |
59 | .pipe(map(description => Object.assign(video, { description }))) | |
60 | }), | |
61 | switchMap(video => { | |
62 | return this.videoChannelService | |
63 | .listAccountVideoChannels(video.account) | |
64 | .pipe( | |
65 | map(result => result.data), | |
66 | map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))), | |
67 | map(videoChannels => ({ video, videoChannels })) | |
68 | ) | |
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 | ) | |
77 | }) | |
78 | ) | |
79 | .subscribe( | |
80 | ({ video, videoChannels, videoCaptions }) => { | |
81 | this.video = new VideoEdit(video) | |
82 | this.userVideoChannels = videoChannels | |
83 | this.videoCaptions = videoCaptions | |
84 | ||
85 | // We cannot set private a video that was not private | |
86 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { | |
87 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE) | |
88 | } else { // We can schedule video publication only if it it is private | |
89 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
90 | } | |
91 | ||
92 | this.hydrateFormFromVideo() | |
93 | }, | |
94 | ||
95 | err => { | |
96 | console.error(err) | |
97 | this.notificationsService.error(this.i18n('Error'), err.message) | |
98 | } | |
99 | ) | |
100 | } | |
101 | ||
102 | checkForm () { | |
103 | this.forceCheck() | |
104 | ||
105 | return this.form.valid | |
106 | } | |
107 | ||
108 | update () { | |
109 | if (this.checkForm() === false) { | |
110 | return | |
111 | } | |
112 | ||
113 | this.video.patch(this.form.value) | |
114 | ||
115 | this.loadingBar.start() | |
116 | this.isUpdatingVideo = true | |
117 | ||
118 | // Update the video | |
119 | this.videoService.updateVideo(this.video) | |
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 | ) | |
138 | ||
139 | } | |
140 | ||
141 | private hydrateFormFromVideo () { | |
142 | this.form.patchValue(this.video.toFormPatch()) | |
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 | } | |
164 | } | |
165 | } |