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