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