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