]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Add progress bar for video upload
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
202f6b6c 1import { HttpEventType, HttpResponse } from '@angular/common/http'
bfb3a98f 2import { Component, OnInit, ViewChild } from '@angular/core'
df98563e
C
3import { FormBuilder, FormGroup } from '@angular/forms'
4import { Router } from '@angular/router'
df98563e 5import { NotificationsService } from 'angular2-notifications'
202f6b6c
C
6import { VideoService } from 'app/shared/video/video.service'
7import { VideoCreate } from '../../../../../shared'
8import { AuthService, ServerService } from '../../core'
27e1a06c
C
9import { FormReactive } from '../../shared'
10import { ValidatorMessage } from '../../shared/forms/form-validators'
11import { VideoEdit } from '../../shared/video/video-edit.model'
1553e15d 12
dc8bc31b
C
13@Component({
14 selector: 'my-videos-add',
27e1a06c
C
15 templateUrl: './video-add.component.html',
16 styleUrls: [
17 './shared/video-edit.component.scss',
18 './video-add.component.scss'
19 ]
dc8bc31b
C
20})
21
4b2f33f3 22export class VideoAddComponent extends FormReactive implements OnInit {
bfb3a98f
C
23 @ViewChild('videofileInput') videofileInput
24
27e1a06c 25 isUploadingVideo = false
baeefe22 26 videoUploaded = false
c182778e 27 videoUploadPercents = 0
3758da94 28
27e1a06c 29 error: string = null
df98563e 30 form: FormGroup
27e1a06c
C
31 formErrors: { [ id: string ]: string } = {}
32 validationMessages: ValidatorMessage = {}
baeefe22 33
27e1a06c
C
34 userVideoChannels = []
35 videoPrivacies = []
36 firstStepPrivacy = 0
37 firstStepChannel = 0
dc8bc31b 38
df98563e 39 constructor (
4b2f33f3 40 private formBuilder: FormBuilder,
7ddd02c9 41 private router: Router,
6e07c3de 42 private notificationsService: NotificationsService,
bcd9f81e 43 private authService: AuthService,
db7af09b 44 private serverService: ServerService,
6e07c3de 45 private videoService: VideoService
4b2f33f3 46 ) {
df98563e 47 super()
4b2f33f3 48 }
dc8bc31b 49
df98563e 50 buildForm () {
27e1a06c 51 this.form = this.formBuilder.group({})
df98563e 52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
53 }
54
df98563e 55 ngOnInit () {
df98563e 56 this.buildForm()
2de96f4d 57
c182778e 58 this.serverService.videoPrivaciesLoaded
baeefe22
C
59 .subscribe(
60 () => {
61 this.videoPrivacies = this.serverService.getVideoPrivacies()
62 this.firstStepPrivacy = this.videoPrivacies[0].id
63 })
27e1a06c 64
2de96f4d
C
65 this.authService.userInformationLoaded
66 .subscribe(
67 () => {
68 const user = this.authService.getUser()
69 if (!user) return
70
71 const videoChannels = user.videoChannels
72 if (Array.isArray(videoChannels) === false) return
73
74 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
27e1a06c 75 this.firstStepChannel = this.userVideoChannels[0].id
2de96f4d
C
76 }
77 )
e822fdae
C
78 }
79
baeefe22
C
80 fileChange () {
81 this.uploadFirstStep()
bf57d5ee
C
82 }
83
bfb3a98f
C
84 checkForm () {
85 this.forceCheck()
86
87 return this.form.valid
e822fdae
C
88 }
89
27e1a06c 90 uploadFirstStep () {
bfb3a98f 91 const videofile = this.videofileInput.nativeElement.files[0]
baeefe22
C
92 const name = videofile.name
93 const privacy = this.firstStepPrivacy.toString()
94 const nsfw = false
95 const channelId = this.firstStepChannel.toString()
bfb3a98f
C
96
97 const formData = new FormData()
98 formData.append('name', name)
fd45e8f4 99 formData.append('privacy', privacy.toString())
bfb3a98f 100 formData.append('nsfw', '' + nsfw)
bcd9f81e 101 formData.append('channelId', '' + channelId)
bfb3a98f
C
102 formData.append('videofile', videofile)
103
baeefe22
C
104 this.isUploadingVideo = true
105 this.form.patchValue({
106 name,
107 privacy,
108 nsfw,
109 channelId
110 })
e822fdae 111
bfb3a98f
C
112 this.videoService.uploadVideo(formData).subscribe(
113 event => {
114 if (event.type === HttpEventType.UploadProgress) {
c182778e 115 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
bfb3a98f
C
116 } else if (event instanceof HttpResponse) {
117 console.log('Video uploaded.')
bfb3a98f 118
baeefe22 119 this.videoUploaded = true
bfb3a98f
C
120 }
121 },
122
315cc0cc
C
123 err => {
124 // Reset progress
c182778e 125 this.videoUploadPercents = 0
315cc0cc
C
126 this.error = err.message
127 }
bfb3a98f 128 )
dc8bc31b 129 }
27e1a06c
C
130
131 updateSecondStep () {
132 if (this.checkForm() === false) {
133 return
134 }
135
136 const video = new VideoEdit(this.form.value)
137
138 this.videoService.updateVideo(video)
139 .subscribe(
140 () => {
141 this.notificationsService.success('Success', 'Video published.')
142 this.router.navigate([ '/videos/watch', video.uuid ])
143 },
144
145 err => {
146 this.error = 'Cannot update the video.'
147 console.error(err)
148 }
149 )
150
151 }
dc8bc31b 152}