]>
Commit | Line | Data |
---|---|---|
fbad87b0 | 1 | import { HttpEventType, HttpResponse } from '@angular/common/http' |
c199c427 | 2 | import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core' |
fbad87b0 | 3 | import { Router } from '@angular/router' |
fbad87b0 | 4 | import { LoadingBarService } from '@ngx-loading-bar/core' |
fbad87b0 C |
5 | import { BytesPipe } from 'ngx-pipes' |
6 | import { Subscription } from 'rxjs' | |
78848714 | 7 | import { VideoPrivacy } from '../../../../../../shared/models/videos' |
f8b2c1b4 | 8 | import { AuthService, Notifier, ServerService } from '../../../core' |
78848714 C |
9 | import { VideoEdit } from '../../../shared/video/video-edit.model' |
10 | import { VideoService } from '../../../shared/video/video.service' | |
fbad87b0 | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
78848714 C |
12 | import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send' |
13 | import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service' | |
14 | import { FormValidatorService, UserService } from '@app/shared' | |
fbad87b0 | 15 | import { VideoCaptionService } from '@app/shared/video-caption' |
7373507f | 16 | import { scrollToTop } from '@app/shared/misc/utils' |
fbad87b0 C |
17 | |
18 | @Component({ | |
19 | selector: 'my-video-upload', | |
20 | templateUrl: './video-upload.component.html', | |
21 | styleUrls: [ | |
78848714 | 22 | '../shared/video-edit.component.scss', |
fbad87b0 C |
23 | './video-upload.component.scss' |
24 | ] | |
25 | }) | |
43620009 | 26 | export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate { |
fbad87b0 | 27 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 28 | @Output() firstStepError = new EventEmitter<void>() |
c199c427 | 29 | @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement> |
fbad87b0 C |
30 | |
31 | // So that it can be accessed in the template | |
32 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
33 | ||
43620009 | 34 | userVideoQuotaUsed = 0 |
bee0abff | 35 | userVideoQuotaUsedDaily = 0 |
43620009 | 36 | |
fbad87b0 C |
37 | isUploadingVideo = false |
38 | isUpdatingVideo = false | |
39 | videoUploaded = false | |
40 | videoUploadObservable: Subscription = null | |
41 | videoUploadPercents = 0 | |
42 | videoUploadedIds = { | |
43 | id: 0, | |
44 | uuid: '' | |
45 | } | |
14e2014a | 46 | waitTranscodingEnabled = true |
fbad87b0 | 47 | |
7373507f C |
48 | error: string |
49 | ||
43620009 | 50 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
fbad87b0 C |
51 | |
52 | constructor ( | |
53 | protected formValidatorService: FormValidatorService, | |
43620009 | 54 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 55 | protected notifier: Notifier, |
43620009 C |
56 | protected authService: AuthService, |
57 | protected serverService: ServerService, | |
58 | protected videoService: VideoService, | |
59 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 60 | private userService: UserService, |
43620009 C |
61 | private router: Router, |
62 | private i18n: I18n | |
fbad87b0 C |
63 | ) { |
64 | super() | |
65 | } | |
66 | ||
67 | get videoExtensions () { | |
68 | return this.serverService.getConfig().video.file.extensions.join(',') | |
69 | } | |
70 | ||
71 | ngOnInit () { | |
43620009 | 72 | super.ngOnInit() |
fbad87b0 C |
73 | |
74 | this.userService.getMyVideoQuotaUsed() | |
348106f2 C |
75 | .subscribe(data => { |
76 | this.userVideoQuotaUsed = data.videoQuotaUsed | |
77 | this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily | |
78 | }) | |
fbad87b0 C |
79 | } |
80 | ||
81 | ngOnDestroy () { | |
43620009 | 82 | if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe() |
fbad87b0 C |
83 | } |
84 | ||
85 | canDeactivate () { | |
86 | let text = '' | |
87 | ||
88 | if (this.videoUploaded === true) { | |
89 | // FIXME: cannot concatenate strings inside i18n service :/ | |
8c2b9756 | 90 | text = this.i18n('Your video was uploaded to your account and is private.') + ' ' + |
fbad87b0 C |
91 | this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?') |
92 | } else { | |
93 | text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?') | |
94 | } | |
95 | ||
96 | return { | |
97 | canDeactivate: !this.isUploadingVideo, | |
98 | text | |
99 | } | |
100 | } | |
101 | ||
102 | fileChange () { | |
103 | this.uploadFirstStep() | |
104 | } | |
105 | ||
fbad87b0 C |
106 | cancelUpload () { |
107 | if (this.videoUploadObservable !== null) { | |
108 | this.videoUploadObservable.unsubscribe() | |
109 | this.isUploadingVideo = false | |
110 | this.videoUploadPercents = 0 | |
111 | this.videoUploadObservable = null | |
f8b2c1b4 | 112 | this.notifier.info(this.i18n('Upload cancelled')) |
fbad87b0 C |
113 | } |
114 | } | |
115 | ||
116 | uploadFirstStep () { | |
c199c427 | 117 | const videofile = this.videofileInput.nativeElement.files[0] |
fbad87b0 C |
118 | if (!videofile) return |
119 | ||
14e2014a | 120 | // Check global user quota |
bee0abff | 121 | const bytePipes = new BytesPipe() |
fbad87b0 C |
122 | const videoQuota = this.authService.getUser().videoQuota |
123 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
fbad87b0 | 124 | const msg = this.i18n( |
d972dc7f | 125 | 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})', |
fbad87b0 C |
126 | { |
127 | videoSize: bytePipes.transform(videofile.size, 0), | |
128 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
129 | videoQuota: bytePipes.transform(videoQuota, 0) | |
130 | } | |
131 | ) | |
f8b2c1b4 | 132 | this.notifier.error(msg) |
fbad87b0 C |
133 | return |
134 | } | |
135 | ||
14e2014a | 136 | // Check daily user quota |
bee0abff FA |
137 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily |
138 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
139 | const msg = this.i18n( | |
d972dc7f | 140 | 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})', |
bee0abff FA |
141 | { |
142 | videoSize: bytePipes.transform(videofile.size, 0), | |
d972dc7f C |
143 | quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0), |
144 | quotaDaily: bytePipes.transform(videoQuotaDaily, 0) | |
bee0abff FA |
145 | } |
146 | ) | |
f8b2c1b4 | 147 | this.notifier.error(msg) |
bee0abff FA |
148 | return |
149 | } | |
150 | ||
14e2014a | 151 | // Build name field |
fbad87b0 C |
152 | const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '') |
153 | let name: string | |
154 | ||
155 | // If the name of the file is very small, keep the extension | |
156 | if (nameWithoutExtension.length < 3) name = videofile.name | |
157 | else name = nameWithoutExtension | |
158 | ||
14e2014a C |
159 | // Force user to wait transcoding for unsupported video types in web browsers |
160 | if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) { | |
161 | this.waitTranscodingEnabled = false | |
162 | } | |
163 | ||
fbad87b0 C |
164 | const privacy = this.firstStepPrivacyId.toString() |
165 | const nsfw = false | |
166 | const waitTranscoding = true | |
167 | const commentsEnabled = true | |
168 | const channelId = this.firstStepChannelId.toString() | |
169 | ||
170 | const formData = new FormData() | |
171 | formData.append('name', name) | |
172 | // Put the video "private" -> we are waiting the user validation of the second step | |
173 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
174 | formData.append('nsfw', '' + nsfw) | |
175 | formData.append('commentsEnabled', '' + commentsEnabled) | |
176 | formData.append('waitTranscoding', '' + waitTranscoding) | |
177 | formData.append('channelId', '' + channelId) | |
178 | formData.append('videofile', videofile) | |
179 | ||
180 | this.isUploadingVideo = true | |
181 | this.firstStepDone.emit(name) | |
182 | ||
183 | this.form.patchValue({ | |
184 | name, | |
185 | privacy, | |
186 | nsfw, | |
187 | channelId | |
188 | }) | |
189 | ||
8cd7faaa C |
190 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies) |
191 | ||
fbad87b0 C |
192 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( |
193 | event => { | |
194 | if (event.type === HttpEventType.UploadProgress) { | |
195 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) | |
196 | } else if (event instanceof HttpResponse) { | |
197 | this.videoUploaded = true | |
198 | ||
199 | this.videoUploadedIds = event.body.video | |
200 | ||
201 | this.videoUploadObservable = null | |
202 | } | |
203 | }, | |
204 | ||
205 | err => { | |
206 | // Reset progress | |
207 | this.isUploadingVideo = false | |
208 | this.videoUploadPercents = 0 | |
209 | this.videoUploadObservable = null | |
7373507f | 210 | this.firstStepError.emit() |
f8b2c1b4 | 211 | this.notifier.error(err.message) |
fbad87b0 C |
212 | } |
213 | ) | |
214 | } | |
215 | ||
59c9c5d9 C |
216 | isPublishingButtonDisabled () { |
217 | return !this.form.valid || | |
218 | this.isUpdatingVideo === true || | |
219 | this.videoUploaded !== true | |
220 | } | |
221 | ||
fbad87b0 C |
222 | updateSecondStep () { |
223 | if (this.checkForm() === false) { | |
224 | return | |
225 | } | |
226 | ||
227 | const video = new VideoEdit() | |
228 | video.patch(this.form.value) | |
229 | video.id = this.videoUploadedIds.id | |
230 | video.uuid = this.videoUploadedIds.uuid | |
231 | ||
232 | this.isUpdatingVideo = true | |
43620009 C |
233 | |
234 | this.updateVideoAndCaptions(video) | |
fbad87b0 C |
235 | .subscribe( |
236 | () => { | |
237 | this.isUpdatingVideo = false | |
238 | this.isUploadingVideo = false | |
fbad87b0 | 239 | |
f8b2c1b4 | 240 | this.notifier.success(this.i18n('Video published.')) |
fbad87b0 C |
241 | this.router.navigate([ '/videos/watch', video.uuid ]) |
242 | }, | |
243 | ||
244 | err => { | |
7373507f C |
245 | this.error = err.message |
246 | scrollToTop() | |
fbad87b0 C |
247 | console.error(err) |
248 | } | |
249 | ) | |
250 | } | |
251 | } |