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