]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-edit/video-add.component.ts
Fix upgrade peertube script (bad semver comparison)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-edit / video-add.component.ts
CommitLineData
df98563e
C
1import { Component, ElementRef, OnInit } from '@angular/core'
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { Router } from '@angular/router'
dc8bc31b 4
df98563e
C
5import { FileUploader } from 'ng2-file-upload/ng2-file-upload'
6import { NotificationsService } from 'angular2-notifications'
8140a704 7
df98563e 8import { AuthService } from '../../core'
6e07c3de
C
9import {
10 FormReactive,
11 VIDEO_NAME,
12 VIDEO_CATEGORY,
d07137b9 13 VIDEO_LICENCE,
db216afd 14 VIDEO_LANGUAGE,
6e07c3de
C
15 VIDEO_DESCRIPTION,
16 VIDEO_TAGS
df98563e
C
17} from '../../shared'
18import { VideoService } from '../shared'
1553e15d 19
dc8bc31b
C
20@Component({
21 selector: 'my-videos-add',
d8e689b8 22 styleUrls: [ './video-edit.component.scss' ],
ec8d8440 23 templateUrl: './video-add.component.html'
dc8bc31b
C
24})
25
4b2f33f3 26export class VideoAddComponent extends FormReactive implements OnInit {
df98563e
C
27 tags: string[] = []
28 uploader: FileUploader
29 videoCategories = []
30 videoLicences = []
31 videoLanguages = []
4b2f33f3 32
df98563e
C
33 tagValidators = VIDEO_TAGS.VALIDATORS
34 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
3758da94 35
df98563e
C
36 error: string = null
37 form: FormGroup
4b2f33f3 38 formErrors = {
e822fdae 39 name: '',
6e07c3de 40 category: '',
d07137b9 41 licence: '',
db216afd 42 language: '',
3758da94 43 description: ''
df98563e 44 }
4b2f33f3
C
45 validationMessages = {
46 name: VIDEO_NAME.MESSAGES,
6e07c3de 47 category: VIDEO_CATEGORY.MESSAGES,
d07137b9 48 licence: VIDEO_LICENCE.MESSAGES,
db216afd 49 language: VIDEO_LANGUAGE.MESSAGES,
3758da94 50 description: VIDEO_DESCRIPTION.MESSAGES
df98563e 51 }
dc8bc31b 52
bf57d5ee 53 // Special error messages
df98563e 54 fileError = ''
bf57d5ee 55
df98563e 56 constructor (
9bfe96e1 57 private authService: AuthService,
4fd8aa32 58 private elementRef: ElementRef,
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 () {
e822fdae 68 if (this.uploader.queue.length === 0) {
df98563e 69 return null
e822fdae
C
70 }
71
df98563e 72 return this.uploader.queue[0].file.name
e822fdae
C
73 }
74
df98563e 75 buildForm () {
4b2f33f3
C
76 this.form = this.formBuilder.group({
77 name: [ '', VIDEO_NAME.VALIDATORS ],
92fb909c 78 nsfw: [ false ],
6e07c3de 79 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 80 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
db216afd 81 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
4b2f33f3 82 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
3758da94 83 tags: [ '']
df98563e 84 })
4b2f33f3 85
df98563e 86 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
87 }
88
df98563e
C
89 ngOnInit () {
90 this.videoCategories = this.videoService.videoCategories
91 this.videoLicences = this.videoService.videoLicences
92 this.videoLanguages = this.videoService.videoLanguages
6e07c3de 93
e822fdae
C
94 this.uploader = new FileUploader({
95 authToken: this.authService.getRequestHeaderValue(),
96 queueLimit: 1,
1840c2f7 97 url: API_URL + '/api/v1/videos',
e822fdae 98 removeAfterUpload: true
df98563e 99 })
e822fdae
C
100
101 this.uploader.onBuildItemForm = (item, form) => {
df98563e
C
102 const name = this.form.value['name']
103 const nsfw = this.form.value['nsfw']
104 const category = this.form.value['category']
105 const licence = this.form.value['licence']
106 const language = this.form.value['language']
107 const description = this.form.value['description']
108 const tags = this.form.value['tags']
109
110 form.append('name', name)
111 form.append('category', category)
112 form.append('nsfw', nsfw)
113 form.append('licence', licence)
db216afd
C
114
115 // Language is optional
116 if (language) {
df98563e 117 form.append('language', language)
db216afd
C
118 }
119
df98563e 120 form.append('description', description)
e822fdae 121
3758da94 122 for (let i = 0; i < tags.length; i++) {
df98563e 123 form.append(`tags[${i}]`, tags[i])
e822fdae 124 }
df98563e 125 }
4b2f33f3 126
df98563e 127 this.buildForm()
e822fdae
C
128 }
129
df98563e
C
130 checkForm () {
131 this.forceCheck()
bf57d5ee 132
bf57d5ee 133 if (this.filename === null) {
df98563e 134 this.fileError = 'You did not add a file.'
bf57d5ee
C
135 }
136
df98563e 137 return this.form.valid === true && this.fileError === ''
bf57d5ee
C
138 }
139
df98563e
C
140 fileChanged () {
141 this.fileError = ''
bf57d5ee
C
142 }
143
df98563e
C
144 removeFile () {
145 this.uploader.clearQueue()
e822fdae
C
146 }
147
df98563e 148 upload () {
bf57d5ee 149 if (this.checkForm() === false) {
df98563e 150 return
bf57d5ee
C
151 }
152
df98563e 153 const item = this.uploader.queue[0]
e822fdae 154 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
df98563e 155 item.alias = 'videofile'
1a005042 156
e822fdae 157 item.onSuccess = () => {
df98563e
C
158 console.log('Video uploaded.')
159 this.notificationsService.success('Success', 'Video uploaded.')
e822fdae
C
160
161 // Print all the videos once it's finished
df98563e
C
162 this.router.navigate(['/videos/list'])
163 }
e822fdae
C
164
165 item.onError = (response: string, status: number) => {
bd5c83a8
C
166 // We need to handle manually these cases beceause we use the FileUpload component
167 if (status === 400) {
df98563e 168 this.error = response
bd5c83a8 169 } else if (status === 401) {
df98563e 170 this.error = 'Access token was expired, refreshing token...'
bd5c83a8
C
171 this.authService.refreshAccessToken().subscribe(
172 () => {
173 // Update the uploader request header
df98563e
C
174 this.uploader.authToken = this.authService.getRequestHeaderValue()
175 this.error += ' access token refreshed. Please retry your request.'
bd5c83a8 176 }
df98563e 177 )
bd5c83a8 178 } else {
df98563e
C
179 this.error = 'Unknow error'
180 console.error(this.error)
bd5c83a8 181 }
df98563e 182 }
e822fdae 183
df98563e 184 this.uploader.uploadAll()
dc8bc31b
C
185 }
186}