]>
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' |
ba430d75 | 17 | import { ServerConfig } from '@shared/models' |
fbad87b0 C |
18 | |
19 | @Component({ | |
20 | selector: 'my-video-upload', | |
21 | templateUrl: './video-upload.component.html', | |
22 | styleUrls: [ | |
78848714 | 23 | '../shared/video-edit.component.scss', |
457bb213 C |
24 | './video-upload.component.scss', |
25 | './video-send.scss' | |
fbad87b0 C |
26 | ] |
27 | }) | |
43620009 | 28 | export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate { |
fbad87b0 | 29 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 30 | @Output() firstStepError = new EventEmitter<void>() |
f36da21e | 31 | @ViewChild('videofileInput', { static: false }) videofileInput: ElementRef<HTMLInputElement> |
fbad87b0 C |
32 | |
33 | // So that it can be accessed in the template | |
34 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
35 | ||
43620009 | 36 | userVideoQuotaUsed = 0 |
bee0abff | 37 | userVideoQuotaUsedDaily = 0 |
43620009 | 38 | |
7b992a86 | 39 | isUploadingAudioFile = false |
fbad87b0 C |
40 | isUploadingVideo = false |
41 | isUpdatingVideo = false | |
7b992a86 | 42 | |
fbad87b0 C |
43 | videoUploaded = false |
44 | videoUploadObservable: Subscription = null | |
45 | videoUploadPercents = 0 | |
46 | videoUploadedIds = { | |
47 | id: 0, | |
48 | uuid: '' | |
49 | } | |
7b992a86 | 50 | |
14e2014a | 51 | waitTranscodingEnabled = true |
7b992a86 | 52 | previewfileUpload: File |
fbad87b0 | 53 | |
7373507f | 54 | error: string |
fbad87b0 | 55 | |
43620009 | 56 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
fbad87b0 C |
57 | |
58 | constructor ( | |
59 | protected formValidatorService: FormValidatorService, | |
43620009 | 60 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 61 | protected notifier: Notifier, |
43620009 C |
62 | protected authService: AuthService, |
63 | protected serverService: ServerService, | |
64 | protected videoService: VideoService, | |
65 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 66 | private userService: UserService, |
43620009 C |
67 | private router: Router, |
68 | private i18n: I18n | |
fbad87b0 C |
69 | ) { |
70 | super() | |
71 | } | |
72 | ||
73 | get videoExtensions () { | |
ba430d75 | 74 | return this.serverConfig.video.file.extensions.join(',') |
fbad87b0 C |
75 | } |
76 | ||
77 | ngOnInit () { | |
43620009 | 78 | super.ngOnInit() |
fbad87b0 C |
79 | |
80 | this.userService.getMyVideoQuotaUsed() | |
348106f2 C |
81 | .subscribe(data => { |
82 | this.userVideoQuotaUsed = data.videoQuotaUsed | |
83 | this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily | |
84 | }) | |
fbad87b0 C |
85 | } |
86 | ||
87 | ngOnDestroy () { | |
43620009 | 88 | if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe() |
fbad87b0 C |
89 | } |
90 | ||
91 | canDeactivate () { | |
92 | let text = '' | |
93 | ||
94 | if (this.videoUploaded === true) { | |
95 | // FIXME: cannot concatenate strings inside i18n service :/ | |
8c2b9756 | 96 | text = this.i18n('Your video was uploaded to your account and is private.') + ' ' + |
fbad87b0 C |
97 | this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?') |
98 | } else { | |
99 | text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?') | |
100 | } | |
101 | ||
102 | return { | |
103 | canDeactivate: !this.isUploadingVideo, | |
104 | text | |
105 | } | |
106 | } | |
107 | ||
7b992a86 C |
108 | getVideoFile () { |
109 | return this.videofileInput.nativeElement.files[0] | |
110 | } | |
111 | ||
112 | getAudioUploadLabel () { | |
113 | const videofile = this.getVideoFile() | |
114 | if (!videofile) return this.i18n('Upload') | |
115 | ||
116 | return this.i18n('Upload {{videofileName}}', { videofileName: videofile.name }) | |
117 | } | |
118 | ||
fbad87b0 C |
119 | fileChange () { |
120 | this.uploadFirstStep() | |
121 | } | |
122 | ||
fbad87b0 C |
123 | cancelUpload () { |
124 | if (this.videoUploadObservable !== null) { | |
125 | this.videoUploadObservable.unsubscribe() | |
126 | this.isUploadingVideo = false | |
127 | this.videoUploadPercents = 0 | |
128 | this.videoUploadObservable = null | |
f8b2c1b4 | 129 | this.notifier.info(this.i18n('Upload cancelled')) |
fbad87b0 C |
130 | } |
131 | } | |
132 | ||
7b992a86 C |
133 | uploadFirstStep (clickedOnButton = false) { |
134 | const videofile = this.getVideoFile() | |
fbad87b0 C |
135 | if (!videofile) return |
136 | ||
7b992a86 C |
137 | if (!this.checkGlobalUserQuota(videofile)) return |
138 | if (!this.checkDailyUserQuota(videofile)) return | |
fbad87b0 | 139 | |
7b992a86 C |
140 | if (clickedOnButton === false && this.isAudioFile(videofile.name)) { |
141 | this.isUploadingAudioFile = true | |
bee0abff FA |
142 | return |
143 | } | |
144 | ||
14e2014a | 145 | // Build name field |
fbad87b0 C |
146 | const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '') |
147 | let name: string | |
148 | ||
149 | // If the name of the file is very small, keep the extension | |
150 | if (nameWithoutExtension.length < 3) name = videofile.name | |
151 | else name = nameWithoutExtension | |
152 | ||
14e2014a C |
153 | // Force user to wait transcoding for unsupported video types in web browsers |
154 | if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) { | |
155 | this.waitTranscodingEnabled = false | |
156 | } | |
157 | ||
fbad87b0 | 158 | const privacy = this.firstStepPrivacyId.toString() |
ba430d75 | 159 | const nsfw = this.serverConfig.instance.isNSFW |
fbad87b0 C |
160 | const waitTranscoding = true |
161 | const commentsEnabled = true | |
7f2cfe3a | 162 | const downloadEnabled = true |
fbad87b0 C |
163 | const channelId = this.firstStepChannelId.toString() |
164 | ||
165 | const formData = new FormData() | |
166 | formData.append('name', name) | |
167 | // Put the video "private" -> we are waiting the user validation of the second step | |
168 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
169 | formData.append('nsfw', '' + nsfw) | |
170 | formData.append('commentsEnabled', '' + commentsEnabled) | |
7f2cfe3a | 171 | formData.append('downloadEnabled', '' + downloadEnabled) |
fbad87b0 C |
172 | formData.append('waitTranscoding', '' + waitTranscoding) |
173 | formData.append('channelId', '' + channelId) | |
174 | formData.append('videofile', videofile) | |
175 | ||
7b992a86 C |
176 | if (this.previewfileUpload) { |
177 | formData.append('previewfile', this.previewfileUpload) | |
178 | formData.append('thumbnailfile', this.previewfileUpload) | |
179 | } | |
180 | ||
fbad87b0 C |
181 | this.isUploadingVideo = true |
182 | this.firstStepDone.emit(name) | |
183 | ||
184 | this.form.patchValue({ | |
185 | name, | |
186 | privacy, | |
187 | nsfw, | |
7b992a86 C |
188 | channelId, |
189 | previewfile: this.previewfileUpload | |
fbad87b0 C |
190 | }) |
191 | ||
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 | } | |
7b992a86 C |
251 | |
252 | private checkGlobalUserQuota (videofile: File) { | |
253 | const bytePipes = new BytesPipe() | |
254 | ||
255 | // Check global user quota | |
256 | const videoQuota = this.authService.getUser().videoQuota | |
257 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
258 | const msg = this.i18n( | |
259 | 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})', | |
260 | { | |
261 | videoSize: bytePipes.transform(videofile.size, 0), | |
262 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
263 | videoQuota: bytePipes.transform(videoQuota, 0) | |
264 | } | |
265 | ) | |
266 | this.notifier.error(msg) | |
267 | ||
268 | return false | |
269 | } | |
270 | ||
271 | return true | |
272 | } | |
273 | ||
274 | private checkDailyUserQuota (videofile: File) { | |
275 | const bytePipes = new BytesPipe() | |
276 | ||
277 | // Check daily user quota | |
278 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily | |
279 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
280 | const msg = this.i18n( | |
281 | 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})', | |
282 | { | |
283 | videoSize: bytePipes.transform(videofile.size, 0), | |
284 | quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0), | |
285 | quotaDaily: bytePipes.transform(videoQuotaDaily, 0) | |
286 | } | |
287 | ) | |
288 | this.notifier.error(msg) | |
289 | ||
290 | return false | |
291 | } | |
292 | ||
293 | return true | |
294 | } | |
295 | ||
296 | private isAudioFile (filename: string) { | |
297 | return filename.endsWith('.mp3') || filename.endsWith('.flac') || filename.endsWith('.ogg') | |
298 | } | |
fbad87b0 | 299 | } |