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