]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-edit/video-add.component.ts
Add info when server is processing a video at upload
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-edit / video-add.component.ts
CommitLineData
bfb3a98f 1import { Component, OnInit, ViewChild } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { Router } from '@angular/router'
dc8bc31b 4
df98563e 5import { NotificationsService } from 'angular2-notifications'
8140a704 6
6e07c3de
C
7import {
8 FormReactive,
9 VIDEO_NAME,
10 VIDEO_CATEGORY,
d07137b9 11 VIDEO_LICENCE,
db216afd 12 VIDEO_LANGUAGE,
6e07c3de
C
13 VIDEO_DESCRIPTION,
14 VIDEO_TAGS
df98563e
C
15} from '../../shared'
16import { VideoService } from '../shared'
4771e000 17import { VideoCreate } from '../../../../../shared'
bfb3a98f
C
18import { HttpEventType, HttpResponse } from '@angular/common/http'
19import { VIDEO_FILE } from '../../shared/forms/form-validators/video'
1553e15d 20
dc8bc31b
C
21@Component({
22 selector: 'my-videos-add',
d8e689b8 23 styleUrls: [ './video-edit.component.scss' ],
ec8d8440 24 templateUrl: './video-add.component.html'
dc8bc31b
C
25})
26
4b2f33f3 27export class VideoAddComponent extends FormReactive implements OnInit {
bfb3a98f
C
28 @ViewChild('videofileInput') videofileInput
29
30 progressPercent = 0
df98563e 31 tags: string[] = []
df98563e
C
32 videoCategories = []
33 videoLicences = []
34 videoLanguages = []
4b2f33f3 35
df98563e
C
36 tagValidators = VIDEO_TAGS.VALIDATORS
37 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
3758da94 38
bfb3a98f 39 error: string
df98563e 40 form: FormGroup
4b2f33f3 41 formErrors = {
e822fdae 42 name: '',
6e07c3de 43 category: '',
d07137b9 44 licence: '',
db216afd 45 language: '',
bfb3a98f
C
46 description: '',
47 videofile: ''
df98563e 48 }
4b2f33f3
C
49 validationMessages = {
50 name: VIDEO_NAME.MESSAGES,
6e07c3de 51 category: VIDEO_CATEGORY.MESSAGES,
d07137b9 52 licence: VIDEO_LICENCE.MESSAGES,
db216afd 53 language: VIDEO_LANGUAGE.MESSAGES,
bfb3a98f
C
54 description: VIDEO_DESCRIPTION.MESSAGES,
55 videofile: VIDEO_FILE.MESSAGES
df98563e 56 }
dc8bc31b 57
df98563e 58 constructor (
4b2f33f3 59 private formBuilder: FormBuilder,
7ddd02c9 60 private router: Router,
6e07c3de
C
61 private notificationsService: NotificationsService,
62 private videoService: VideoService
4b2f33f3 63 ) {
df98563e 64 super()
4b2f33f3 65 }
dc8bc31b 66
df98563e 67 get filename () {
bfb3a98f 68 return this.form.value['videofile']
e822fdae
C
69 }
70
df98563e 71 buildForm () {
4b2f33f3
C
72 this.form = this.formBuilder.group({
73 name: [ '', VIDEO_NAME.VALIDATORS ],
92fb909c 74 nsfw: [ false ],
6e07c3de 75 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 76 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
db216afd 77 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
4b2f33f3 78 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
bfb3a98f
C
79 videofile: [ '', VIDEO_FILE.VALIDATORS ],
80 tags: [ '' ]
df98563e 81 })
4b2f33f3 82
df98563e 83 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
84 }
85
df98563e
C
86 ngOnInit () {
87 this.videoCategories = this.videoService.videoCategories
88 this.videoLicences = this.videoService.videoLicences
89 this.videoLanguages = this.videoService.videoLanguages
6e07c3de 90
df98563e 91 this.buildForm()
e822fdae
C
92 }
93
bfb3a98f
C
94 // The goal is to keep reactive form validation (required field)
95 // https://stackoverflow.com/a/44238894
96 fileChange ($event) {
97 this.form.controls['videofile'].setValue($event.target.files[0].name)
bf57d5ee
C
98 }
99
bfb3a98f
C
100 removeFile () {
101 this.videofileInput.nativeElement.value = ''
102 this.form.controls['videofile'].setValue('')
bf57d5ee
C
103 }
104
bfb3a98f
C
105 checkForm () {
106 this.forceCheck()
107
108 return this.form.valid
e822fdae
C
109 }
110
df98563e 111 upload () {
bf57d5ee 112 if (this.checkForm() === false) {
df98563e 113 return
bf57d5ee
C
114 }
115
bfb3a98f
C
116 const formValue: VideoCreate = this.form.value
117
118 const name = formValue.name
119 const nsfw = formValue.nsfw
120 const category = formValue.category
121 const licence = formValue.licence
122 const language = formValue.language
123 const description = formValue.description
124 const tags = formValue.tags
125 const videofile = this.videofileInput.nativeElement.files[0]
126
127 const formData = new FormData()
128 formData.append('name', name)
129 formData.append('category', '' + category)
130 formData.append('nsfw', '' + nsfw)
131 formData.append('licence', '' + licence)
132 formData.append('videofile', videofile)
133
134 // Language is optional
135 if (language) {
136 formData.append('language', '' + language)
df98563e 137 }
e822fdae 138
bfb3a98f
C
139 formData.append('description', description)
140
141 for (let i = 0; i < tags.length; i++) {
142 formData.append(`tags[${i}]`, tags[i])
df98563e 143 }
e822fdae 144
bfb3a98f
C
145 this.videoService.uploadVideo(formData).subscribe(
146 event => {
147 if (event.type === HttpEventType.UploadProgress) {
148 this.progressPercent = Math.round(100 * event.loaded / event.total)
149 } else if (event instanceof HttpResponse) {
150 console.log('Video uploaded.')
151 this.notificationsService.success('Success', 'Video uploaded.')
152
153 // Display all the videos once it's finished
315cc0cc 154 this.router.navigate([ '/videos/list' ])
bfb3a98f
C
155 }
156 },
157
315cc0cc
C
158 err => {
159 // Reset progress
160 this.progressPercent = 0
161 this.error = err.message
162 }
bfb3a98f 163 )
dc8bc31b
C
164 }
165}