]>
Commit | Line | Data |
---|---|---|
d4a8e7a6 C |
1 | import { UploadState, UploadxOptions, UploadxService } from 'ngx-uploadx' |
2 | import { HttpErrorResponse, HttpEventType, HttpHeaders } from '@angular/common/http' | |
2e257e36 | 3 | import { AfterViewInit, Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core' |
fbad87b0 | 4 | import { Router } from '@angular/router' |
2e257e36 | 5 | import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService, UserService } from '@app/core' |
d4a8e7a6 | 6 | import { genericUploadErrorHandler, scrollToTop } from '@app/helpers' |
67ed6552 | 7 | import { FormValidatorService } from '@app/shared/shared-forms' |
d4a8e7a6 | 8 | import { BytesPipe, Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main' |
fbad87b0 | 9 | import { LoadingBarService } from '@ngx-loading-bar/core' |
2e257e36 | 10 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
67ed6552 | 11 | import { VideoPrivacy } from '@shared/models' |
d4a8e7a6 | 12 | import { UploaderXFormData } from './uploaderx-form-data' |
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 | }) | |
f6d6e7f8 | 24 | export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, AfterViewInit, CanComponentDeactivate { |
fbad87b0 | 25 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 26 | @Output() firstStepError = new EventEmitter<void>() |
2f5d2ec5 | 27 | @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement> |
fbad87b0 | 28 | |
43620009 | 29 | userVideoQuotaUsed = 0 |
bee0abff | 30 | userVideoQuotaUsedDaily = 0 |
43620009 | 31 | |
7b992a86 | 32 | isUploadingAudioFile = false |
fbad87b0 | 33 | isUploadingVideo = false |
7b992a86 | 34 | |
fbad87b0 | 35 | videoUploaded = false |
fbad87b0 C |
36 | videoUploadPercents = 0 |
37 | videoUploadedIds = { | |
38 | id: 0, | |
39 | uuid: '' | |
40 | } | |
d4132d3f | 41 | formData: FormData |
7b992a86 | 42 | |
7b992a86 | 43 | previewfileUpload: File |
fbad87b0 | 44 | |
7373507f | 45 | error: string |
d4132d3f | 46 | enableRetryAfterError: boolean |
fbad87b0 | 47 | |
f6d6e7f8 | 48 | // So that it can be accessed in the template |
f6d6e7f8 | 49 | protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + 'upload-resumable' |
50 | ||
51 | private uploadxOptions: UploadxOptions | |
52 | private isUpdatingVideo = false | |
53 | private fileToUpload: File | |
fbad87b0 C |
54 | |
55 | constructor ( | |
56 | protected formValidatorService: FormValidatorService, | |
43620009 | 57 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 58 | protected notifier: Notifier, |
43620009 C |
59 | protected authService: AuthService, |
60 | protected serverService: ServerService, | |
61 | protected videoService: VideoService, | |
62 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 63 | private userService: UserService, |
2e257e36 | 64 | private router: Router, |
f6d6e7f8 | 65 | private hooks: HooksService, |
66 | private resumableUploadService: UploadxService | |
67 | ) { | |
fbad87b0 | 68 | super() |
f6d6e7f8 | 69 | |
70 | this.uploadxOptions = { | |
71 | endpoint: this.BASE_VIDEO_UPLOAD_URL, | |
72 | multiple: false, | |
73 | token: this.authService.getAccessToken(), | |
74 | uploaderClass: UploaderXFormData, | |
75 | retryConfig: { | |
76 | maxAttempts: 6, | |
77 | shouldRetry: (code: number) => { | |
78 | return code < 400 || code >= 501 | |
79 | } | |
80 | } | |
81 | } | |
fbad87b0 C |
82 | } |
83 | ||
84 | get videoExtensions () { | |
758f0d19 | 85 | return this.serverConfig.video.file.extensions.join(', ') |
fbad87b0 C |
86 | } |
87 | ||
f6d6e7f8 | 88 | onUploadVideoOngoing (state: UploadState) { |
89 | switch (state.status) { | |
90 | case 'error': | |
91 | const error = state.response?.error || 'Unknow error' | |
92 | ||
93 | this.handleUploadError({ | |
94 | error: new Error(error), | |
95 | name: 'HttpErrorResponse', | |
96 | message: error, | |
97 | ok: false, | |
98 | headers: new HttpHeaders(state.responseHeaders), | |
99 | status: +state.responseStatus, | |
100 | statusText: error, | |
101 | type: HttpEventType.Response, | |
102 | url: state.url | |
103 | }) | |
104 | break | |
105 | ||
106 | case 'cancelled': | |
107 | this.isUploadingVideo = false | |
108 | this.videoUploadPercents = 0 | |
109 | ||
110 | this.firstStepError.emit() | |
111 | this.enableRetryAfterError = false | |
112 | this.error = '' | |
113 | break | |
114 | ||
115 | case 'queue': | |
116 | this.closeFirstStep(state.name) | |
117 | break | |
118 | ||
119 | case 'uploading': | |
120 | this.videoUploadPercents = state.progress | |
121 | break | |
122 | ||
123 | case 'paused': | |
71fb8b5a | 124 | this.notifier.info($localize`Upload on hold`) |
f6d6e7f8 | 125 | break |
126 | ||
127 | case 'complete': | |
128 | this.videoUploaded = true | |
129 | this.videoUploadPercents = 100 | |
130 | ||
131 | this.videoUploadedIds = state?.response.video | |
132 | break | |
133 | } | |
134 | } | |
135 | ||
fbad87b0 | 136 | ngOnInit () { |
43620009 | 137 | super.ngOnInit() |
fbad87b0 C |
138 | |
139 | this.userService.getMyVideoQuotaUsed() | |
348106f2 C |
140 | .subscribe(data => { |
141 | this.userVideoQuotaUsed = data.videoQuotaUsed | |
142 | this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily | |
143 | }) | |
f6d6e7f8 | 144 | |
145 | this.resumableUploadService.events | |
146 | .subscribe(state => this.onUploadVideoOngoing(state)) | |
fbad87b0 C |
147 | } |
148 | ||
2e257e36 C |
149 | ngAfterViewInit () { |
150 | this.hooks.runAction('action:video-upload.init', 'video-edit') | |
151 | } | |
152 | ||
fbad87b0 | 153 | ngOnDestroy () { |
f6d6e7f8 | 154 | this.cancelUpload() |
fbad87b0 C |
155 | } |
156 | ||
157 | canDeactivate () { | |
158 | let text = '' | |
159 | ||
160 | if (this.videoUploaded === true) { | |
5bfc33b6 | 161 | // FIXME: cannot concatenate strings using $localize |
66357162 C |
162 | text = $localize`Your video was uploaded to your account and is private.` + ' ' + |
163 | $localize`But associated data (tags, description...) will be lost, are you sure you want to leave this page?` | |
fbad87b0 | 164 | } else { |
66357162 | 165 | text = $localize`Your video is not uploaded yet, are you sure you want to leave this page?` |
fbad87b0 C |
166 | } |
167 | ||
168 | return { | |
169 | canDeactivate: !this.isUploadingVideo, | |
170 | text | |
171 | } | |
172 | } | |
173 | ||
f6d6e7f8 | 174 | onFileDropped (files: FileList) { |
c9ff8a08 | 175 | this.videofileInput.nativeElement.files = files |
7b992a86 | 176 | |
f6d6e7f8 | 177 | this.onFileChange({ target: this.videofileInput.nativeElement }) |
7b992a86 C |
178 | } |
179 | ||
f6d6e7f8 | 180 | onFileChange (event: Event | { target: HTMLInputElement }) { |
181 | const file = (event.target as HTMLInputElement).files[0] | |
e713698f | 182 | |
f6d6e7f8 | 183 | if (!file) return |
e713698f | 184 | |
f6d6e7f8 | 185 | if (!this.checkGlobalUserQuota(file)) return |
186 | if (!this.checkDailyUserQuota(file)) return | |
fbad87b0 | 187 | |
f6d6e7f8 | 188 | if (this.isAudioFile(file.name)) { |
7b992a86 | 189 | this.isUploadingAudioFile = true |
bee0abff FA |
190 | return |
191 | } | |
192 | ||
fbad87b0 | 193 | this.isUploadingVideo = true |
f6d6e7f8 | 194 | this.fileToUpload = file |
fbad87b0 | 195 | |
f6d6e7f8 | 196 | this.uploadFile(file) |
d4132d3f RK |
197 | } |
198 | ||
f6d6e7f8 | 199 | uploadAudio () { |
200 | this.uploadFile(this.getInputVideoFile(), this.previewfileUpload) | |
201 | } | |
fbad87b0 | 202 | |
f6d6e7f8 | 203 | retryUpload () { |
204 | this.enableRetryAfterError = false | |
205 | this.error = '' | |
206 | this.uploadFile(this.fileToUpload) | |
207 | } | |
f2eb23cd | 208 | |
f6d6e7f8 | 209 | cancelUpload () { |
210 | this.resumableUploadService.control({ action: 'cancel' }) | |
fbad87b0 C |
211 | } |
212 | ||
59c9c5d9 C |
213 | isPublishingButtonDisabled () { |
214 | return !this.form.valid || | |
215 | this.isUpdatingVideo === true || | |
c7a53f61 C |
216 | this.videoUploaded !== true || |
217 | !this.videoUploadedIds.id | |
59c9c5d9 C |
218 | } |
219 | ||
f6d6e7f8 | 220 | getAudioUploadLabel () { |
221 | const videofile = this.getInputVideoFile() | |
222 | if (!videofile) return $localize`Upload` | |
223 | ||
224 | return $localize`Upload ${videofile.name}` | |
225 | } | |
226 | ||
fbad87b0 | 227 | updateSecondStep () { |
c7a53f61 | 228 | if (this.isPublishingButtonDisabled() || !this.checkForm()) { |
fbad87b0 C |
229 | return |
230 | } | |
231 | ||
232 | const video = new VideoEdit() | |
233 | video.patch(this.form.value) | |
234 | video.id = this.videoUploadedIds.id | |
235 | video.uuid = this.videoUploadedIds.uuid | |
236 | ||
237 | this.isUpdatingVideo = true | |
43620009 C |
238 | |
239 | this.updateVideoAndCaptions(video) | |
fbad87b0 C |
240 | .subscribe( |
241 | () => { | |
242 | this.isUpdatingVideo = false | |
243 | this.isUploadingVideo = false | |
fbad87b0 | 244 | |
66357162 | 245 | this.notifier.success($localize`Video published.`) |
d4a8e7a6 | 246 | this.router.navigateByUrl(Video.buildWatchUrl(video)) |
fbad87b0 C |
247 | }, |
248 | ||
249 | err => { | |
7373507f C |
250 | this.error = err.message |
251 | scrollToTop() | |
fbad87b0 C |
252 | console.error(err) |
253 | } | |
254 | ) | |
255 | } | |
7b992a86 | 256 | |
f6d6e7f8 | 257 | private getInputVideoFile () { |
258 | return this.videofileInput.nativeElement.files[0] | |
259 | } | |
260 | ||
261 | private uploadFile (file: File, previewfile?: File) { | |
262 | const metadata = { | |
263 | waitTranscoding: true, | |
264 | commentsEnabled: true, | |
265 | downloadEnabled: true, | |
266 | channelId: this.firstStepChannelId, | |
267 | nsfw: this.serverConfig.instance.isNSFW, | |
a3f45a2a | 268 | privacy: this.highestPrivacy.toString(), |
f6d6e7f8 | 269 | filename: file.name, |
270 | previewfile: previewfile as any | |
271 | } | |
272 | ||
273 | this.resumableUploadService.handleFiles(file, { | |
274 | ...this.uploadxOptions, | |
275 | metadata | |
276 | }) | |
277 | ||
278 | this.isUploadingVideo = true | |
279 | } | |
280 | ||
281 | private handleUploadError (err: HttpErrorResponse) { | |
282 | // Reset progress (but keep isUploadingVideo true) | |
283 | this.videoUploadPercents = 0 | |
284 | this.enableRetryAfterError = true | |
285 | ||
286 | this.error = genericUploadErrorHandler({ | |
287 | err, | |
288 | name: $localize`video`, | |
289 | notifier: this.notifier, | |
290 | sticky: false | |
291 | }) | |
292 | ||
293 | if (err.status === HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) { | |
294 | this.cancelUpload() | |
295 | } | |
296 | } | |
297 | ||
298 | private closeFirstStep (filename: string) { | |
299 | const nameWithoutExtension = filename.replace(/\.[^/.]+$/, '') | |
300 | const name = nameWithoutExtension.length < 3 ? filename : nameWithoutExtension | |
301 | ||
302 | this.form.patchValue({ | |
303 | name, | |
304 | privacy: this.firstStepPrivacyId, | |
305 | nsfw: this.serverConfig.instance.isNSFW, | |
306 | channelId: this.firstStepChannelId, | |
307 | previewfile: this.previewfileUpload | |
308 | }) | |
309 | ||
310 | this.firstStepDone.emit(name) | |
311 | } | |
312 | ||
7b992a86 C |
313 | private checkGlobalUserQuota (videofile: File) { |
314 | const bytePipes = new BytesPipe() | |
315 | ||
316 | // Check global user quota | |
317 | const videoQuota = this.authService.getUser().videoQuota | |
318 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
66357162 C |
319 | const videoSizeBytes = bytePipes.transform(videofile.size, 0) |
320 | const videoQuotaUsedBytes = bytePipes.transform(this.userVideoQuotaUsed, 0) | |
321 | const videoQuotaBytes = bytePipes.transform(videoQuota, 0) | |
322 | ||
f6d6e7f8 | 323 | const msg = $localize`Your video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${videoQuotaUsedBytes}, quota: ${videoQuotaBytes})` |
7b992a86 C |
324 | this.notifier.error(msg) |
325 | ||
326 | return false | |
327 | } | |
328 | ||
329 | return true | |
330 | } | |
331 | ||
332 | private checkDailyUserQuota (videofile: File) { | |
333 | const bytePipes = new BytesPipe() | |
334 | ||
335 | // Check daily user quota | |
336 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily | |
337 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
66357162 C |
338 | const videoSizeBytes = bytePipes.transform(videofile.size, 0) |
339 | const quotaUsedDailyBytes = bytePipes.transform(this.userVideoQuotaUsedDaily, 0) | |
340 | const quotaDailyBytes = bytePipes.transform(videoQuotaDaily, 0) | |
f6d6e7f8 | 341 | const msg = $localize`Your daily video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${quotaUsedDailyBytes}, quota: ${quotaDailyBytes})` |
7b992a86 C |
342 | this.notifier.error(msg) |
343 | ||
344 | return false | |
345 | } | |
346 | ||
347 | return true | |
348 | } | |
349 | ||
350 | private isAudioFile (filename: string) { | |
99d362de C |
351 | const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ] |
352 | ||
353 | return extensions.some(e => filename.endsWith(e)) | |
7b992a86 | 354 | } |
fbad87b0 | 355 | } |