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