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