]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { BytesPipe } from 'ngx-pipes' |
2 | import { Subscription } from 'rxjs' | |
fbad87b0 | 3 | import { HttpEventType, HttpResponse } from '@angular/common/http' |
c199c427 | 4 | import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core' |
fbad87b0 | 5 | import { Router } from '@angular/router' |
67ed6552 C |
6 | import { AuthService, CanComponentDeactivate, Notifier, ServerService, UserService } from '@app/core' |
7 | import { scrollToTop } from '@app/helpers' | |
8 | import { FormValidatorService } from '@app/shared/shared-forms' | |
9 | import { VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main' | |
fbad87b0 | 10 | import { LoadingBarService } from '@ngx-loading-bar/core' |
fbad87b0 | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 | 12 | import { VideoPrivacy } from '@shared/models' |
1942f11d | 13 | import { VideoSend } from './video-send' |
fbad87b0 C |
14 | |
15 | @Component({ | |
16 | selector: 'my-video-upload', | |
17 | templateUrl: './video-upload.component.html', | |
18 | styleUrls: [ | |
78848714 | 19 | '../shared/video-edit.component.scss', |
457bb213 C |
20 | './video-upload.component.scss', |
21 | './video-send.scss' | |
fbad87b0 C |
22 | ] |
23 | }) | |
43620009 | 24 | export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate { |
fbad87b0 | 25 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 26 | @Output() firstStepError = new EventEmitter<void>() |
2f5d2ec5 | 27 | @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement> |
fbad87b0 C |
28 | |
29 | // So that it can be accessed in the template | |
30 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
31 | ||
43620009 | 32 | userVideoQuotaUsed = 0 |
bee0abff | 33 | userVideoQuotaUsedDaily = 0 |
43620009 | 34 | |
7b992a86 | 35 | isUploadingAudioFile = false |
fbad87b0 C |
36 | isUploadingVideo = false |
37 | isUpdatingVideo = false | |
7b992a86 | 38 | |
fbad87b0 C |
39 | videoUploaded = false |
40 | videoUploadObservable: Subscription = null | |
41 | videoUploadPercents = 0 | |
42 | videoUploadedIds = { | |
43 | id: 0, | |
44 | uuid: '' | |
45 | } | |
7b992a86 | 46 | |
14e2014a | 47 | waitTranscodingEnabled = true |
7b992a86 | 48 | previewfileUpload: File |
fbad87b0 | 49 | |
7373507f | 50 | error: string |
fbad87b0 | 51 | |
43620009 | 52 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
fbad87b0 C |
53 | |
54 | constructor ( | |
55 | protected formValidatorService: FormValidatorService, | |
43620009 | 56 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 57 | protected notifier: Notifier, |
43620009 C |
58 | protected authService: AuthService, |
59 | protected serverService: ServerService, | |
60 | protected videoService: VideoService, | |
61 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 62 | private userService: UserService, |
43620009 C |
63 | private router: Router, |
64 | private i18n: I18n | |
fbad87b0 C |
65 | ) { |
66 | super() | |
67 | } | |
68 | ||
69 | get videoExtensions () { | |
758f0d19 | 70 | return this.serverConfig.video.file.extensions.join(', ') |
fbad87b0 C |
71 | } |
72 | ||
73 | ngOnInit () { | |
43620009 | 74 | super.ngOnInit() |
fbad87b0 C |
75 | |
76 | this.userService.getMyVideoQuotaUsed() | |
348106f2 C |
77 | .subscribe(data => { |
78 | this.userVideoQuotaUsed = data.videoQuotaUsed | |
79 | this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily | |
80 | }) | |
fbad87b0 C |
81 | } |
82 | ||
83 | ngOnDestroy () { | |
43620009 | 84 | if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe() |
fbad87b0 C |
85 | } |
86 | ||
87 | canDeactivate () { | |
88 | let text = '' | |
89 | ||
90 | if (this.videoUploaded === true) { | |
91 | // FIXME: cannot concatenate strings inside i18n service :/ | |
8c2b9756 | 92 | text = this.i18n('Your video was uploaded to your account and is private.') + ' ' + |
fbad87b0 C |
93 | this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?') |
94 | } else { | |
95 | text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?') | |
96 | } | |
97 | ||
98 | return { | |
99 | canDeactivate: !this.isUploadingVideo, | |
100 | text | |
101 | } | |
102 | } | |
103 | ||
7b992a86 C |
104 | getVideoFile () { |
105 | return this.videofileInput.nativeElement.files[0] | |
106 | } | |
107 | ||
c9ff8a08 RK |
108 | setVideoFile (files: FileList) { |
109 | this.videofileInput.nativeElement.files = files | |
110 | this.fileChange() | |
111 | } | |
112 | ||
7b992a86 C |
113 | getAudioUploadLabel () { |
114 | const videofile = this.getVideoFile() | |
115 | if (!videofile) return this.i18n('Upload') | |
116 | ||
117 | return this.i18n('Upload {{videofileName}}', { videofileName: videofile.name }) | |
118 | } | |
119 | ||
fbad87b0 C |
120 | fileChange () { |
121 | this.uploadFirstStep() | |
122 | } | |
123 | ||
fbad87b0 C |
124 | cancelUpload () { |
125 | if (this.videoUploadObservable !== null) { | |
126 | this.videoUploadObservable.unsubscribe() | |
e713698f | 127 | |
fbad87b0 C |
128 | this.isUploadingVideo = false |
129 | this.videoUploadPercents = 0 | |
130 | this.videoUploadObservable = null | |
e713698f C |
131 | |
132 | this.firstStepError.emit() | |
133 | ||
f8b2c1b4 | 134 | this.notifier.info(this.i18n('Upload cancelled')) |
fbad87b0 C |
135 | } |
136 | } | |
137 | ||
7b992a86 C |
138 | uploadFirstStep (clickedOnButton = false) { |
139 | const videofile = this.getVideoFile() | |
fbad87b0 C |
140 | if (!videofile) return |
141 | ||
7b992a86 C |
142 | if (!this.checkGlobalUserQuota(videofile)) return |
143 | if (!this.checkDailyUserQuota(videofile)) return | |
fbad87b0 | 144 | |
7b992a86 C |
145 | if (clickedOnButton === false && this.isAudioFile(videofile.name)) { |
146 | this.isUploadingAudioFile = true | |
bee0abff FA |
147 | return |
148 | } | |
149 | ||
14e2014a | 150 | // Build name field |
fbad87b0 C |
151 | const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '') |
152 | let name: string | |
153 | ||
154 | // If the name of the file is very small, keep the extension | |
155 | if (nameWithoutExtension.length < 3) name = videofile.name | |
156 | else name = nameWithoutExtension | |
157 | ||
14e2014a C |
158 | // Force user to wait transcoding for unsupported video types in web browsers |
159 | if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) { | |
160 | this.waitTranscodingEnabled = false | |
161 | } | |
162 | ||
fbad87b0 | 163 | const privacy = this.firstStepPrivacyId.toString() |
ba430d75 | 164 | const nsfw = this.serverConfig.instance.isNSFW |
fbad87b0 C |
165 | const waitTranscoding = true |
166 | const commentsEnabled = true | |
7f2cfe3a | 167 | const downloadEnabled = true |
fbad87b0 C |
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) | |
7f2cfe3a | 176 | formData.append('downloadEnabled', '' + downloadEnabled) |
fbad87b0 C |
177 | formData.append('waitTranscoding', '' + waitTranscoding) |
178 | formData.append('channelId', '' + channelId) | |
179 | formData.append('videofile', videofile) | |
180 | ||
7b992a86 C |
181 | if (this.previewfileUpload) { |
182 | formData.append('previewfile', this.previewfileUpload) | |
183 | formData.append('thumbnailfile', this.previewfileUpload) | |
184 | } | |
185 | ||
fbad87b0 C |
186 | this.isUploadingVideo = true |
187 | this.firstStepDone.emit(name) | |
188 | ||
189 | this.form.patchValue({ | |
190 | name, | |
191 | privacy, | |
192 | nsfw, | |
7b992a86 C |
193 | channelId, |
194 | previewfile: this.previewfileUpload | |
fbad87b0 C |
195 | }) |
196 | ||
197 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( | |
198 | event => { | |
199 | if (event.type === HttpEventType.UploadProgress) { | |
200 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) | |
201 | } else if (event instanceof HttpResponse) { | |
202 | this.videoUploaded = true | |
203 | ||
204 | this.videoUploadedIds = event.body.video | |
205 | ||
206 | this.videoUploadObservable = null | |
207 | } | |
208 | }, | |
209 | ||
210 | err => { | |
211 | // Reset progress | |
212 | this.isUploadingVideo = false | |
213 | this.videoUploadPercents = 0 | |
214 | this.videoUploadObservable = null | |
7373507f | 215 | this.firstStepError.emit() |
f8b2c1b4 | 216 | this.notifier.error(err.message) |
fbad87b0 C |
217 | } |
218 | ) | |
219 | } | |
220 | ||
59c9c5d9 C |
221 | isPublishingButtonDisabled () { |
222 | return !this.form.valid || | |
223 | this.isUpdatingVideo === true || | |
c7a53f61 C |
224 | this.videoUploaded !== true || |
225 | !this.videoUploadedIds.id | |
59c9c5d9 C |
226 | } |
227 | ||
fbad87b0 | 228 | updateSecondStep () { |
c7a53f61 | 229 | if (this.isPublishingButtonDisabled() || !this.checkForm()) { |
fbad87b0 C |
230 | return |
231 | } | |
232 | ||
233 | const video = new VideoEdit() | |
234 | video.patch(this.form.value) | |
235 | video.id = this.videoUploadedIds.id | |
236 | video.uuid = this.videoUploadedIds.uuid | |
237 | ||
238 | this.isUpdatingVideo = true | |
43620009 C |
239 | |
240 | this.updateVideoAndCaptions(video) | |
fbad87b0 C |
241 | .subscribe( |
242 | () => { | |
243 | this.isUpdatingVideo = false | |
244 | this.isUploadingVideo = false | |
fbad87b0 | 245 | |
f8b2c1b4 | 246 | this.notifier.success(this.i18n('Video published.')) |
fbad87b0 C |
247 | this.router.navigate([ '/videos/watch', video.uuid ]) |
248 | }, | |
249 | ||
250 | err => { | |
7373507f C |
251 | this.error = err.message |
252 | scrollToTop() | |
fbad87b0 C |
253 | console.error(err) |
254 | } | |
255 | ) | |
256 | } | |
7b992a86 C |
257 | |
258 | private checkGlobalUserQuota (videofile: File) { | |
259 | const bytePipes = new BytesPipe() | |
260 | ||
261 | // Check global user quota | |
262 | const videoQuota = this.authService.getUser().videoQuota | |
263 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
264 | const msg = this.i18n( | |
265 | 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})', | |
266 | { | |
267 | videoSize: bytePipes.transform(videofile.size, 0), | |
268 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
269 | videoQuota: bytePipes.transform(videoQuota, 0) | |
270 | } | |
271 | ) | |
272 | this.notifier.error(msg) | |
273 | ||
274 | return false | |
275 | } | |
276 | ||
277 | return true | |
278 | } | |
279 | ||
280 | private checkDailyUserQuota (videofile: File) { | |
281 | const bytePipes = new BytesPipe() | |
282 | ||
283 | // Check daily user quota | |
284 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily | |
285 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
286 | const msg = this.i18n( | |
287 | 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})', | |
288 | { | |
289 | videoSize: bytePipes.transform(videofile.size, 0), | |
290 | quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0), | |
291 | quotaDaily: bytePipes.transform(videoQuotaDaily, 0) | |
292 | } | |
293 | ) | |
294 | this.notifier.error(msg) | |
295 | ||
296 | return false | |
297 | } | |
298 | ||
299 | return true | |
300 | } | |
301 | ||
302 | private isAudioFile (filename: string) { | |
99d362de C |
303 | const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ] |
304 | ||
305 | return extensions.some(e => filename.endsWith(e)) | |
7b992a86 | 306 | } |
fbad87b0 | 307 | } |