]>
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>() |
2f5d2ec5 | 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 () { | |
758f0d19 | 73 | return this.serverConfig.video.file.extensions.join(', ') |
fbad87b0 C |
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 | ||
c9ff8a08 RK |
111 | setVideoFile (files: FileList) { |
112 | this.videofileInput.nativeElement.files = files | |
113 | this.fileChange() | |
114 | } | |
115 | ||
7b992a86 C |
116 | getAudioUploadLabel () { |
117 | const videofile = this.getVideoFile() | |
118 | if (!videofile) return this.i18n('Upload') | |
119 | ||
120 | return this.i18n('Upload {{videofileName}}', { videofileName: videofile.name }) | |
121 | } | |
122 | ||
fbad87b0 C |
123 | fileChange () { |
124 | this.uploadFirstStep() | |
125 | } | |
126 | ||
fbad87b0 C |
127 | cancelUpload () { |
128 | if (this.videoUploadObservable !== null) { | |
129 | this.videoUploadObservable.unsubscribe() | |
e713698f | 130 | |
fbad87b0 C |
131 | this.isUploadingVideo = false |
132 | this.videoUploadPercents = 0 | |
133 | this.videoUploadObservable = null | |
e713698f C |
134 | |
135 | this.firstStepError.emit() | |
136 | ||
f8b2c1b4 | 137 | this.notifier.info(this.i18n('Upload cancelled')) |
fbad87b0 C |
138 | } |
139 | } | |
140 | ||
7b992a86 C |
141 | uploadFirstStep (clickedOnButton = false) { |
142 | const videofile = this.getVideoFile() | |
fbad87b0 C |
143 | if (!videofile) return |
144 | ||
7b992a86 C |
145 | if (!this.checkGlobalUserQuota(videofile)) return |
146 | if (!this.checkDailyUserQuota(videofile)) return | |
fbad87b0 | 147 | |
7b992a86 C |
148 | if (clickedOnButton === false && this.isAudioFile(videofile.name)) { |
149 | this.isUploadingAudioFile = true | |
bee0abff FA |
150 | return |
151 | } | |
152 | ||
14e2014a | 153 | // Build name field |
fbad87b0 C |
154 | const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '') |
155 | let name: string | |
156 | ||
157 | // If the name of the file is very small, keep the extension | |
158 | if (nameWithoutExtension.length < 3) name = videofile.name | |
159 | else name = nameWithoutExtension | |
160 | ||
14e2014a C |
161 | // Force user to wait transcoding for unsupported video types in web browsers |
162 | if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) { | |
163 | this.waitTranscodingEnabled = false | |
164 | } | |
165 | ||
fbad87b0 | 166 | const privacy = this.firstStepPrivacyId.toString() |
ba430d75 | 167 | const nsfw = this.serverConfig.instance.isNSFW |
fbad87b0 C |
168 | const waitTranscoding = true |
169 | const commentsEnabled = true | |
7f2cfe3a | 170 | const downloadEnabled = true |
fbad87b0 C |
171 | const channelId = this.firstStepChannelId.toString() |
172 | ||
173 | const formData = new FormData() | |
174 | formData.append('name', name) | |
175 | // Put the video "private" -> we are waiting the user validation of the second step | |
176 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
177 | formData.append('nsfw', '' + nsfw) | |
178 | formData.append('commentsEnabled', '' + commentsEnabled) | |
7f2cfe3a | 179 | formData.append('downloadEnabled', '' + downloadEnabled) |
fbad87b0 C |
180 | formData.append('waitTranscoding', '' + waitTranscoding) |
181 | formData.append('channelId', '' + channelId) | |
182 | formData.append('videofile', videofile) | |
183 | ||
7b992a86 C |
184 | if (this.previewfileUpload) { |
185 | formData.append('previewfile', this.previewfileUpload) | |
186 | formData.append('thumbnailfile', this.previewfileUpload) | |
187 | } | |
188 | ||
fbad87b0 C |
189 | this.isUploadingVideo = true |
190 | this.firstStepDone.emit(name) | |
191 | ||
192 | this.form.patchValue({ | |
193 | name, | |
194 | privacy, | |
195 | nsfw, | |
7b992a86 C |
196 | channelId, |
197 | previewfile: this.previewfileUpload | |
fbad87b0 C |
198 | }) |
199 | ||
200 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( | |
201 | event => { | |
202 | if (event.type === HttpEventType.UploadProgress) { | |
203 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) | |
204 | } else if (event instanceof HttpResponse) { | |
205 | this.videoUploaded = true | |
206 | ||
207 | this.videoUploadedIds = event.body.video | |
208 | ||
209 | this.videoUploadObservable = null | |
210 | } | |
211 | }, | |
212 | ||
213 | err => { | |
214 | // Reset progress | |
215 | this.isUploadingVideo = false | |
216 | this.videoUploadPercents = 0 | |
217 | this.videoUploadObservable = null | |
7373507f | 218 | this.firstStepError.emit() |
f8b2c1b4 | 219 | this.notifier.error(err.message) |
fbad87b0 C |
220 | } |
221 | ) | |
222 | } | |
223 | ||
59c9c5d9 C |
224 | isPublishingButtonDisabled () { |
225 | return !this.form.valid || | |
226 | this.isUpdatingVideo === true || | |
227 | this.videoUploaded !== true | |
228 | } | |
229 | ||
fbad87b0 C |
230 | updateSecondStep () { |
231 | if (this.checkForm() === false) { | |
232 | return | |
233 | } | |
234 | ||
235 | const video = new VideoEdit() | |
236 | video.patch(this.form.value) | |
237 | video.id = this.videoUploadedIds.id | |
238 | video.uuid = this.videoUploadedIds.uuid | |
239 | ||
240 | this.isUpdatingVideo = true | |
43620009 C |
241 | |
242 | this.updateVideoAndCaptions(video) | |
fbad87b0 C |
243 | .subscribe( |
244 | () => { | |
245 | this.isUpdatingVideo = false | |
246 | this.isUploadingVideo = false | |
fbad87b0 | 247 | |
f8b2c1b4 | 248 | this.notifier.success(this.i18n('Video published.')) |
fbad87b0 C |
249 | this.router.navigate([ '/videos/watch', video.uuid ]) |
250 | }, | |
251 | ||
252 | err => { | |
7373507f C |
253 | this.error = err.message |
254 | scrollToTop() | |
fbad87b0 C |
255 | console.error(err) |
256 | } | |
257 | ) | |
258 | } | |
7b992a86 C |
259 | |
260 | private checkGlobalUserQuota (videofile: File) { | |
261 | const bytePipes = new BytesPipe() | |
262 | ||
263 | // Check global user quota | |
264 | const videoQuota = this.authService.getUser().videoQuota | |
265 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
266 | const msg = this.i18n( | |
267 | 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})', | |
268 | { | |
269 | videoSize: bytePipes.transform(videofile.size, 0), | |
270 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
271 | videoQuota: bytePipes.transform(videoQuota, 0) | |
272 | } | |
273 | ) | |
274 | this.notifier.error(msg) | |
275 | ||
276 | return false | |
277 | } | |
278 | ||
279 | return true | |
280 | } | |
281 | ||
282 | private checkDailyUserQuota (videofile: File) { | |
283 | const bytePipes = new BytesPipe() | |
284 | ||
285 | // Check daily user quota | |
286 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily | |
287 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
288 | const msg = this.i18n( | |
289 | 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})', | |
290 | { | |
291 | videoSize: bytePipes.transform(videofile.size, 0), | |
292 | quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0), | |
293 | quotaDaily: bytePipes.transform(videoQuotaDaily, 0) | |
294 | } | |
295 | ) | |
296 | this.notifier.error(msg) | |
297 | ||
298 | return false | |
299 | } | |
300 | ||
301 | return true | |
302 | } | |
303 | ||
304 | private isAudioFile (filename: string) { | |
99d362de C |
305 | const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ] |
306 | ||
307 | return extensions.some(e => filename.endsWith(e)) | |
7b992a86 | 308 | } |
fbad87b0 | 309 | } |