]>
Commit | Line | Data |
---|---|---|
45353742 | 1 | import { truncate } from 'lodash-es' |
d4a8e7a6 | 2 | import { UploadState, UploadxOptions, UploadxService } from 'ngx-uploadx' |
45353742 | 3 | import { isIOS } from 'src/assets/player/utils' |
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' |
2e80d256 | 12 | import { HttpStatusCode, VideoCreateResult, VideoPrivacy } 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, | |
285 | metadata | |
286 | }) | |
287 | ||
288 | this.isUploadingVideo = true | |
289 | } | |
290 | ||
291 | private handleUploadError (err: HttpErrorResponse) { | |
292 | // Reset progress (but keep isUploadingVideo true) | |
293 | this.videoUploadPercents = 0 | |
294 | this.enableRetryAfterError = true | |
295 | ||
296 | this.error = genericUploadErrorHandler({ | |
297 | err, | |
298 | name: $localize`video`, | |
299 | notifier: this.notifier, | |
300 | sticky: false | |
301 | }) | |
302 | ||
303 | if (err.status === HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) { | |
304 | this.cancelUpload() | |
305 | } | |
306 | } | |
307 | ||
308 | private closeFirstStep (filename: string) { | |
45353742 | 309 | const name = this.buildVideoFilename(filename) |
f6d6e7f8 | 310 | |
311 | this.form.patchValue({ | |
312 | name, | |
313 | privacy: this.firstStepPrivacyId, | |
314 | nsfw: this.serverConfig.instance.isNSFW, | |
315 | channelId: this.firstStepChannelId, | |
316 | previewfile: this.previewfileUpload | |
317 | }) | |
318 | ||
319 | this.firstStepDone.emit(name) | |
320 | } | |
321 | ||
7b992a86 C |
322 | private checkGlobalUserQuota (videofile: File) { |
323 | const bytePipes = new BytesPipe() | |
324 | ||
325 | // Check global user quota | |
326 | const videoQuota = this.authService.getUser().videoQuota | |
327 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { | |
66357162 C |
328 | const videoSizeBytes = bytePipes.transform(videofile.size, 0) |
329 | const videoQuotaUsedBytes = bytePipes.transform(this.userVideoQuotaUsed, 0) | |
330 | const videoQuotaBytes = bytePipes.transform(videoQuota, 0) | |
331 | ||
9df52d66 | 332 | // eslint-disable-next-line max-len |
f6d6e7f8 | 333 | const msg = $localize`Your video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${videoQuotaUsedBytes}, quota: ${videoQuotaBytes})` |
7b992a86 C |
334 | this.notifier.error(msg) |
335 | ||
336 | return false | |
337 | } | |
338 | ||
339 | return true | |
340 | } | |
341 | ||
342 | private checkDailyUserQuota (videofile: File) { | |
343 | const bytePipes = new BytesPipe() | |
344 | ||
345 | // Check daily user quota | |
346 | const videoQuotaDaily = this.authService.getUser().videoQuotaDaily | |
347 | if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) { | |
66357162 C |
348 | const videoSizeBytes = bytePipes.transform(videofile.size, 0) |
349 | const quotaUsedDailyBytes = bytePipes.transform(this.userVideoQuotaUsedDaily, 0) | |
350 | const quotaDailyBytes = bytePipes.transform(videoQuotaDaily, 0) | |
9df52d66 | 351 | // eslint-disable-next-line max-len |
f6d6e7f8 | 352 | const msg = $localize`Your daily video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${quotaUsedDailyBytes}, quota: ${quotaDailyBytes})` |
7b992a86 C |
353 | this.notifier.error(msg) |
354 | ||
355 | return false | |
356 | } | |
357 | ||
358 | return true | |
359 | } | |
360 | ||
361 | private isAudioFile (filename: string) { | |
99d362de C |
362 | const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ] |
363 | ||
364 | return extensions.some(e => filename.endsWith(e)) | |
7b992a86 | 365 | } |
45353742 C |
366 | |
367 | private buildVideoFilename (filename: string) { | |
368 | const nameWithoutExtension = filename.replace(/\.[^/.]+$/, '') | |
369 | let name = nameWithoutExtension.length < 3 | |
370 | ? filename | |
371 | : nameWithoutExtension | |
372 | ||
373 | const videoNameMaxSize = 110 | |
374 | if (name.length > videoNameMaxSize) { | |
375 | name = truncate(name, { length: videoNameMaxSize, omission: '' }) | |
376 | } | |
377 | ||
378 | return name | |
379 | } | |
fbad87b0 | 380 | } |