]>
Commit | Line | Data |
---|---|---|
45353742 | 1 | import { truncate } from 'lodash-es' |
d4a8e7a6 C |
2 | import { UploadState, UploadxOptions, UploadxService } from 'ngx-uploadx' |
3 | import { HttpErrorResponse, HttpEventType, HttpHeaders } from '@angular/common/http' | |
2e257e36 | 4 | import { AfterViewInit, Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core' |
72dd3a7c | 5 | import { ActivatedRoute, Router } from '@angular/router' |
6 | import { AuthService, CanComponentDeactivate, HooksService, MetaService, Notifier, ServerService, UserService } from '@app/core' | |
d4a8e7a6 | 7 | import { genericUploadErrorHandler, scrollToTop } from '@app/helpers' |
5c5bcea2 | 8 | import { FormReactiveService } from '@app/shared/shared-forms' |
d4a8e7a6 | 9 | import { BytesPipe, Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main' |
fbad87b0 | 10 | import { LoadingBarService } from '@ngx-loading-bar/core' |
42b40636 C |
11 | import { logger } from '@root-helpers/logger' |
12 | import { isIOS } from '@root-helpers/web-browser' | |
f33e5159 | 13 | import { HttpStatusCode, VideoCreateResult } from '@shared/models' |
d4a8e7a6 | 14 | import { UploaderXFormData } from './uploaderx-form-data' |
1942f11d | 15 | import { VideoSend } from './video-send' |
b105ea60 | 16 | import { Subscription } from 'rxjs' |
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 | }) | |
f6d6e7f8 | 27 | export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, AfterViewInit, CanComponentDeactivate { |
fbad87b0 | 28 | @Output() firstStepDone = new EventEmitter<string>() |
7373507f | 29 | @Output() firstStepError = new EventEmitter<void>() |
2f5d2ec5 | 30 | @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement> |
fbad87b0 | 31 | |
43620009 | 32 | userVideoQuotaUsed = 0 |
bee0abff | 33 | userVideoQuotaUsedDaily = 0 |
43620009 | 34 | |
7b992a86 | 35 | isUploadingAudioFile = false |
fbad87b0 | 36 | isUploadingVideo = false |
7b992a86 | 37 | |
fbad87b0 | 38 | videoUploaded = false |
fbad87b0 | 39 | videoUploadPercents = 0 |
2e80d256 | 40 | videoUploadedIds: VideoCreateResult = { |
fbad87b0 | 41 | id: 0, |
2e80d256 C |
42 | uuid: '', |
43 | shortUUID: '' | |
fbad87b0 | 44 | } |
d4132d3f | 45 | formData: FormData |
7b992a86 | 46 | |
7b992a86 | 47 | previewfileUpload: File |
fbad87b0 | 48 | |
7373507f | 49 | error: string |
d4132d3f | 50 | enableRetryAfterError: boolean |
fbad87b0 | 51 | |
f6d6e7f8 | 52 | // So that it can be accessed in the template |
231ff4af | 53 | protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + '/upload-resumable' |
f6d6e7f8 | 54 | |
f6d6e7f8 | 55 | private isUpdatingVideo = false |
56 | private fileToUpload: File | |
fbad87b0 | 57 | |
83664918 C |
58 | private alreadyRefreshedToken = false |
59 | ||
b105ea60 C |
60 | private uploadServiceSubscription: Subscription |
61 | ||
fbad87b0 | 62 | constructor ( |
5c5bcea2 | 63 | protected formReactiveService: FormReactiveService, |
43620009 | 64 | protected loadingBar: LoadingBarService, |
f8b2c1b4 | 65 | protected notifier: Notifier, |
43620009 C |
66 | protected authService: AuthService, |
67 | protected serverService: ServerService, | |
68 | protected videoService: VideoService, | |
69 | protected videoCaptionService: VideoCaptionService, | |
fbad87b0 | 70 | private userService: UserService, |
2e257e36 | 71 | private router: Router, |
f6d6e7f8 | 72 | private hooks: HooksService, |
72dd3a7c | 73 | private resumableUploadService: UploadxService, |
74 | private metaService: MetaService, | |
75 | private route: ActivatedRoute | |
f6d6e7f8 | 76 | ) { |
fbad87b0 C |
77 | super() |
78 | } | |
79 | ||
80 | get videoExtensions () { | |
758f0d19 | 81 | return this.serverConfig.video.file.extensions.join(', ') |
fbad87b0 C |
82 | } |
83 | ||
3ce48a0c C |
84 | ngOnInit () { |
85 | super.ngOnInit() | |
86 | ||
87 | this.userService.getMyVideoQuotaUsed() | |
88 | .subscribe(data => { | |
89 | this.userVideoQuotaUsed = data.videoQuotaUsed | |
90 | this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily | |
91 | }) | |
92 | ||
b105ea60 | 93 | this.uploadServiceSubscription = this.resumableUploadService.events |
3ce48a0c | 94 | .subscribe(state => this.onUploadVideoOngoing(state)) |
3ce48a0c C |
95 | } |
96 | ||
97 | ngAfterViewInit () { | |
98 | this.hooks.runAction('action:video-upload.init', 'video-edit') | |
99 | } | |
100 | ||
101 | ngOnDestroy () { | |
b105ea60 C |
102 | this.resumableUploadService.disconnect() |
103 | ||
104 | if (this.uploadServiceSubscription) this.uploadServiceSubscription.unsubscribe() | |
3ce48a0c C |
105 | } |
106 | ||
107 | canDeactivate () { | |
108 | let text = '' | |
109 | ||
110 | if (this.videoUploaded === true) { | |
60dd77c6 | 111 | // We can't concatenate strings using $localize |
3ce48a0c C |
112 | text = $localize`Your video was uploaded to your account and is private.` + ' ' + |
113 | $localize`But associated data (tags, description...) will be lost, are you sure you want to leave this page?` | |
114 | } else { | |
115 | text = $localize`Your video is not uploaded yet, are you sure you want to leave this page?` | |
116 | } | |
117 | ||
118 | return { | |
119 | canDeactivate: !this.isUploadingVideo, | |
120 | text | |
121 | } | |
122 | } | |
123 | ||
72dd3a7c | 124 | updateTitle () { |
125 | const videoName = this.form.get('name').value | |
126 | ||
127 | if (this.videoUploaded) { | |
25fd0b59 | 128 | this.metaService.setTitle($localize`Publish ${videoName}`) |
72dd3a7c | 129 | } else if (this.isUploadingAudioFile || this.isUploadingVideo) { |
130 | this.metaService.setTitle(`${this.videoUploadPercents}% - ${videoName}`) | |
131 | } else { | |
132 | this.metaService.update(this.route.snapshot.data['meta']) | |
133 | } | |
134 | } | |
135 | ||
f6d6e7f8 | 136 | onUploadVideoOngoing (state: UploadState) { |
137 | switch (state.status) { | |
9df52d66 | 138 | case 'error': { |
672e185b | 139 | if (!this.alreadyRefreshedToken && state.responseStatus === HttpStatusCode.UNAUTHORIZED_401) { |
83664918 C |
140 | this.alreadyRefreshedToken = true |
141 | ||
142 | return this.refereshTokenAndRetryUpload() | |
143 | } | |
144 | ||
f4120aea | 145 | const error = state.response?.error?.message || state.response?.error || 'Unknown error' |
f6d6e7f8 | 146 | |
147 | this.handleUploadError({ | |
148 | error: new Error(error), | |
149 | name: 'HttpErrorResponse', | |
150 | message: error, | |
151 | ok: false, | |
152 | headers: new HttpHeaders(state.responseHeaders), | |
153 | status: +state.responseStatus, | |
154 | statusText: error, | |
155 | type: HttpEventType.Response, | |
156 | url: state.url | |
157 | }) | |
158 | break | |
9df52d66 | 159 | } |
f6d6e7f8 | 160 | |
161 | case 'cancelled': | |
162 | this.isUploadingVideo = false | |
163 | this.videoUploadPercents = 0 | |
164 | ||
165 | this.firstStepError.emit() | |
166 | this.enableRetryAfterError = false | |
167 | this.error = '' | |
decbd0b6 | 168 | this.isUploadingAudioFile = false |
f6d6e7f8 | 169 | break |
170 | ||
171 | case 'queue': | |
172 | this.closeFirstStep(state.name) | |
173 | break | |
174 | ||
175 | case 'uploading': | |
72dd3a7c | 176 | // TODO: remove || 0 when // https://github.com/kukhariev/ngx-uploadx/pull/368 is released |
177 | this.videoUploadPercents = state.progress || 0 | |
f6d6e7f8 | 178 | break |
179 | ||
180 | case 'paused': | |
71fb8b5a | 181 | this.notifier.info($localize`Upload on hold`) |
f6d6e7f8 | 182 | break |
183 | ||
184 | case 'complete': | |
185 | this.videoUploaded = true | |
186 | this.videoUploadPercents = 100 | |
187 | ||
188 | this.videoUploadedIds = state?.response.video | |
189 | break | |
190 | } | |
72dd3a7c | 191 | |
192 | this.updateTitle() | |
f6d6e7f8 | 193 | } |
194 | ||
f6d6e7f8 | 195 | onFileDropped (files: FileList) { |
c9ff8a08 | 196 | this.videofileInput.nativeElement.files = files |
7b992a86 | 197 | |
f6d6e7f8 | 198 | this.onFileChange({ target: this.videofileInput.nativeElement }) |
7b992a86 C |
199 | } |
200 | ||
f6d6e7f8 | 201 | onFileChange (event: Event | { target: HTMLInputElement }) { |
202 | const file = (event.target as HTMLInputElement).files[0] | |
e713698f | 203 | |
f6d6e7f8 | 204 | if (!file) return |
e713698f | 205 | |
f6d6e7f8 | 206 | if (!this.checkGlobalUserQuota(file)) return |
207 | if (!this.checkDailyUserQuota(file)) return | |
fbad87b0 | 208 | |
f6d6e7f8 | 209 | if (this.isAudioFile(file.name)) { |
7b992a86 | 210 | this.isUploadingAudioFile = true |
bee0abff FA |
211 | return |
212 | } | |
213 | ||
fbad87b0 | 214 | this.isUploadingVideo = true |
f6d6e7f8 | 215 | this.fileToUpload = file |
fbad87b0 | 216 | |
f6d6e7f8 | 217 | this.uploadFile(file) |
d4132d3f RK |
218 | } |
219 | ||
f6d6e7f8 | 220 | uploadAudio () { |
221 | this.uploadFile(this.getInputVideoFile(), this.previewfileUpload) | |
222 | } | |
fbad87b0 | 223 | |
f6d6e7f8 | 224 | retryUpload () { |
225 | this.enableRetryAfterError = false | |
226 | this.error = '' | |
227 | this.uploadFile(this.fileToUpload) | |
228 | } | |
f2eb23cd | 229 | |
f6d6e7f8 | 230 | cancelUpload () { |
231 | this.resumableUploadService.control({ action: 'cancel' }) | |
fbad87b0 C |
232 | } |
233 | ||
59c9c5d9 | 234 | isPublishingButtonDisabled () { |
cc4bf76c | 235 | return !this.form.valid || |
59c9c5d9 | 236 | this.isUpdatingVideo === true || |
c7a53f61 C |
237 | this.videoUploaded !== true || |
238 | !this.videoUploadedIds.id | |
59c9c5d9 C |
239 | } |
240 | ||
f6d6e7f8 | 241 | getAudioUploadLabel () { |
242 | const videofile = this.getInputVideoFile() | |
243 | if (!videofile) return $localize`Upload` | |
244 | ||
245 | return $localize`Upload ${videofile.name}` | |
246 | } | |
247 | ||
cc4bf76c C |
248 | async updateSecondStep () { |
249 | if (!await this.isFormValid()) return | |
250 | if (this.isPublishingButtonDisabled()) return | |
fbad87b0 C |
251 | |
252 | const video = new VideoEdit() | |
253 | video.patch(this.form.value) | |
254 | video.id = this.videoUploadedIds.id | |
255 | video.uuid = this.videoUploadedIds.uuid | |
2e80d256 | 256 | video.shortUUID = this.videoUploadedIds.shortUUID |
fbad87b0 C |
257 | |
258 | this.isUpdatingVideo = true | |
43620009 C |
259 | |
260 | this.updateVideoAndCaptions(video) | |
1378c0d3 C |
261 | .subscribe({ |
262 | next: () => { | |
fbad87b0 C |
263 | this.isUpdatingVideo = false |
264 | this.isUploadingVideo = false | |
fbad87b0 | 265 | |
66357162 | 266 | this.notifier.success($localize`Video published.`) |
d4a8e7a6 | 267 | this.router.navigateByUrl(Video.buildWatchUrl(video)) |
fbad87b0 C |
268 | }, |
269 | ||
1378c0d3 | 270 | error: err => { |
7373507f C |
271 | this.error = err.message |
272 | scrollToTop() | |
42b40636 | 273 | logger.error(err) |
fbad87b0 | 274 | } |
1378c0d3 | 275 | }) |
fbad87b0 | 276 | } |
7b992a86 | 277 | |
f6d6e7f8 | 278 | private getInputVideoFile () { |
279 | return this.videofileInput.nativeElement.files[0] | |
280 | } | |
281 | ||
282 | private uploadFile (file: File, previewfile?: File) { | |
283 | const metadata = { | |
284 | waitTranscoding: true, | |
f6d6e7f8 | 285 | channelId: this.firstStepChannelId, |
286 | nsfw: this.serverConfig.instance.isNSFW, | |
a3f45a2a | 287 | privacy: this.highestPrivacy.toString(), |
45353742 | 288 | name: this.buildVideoFilename(file.name), |
f6d6e7f8 | 289 | filename: file.name, |
290 | previewfile: previewfile as any | |
291 | } | |
292 | ||
293 | this.resumableUploadService.handleFiles(file, { | |
83664918 C |
294 | ...this.getUploadxOptions(), |
295 | ||
296 | metadata | |
f6d6e7f8 | 297 | }) |
298 | ||
299 | this.isUploadingVideo = true | |
300 | } | |
301 | ||
302 | private handleUploadError (err: HttpErrorResponse) { | |
303 | // Reset progress (but keep isUploadingVideo true) | |
304 | this.videoUploadPercents = 0 | |
305 | this.enableRetryAfterError = true | |
306 | ||
307 | this.error = genericUploadErrorHandler({ | |
308 | err, | |
309 | name: $localize`video`, | |
310 | notifier: this.notifier, | |
311 | sticky: false | |
312 | }) | |
313 | ||
314 | if (err.status === HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) { | |
315 | this.cancelUpload() | |
316 | } | |
317 | } | |
318 | ||
319 | private closeFirstStep (filename: string) { | |
45353742 | 320 | const name = this.buildVideoFilename(filename) |
f6d6e7f8 | 321 | |
322 | this.form.patchValue({ | |
323 | name, | |
324 | privacy: this.firstStepPrivacyId, | |
325 | nsfw: this.serverConfig.instance.isNSFW, | |
326 | channelId: this.firstStepChannelId, | |
327 | previewfile: this.previewfileUpload | |
328 | }) | |
329 | ||
330 | this.firstStepDone.emit(name) | |
72dd3a7c | 331 | this.updateTitle() |
f6d6e7f8 | 332 | } |
333 | ||
7b992a86 C |
334 | private checkGlobalUserQuota (videofile: File) { |
335 | const bytePipes = new BytesPipe() | |
336 | ||
337 | // Check global user quota | |
338 | const videoQuota = this.authService.getUser().videoQuota | |
339 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
66357162 C |
340 | const videoSizeBytes = bytePipes.transform(videofile.size, 0) |
341 | const videoQuotaUsedBytes = bytePipes.transform(this.userVideoQuotaUsed, 0) | |
342 | const videoQuotaBytes = bytePipes.transform(videoQuota, 0) | |
343 | ||
9df52d66 | 344 | // eslint-disable-next-line max-len |
f6d6e7f8 | 345 | const msg = $localize`Your video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${videoQuotaUsedBytes}, quota: ${videoQuotaBytes})` |
7b992a86 C |
346 | this.notifier.error(msg) |
347 | ||
348 | return false | |
349 | } | |
350 | ||
351 | return true | |
352 | } | |
353 | ||
354 | private checkDailyUserQuota (videofile: File) { | |
355 | const bytePipes = new BytesPipe() | |
356 | ||
357 | // Check daily user quota | |
358 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily | |
359 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
66357162 C |
360 | const videoSizeBytes = bytePipes.transform(videofile.size, 0) |
361 | const quotaUsedDailyBytes = bytePipes.transform(this.userVideoQuotaUsedDaily, 0) | |
362 | const quotaDailyBytes = bytePipes.transform(videoQuotaDaily, 0) | |
9df52d66 | 363 | // eslint-disable-next-line max-len |
f6d6e7f8 | 364 | const msg = $localize`Your daily video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${quotaUsedDailyBytes}, quota: ${quotaDailyBytes})` |
7b992a86 C |
365 | this.notifier.error(msg) |
366 | ||
367 | return false | |
368 | } | |
369 | ||
370 | return true | |
371 | } | |
372 | ||
373 | private isAudioFile (filename: string) { | |
99d362de C |
374 | const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ] |
375 | ||
376 | return extensions.some(e => filename.endsWith(e)) | |
7b992a86 | 377 | } |
45353742 C |
378 | |
379 | private buildVideoFilename (filename: string) { | |
380 | const nameWithoutExtension = filename.replace(/\.[^/.]+$/, '') | |
381 | let name = nameWithoutExtension.length < 3 | |
382 | ? filename | |
383 | : nameWithoutExtension | |
384 | ||
385 | const videoNameMaxSize = 110 | |
386 | if (name.length > videoNameMaxSize) { | |
387 | name = truncate(name, { length: videoNameMaxSize, omission: '' }) | |
388 | } | |
389 | ||
390 | return name | |
391 | } | |
83664918 C |
392 | |
393 | private refereshTokenAndRetryUpload () { | |
394 | this.authService.refreshAccessToken() | |
395 | .subscribe(() => this.retryUpload()) | |
396 | } | |
397 | ||
398 | private getUploadxOptions (): UploadxOptions { | |
399 | // FIXME: https://github.com/Chocobozzz/PeerTube/issues/4382#issuecomment-915854167 | |
400 | const chunkSize = isIOS() | |
401 | ? 0 | |
402 | : undefined // Auto chunk size | |
403 | ||
404 | return { | |
405 | endpoint: this.BASE_VIDEO_UPLOAD_URL, | |
406 | multiple: false, | |
407 | ||
408 | maxChunkSize: this.serverConfig.client.videos.resumableUpload.maxChunkSize, | |
409 | chunkSize, | |
410 | ||
411 | token: this.authService.getAccessToken(), | |
412 | ||
413 | uploaderClass: UploaderXFormData, | |
414 | ||
415 | retryConfig: { | |
416 | maxAttempts: 30, // maximum attempts for 503 codes, otherwise set to 6, see below | |
417 | maxDelay: 120_000, // 2 min | |
418 | shouldRetry: (code: number, attempts: number) => { | |
419 | return code === HttpStatusCode.SERVICE_UNAVAILABLE_503 || ((code < 400 || code > 500) && attempts < 6) | |
420 | } | |
421 | } | |
422 | } | |
423 | } | |
fbad87b0 | 424 | } |