]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-edit/video-add.component.ts
Improve real world script
[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'
4771e000 19import { VideoCreate } from '../../../../../shared'
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 {
df98563e
C
28 tags: string[] = []
29 uploader: FileUploader
30 videoCategories = []
31 videoLicences = []
32 videoLanguages = []
4b2f33f3 33
df98563e
C
34 tagValidators = VIDEO_TAGS.VALIDATORS
35 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
3758da94 36
df98563e
C
37 error: string = null
38 form: FormGroup
4b2f33f3 39 formErrors = {
e822fdae 40 name: '',
6e07c3de 41 category: '',
d07137b9 42 licence: '',
db216afd 43 language: '',
3758da94 44 description: ''
df98563e 45 }
4b2f33f3
C
46 validationMessages = {
47 name: VIDEO_NAME.MESSAGES,
6e07c3de 48 category: VIDEO_CATEGORY.MESSAGES,
d07137b9 49 licence: VIDEO_LICENCE.MESSAGES,
db216afd 50 language: VIDEO_LANGUAGE.MESSAGES,
3758da94 51 description: VIDEO_DESCRIPTION.MESSAGES
df98563e 52 }
dc8bc31b 53
bf57d5ee 54 // Special error messages
df98563e 55 fileError = ''
bf57d5ee 56
df98563e 57 constructor (
9bfe96e1 58 private authService: AuthService,
4fd8aa32 59 private elementRef: ElementRef,
4b2f33f3 60 private formBuilder: FormBuilder,
7ddd02c9 61 private router: Router,
6e07c3de
C
62 private notificationsService: NotificationsService,
63 private videoService: VideoService
4b2f33f3 64 ) {
df98563e 65 super()
4b2f33f3 66 }
dc8bc31b 67
df98563e 68 get filename () {
e822fdae 69 if (this.uploader.queue.length === 0) {
df98563e 70 return null
e822fdae
C
71 }
72
df98563e 73 return this.uploader.queue[0].file.name
e822fdae
C
74 }
75
df98563e 76 buildForm () {
4b2f33f3
C
77 this.form = this.formBuilder.group({
78 name: [ '', VIDEO_NAME.VALIDATORS ],
92fb909c 79 nsfw: [ false ],
6e07c3de 80 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 81 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
db216afd 82 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
4b2f33f3 83 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
3758da94 84 tags: [ '']
df98563e 85 })
4b2f33f3 86
df98563e 87 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
88 }
89
df98563e
C
90 ngOnInit () {
91 this.videoCategories = this.videoService.videoCategories
92 this.videoLicences = this.videoService.videoLicences
93 this.videoLanguages = this.videoService.videoLanguages
6e07c3de 94
e822fdae
C
95 this.uploader = new FileUploader({
96 authToken: this.authService.getRequestHeaderValue(),
97 queueLimit: 1,
e95561cd 98 url: API_URL + '/api/v1/videos/upload',
e822fdae 99 removeAfterUpload: true
df98563e 100 })
e822fdae 101
4771e000
C
102 this.uploader.onBuildItemForm = (item, form: FormData) => {
103 const formValue: VideoCreate = this.form.value
104
105 const name = formValue.name
106 const nsfw = formValue.nsfw
107 const category = formValue.category
108 const licence = formValue.licence
109 const language = formValue.language
110 const description = formValue.description
111 const tags = formValue.tags
df98563e
C
112
113 form.append('name', name)
4771e000
C
114 form.append('category', '' + category)
115 form.append('nsfw', '' + nsfw)
116 form.append('licence', '' + licence)
db216afd
C
117
118 // Language is optional
119 if (language) {
4771e000 120 form.append('language', '' + language)
db216afd
C
121 }
122
df98563e 123 form.append('description', description)
e822fdae 124
3758da94 125 for (let i = 0; i < tags.length; i++) {
df98563e 126 form.append(`tags[${i}]`, tags[i])
e822fdae 127 }
df98563e 128 }
4b2f33f3 129
df98563e 130 this.buildForm()
e822fdae
C
131 }
132
df98563e
C
133 checkForm () {
134 this.forceCheck()
bf57d5ee 135
bf57d5ee 136 if (this.filename === null) {
df98563e 137 this.fileError = 'You did not add a file.'
bf57d5ee
C
138 }
139
df98563e 140 return this.form.valid === true && this.fileError === ''
bf57d5ee
C
141 }
142
df98563e
C
143 fileChanged () {
144 this.fileError = ''
bf57d5ee
C
145 }
146
df98563e
C
147 removeFile () {
148 this.uploader.clearQueue()
e822fdae
C
149 }
150
df98563e 151 upload () {
bf57d5ee 152 if (this.checkForm() === false) {
df98563e 153 return
bf57d5ee
C
154 }
155
df98563e 156 const item = this.uploader.queue[0]
e822fdae 157 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
df98563e 158 item.alias = 'videofile'
1a005042 159
e822fdae 160 item.onSuccess = () => {
df98563e
C
161 console.log('Video uploaded.')
162 this.notificationsService.success('Success', 'Video uploaded.')
e822fdae
C
163
164 // Print all the videos once it's finished
df98563e
C
165 this.router.navigate(['/videos/list'])
166 }
e822fdae
C
167
168 item.onError = (response: string, status: number) => {
e95561cd 169 // We need to handle manually these cases because we use the FileUpload component
bd5c83a8 170 if (status === 400) {
df98563e 171 this.error = response
bd5c83a8 172 } else if (status === 401) {
df98563e 173 this.error = 'Access token was expired, refreshing token...'
bd5c83a8
C
174 this.authService.refreshAccessToken().subscribe(
175 () => {
176 // Update the uploader request header
df98563e
C
177 this.uploader.authToken = this.authService.getRequestHeaderValue()
178 this.error += ' access token refreshed. Please retry your request.'
bd5c83a8 179 }
df98563e 180 )
a8b5de6c
C
181 } else if (status === 403) {
182 this.error = 'Your video quota is reached, you can\'t upload this video.'
bd5c83a8 183 } else {
e95561cd 184 this.error = 'Unknown error'
df98563e 185 console.error(this.error)
bd5c83a8 186 }
df98563e 187 }
e822fdae 188
df98563e 189 this.uploader.uploadAll()
dc8bc31b
C
190 }
191}