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