aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/video-edit/video-add.component.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-09-14 17:06:31 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-09-14 17:25:36 +0200
commitbfb3a98fac582f104c6d9b8b7242ea2cbb650b91 (patch)
tree24ead7cbbdc785315c406dfba85a55fe5e235d09 /client/src/app/videos/video-edit/video-add.component.ts
parentd5050d1e097e761685fbaafe6e3d4b8b78d48356 (diff)
downloadPeerTube-bfb3a98fac582f104c6d9b8b7242ea2cbb650b91.tar.gz
PeerTube-bfb3a98fac582f104c6d9b8b7242ea2cbb650b91.tar.zst
PeerTube-bfb3a98fac582f104c6d9b8b7242ea2cbb650b91.zip
Remove ng2 file upload module
Unmaintained and we don't need it anymore with httpclient
Diffstat (limited to 'client/src/app/videos/video-edit/video-add.component.ts')
-rw-r--r--client/src/app/videos/video-edit/video-add.component.ts160
1 files changed, 65 insertions, 95 deletions
diff --git a/client/src/app/videos/video-edit/video-add.component.ts b/client/src/app/videos/video-edit/video-add.component.ts
index 42b11cd08..537ef9bc3 100644
--- a/client/src/app/videos/video-edit/video-add.component.ts
+++ b/client/src/app/videos/video-edit/video-add.component.ts
@@ -1,11 +1,9 @@
1import { Component, ElementRef, OnInit } from '@angular/core' 1import { Component, OnInit, ViewChild } from '@angular/core'
2import { FormBuilder, FormGroup } from '@angular/forms' 2import { FormBuilder, FormGroup } from '@angular/forms'
3import { Router } from '@angular/router' 3import { Router } from '@angular/router'
4 4
5import { FileUploader } from 'ng2-file-upload/ng2-file-upload'
6import { NotificationsService } from 'angular2-notifications' 5import { NotificationsService } from 'angular2-notifications'
7 6
8import { AuthService } from '../../core'
9import { 7import {
10 FormReactive, 8 FormReactive,
11 VIDEO_NAME, 9 VIDEO_NAME,
@@ -17,6 +15,8 @@ import {
17} from '../../shared' 15} from '../../shared'
18import { VideoService } from '../shared' 16import { VideoService } from '../shared'
19import { VideoCreate } from '../../../../../shared' 17import { VideoCreate } from '../../../../../shared'
18import { HttpEventType, HttpResponse } from '@angular/common/http'
19import { VIDEO_FILE } from '../../shared/forms/form-validators/video'
20 20
21@Component({ 21@Component({
22 selector: 'my-videos-add', 22 selector: 'my-videos-add',
@@ -25,8 +25,10 @@ import { VideoCreate } from '../../../../../shared'
25}) 25})
26 26
27export class VideoAddComponent extends FormReactive implements OnInit { 27export class VideoAddComponent extends FormReactive implements OnInit {
28 @ViewChild('videofileInput') videofileInput
29
30 progressPercent = 0
28 tags: string[] = [] 31 tags: string[] = []
29 uploader: FileUploader
30 videoCategories = [] 32 videoCategories = []
31 videoLicences = [] 33 videoLicences = []
32 videoLanguages = [] 34 videoLanguages = []
@@ -34,29 +36,26 @@ export class VideoAddComponent extends FormReactive implements OnInit {
34 tagValidators = VIDEO_TAGS.VALIDATORS 36 tagValidators = VIDEO_TAGS.VALIDATORS
35 tagValidatorsMessages = VIDEO_TAGS.MESSAGES 37 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
36 38
37 error: string = null 39 error: string
38 form: FormGroup 40 form: FormGroup
39 formErrors = { 41 formErrors = {
40 name: '', 42 name: '',
41 category: '', 43 category: '',
42 licence: '', 44 licence: '',
43 language: '', 45 language: '',
44 description: '' 46 description: '',
47 videofile: ''
45 } 48 }
46 validationMessages = { 49 validationMessages = {
47 name: VIDEO_NAME.MESSAGES, 50 name: VIDEO_NAME.MESSAGES,
48 category: VIDEO_CATEGORY.MESSAGES, 51 category: VIDEO_CATEGORY.MESSAGES,
49 licence: VIDEO_LICENCE.MESSAGES, 52 licence: VIDEO_LICENCE.MESSAGES,
50 language: VIDEO_LANGUAGE.MESSAGES, 53 language: VIDEO_LANGUAGE.MESSAGES,
51 description: VIDEO_DESCRIPTION.MESSAGES 54 description: VIDEO_DESCRIPTION.MESSAGES,
55 videofile: VIDEO_FILE.MESSAGES
52 } 56 }
53 57
54 // Special error messages
55 fileError = ''
56
57 constructor ( 58 constructor (
58 private authService: AuthService,
59 private elementRef: ElementRef,
60 private formBuilder: FormBuilder, 59 private formBuilder: FormBuilder,
61 private router: Router, 60 private router: Router,
62 private notificationsService: NotificationsService, 61 private notificationsService: NotificationsService,
@@ -66,11 +65,7 @@ export class VideoAddComponent extends FormReactive implements OnInit {
66 } 65 }
67 66
68 get filename () { 67 get filename () {
69 if (this.uploader.queue.length === 0) { 68 return this.form.value['videofile']
70 return null
71 }
72
73 return this.uploader.queue[0].file.name
74 } 69 }
75 70
76 buildForm () { 71 buildForm () {
@@ -81,7 +76,8 @@ export class VideoAddComponent extends FormReactive implements OnInit {
81 licence: [ '', VIDEO_LICENCE.VALIDATORS ], 76 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
82 language: [ '', VIDEO_LANGUAGE.VALIDATORS ], 77 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
83 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], 78 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
84 tags: [ ''] 79 videofile: [ '', VIDEO_FILE.VALIDATORS ],
80 tags: [ '' ]
85 }) 81 })
86 82
87 this.form.valueChanges.subscribe(data => this.onValueChanged(data)) 83 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
@@ -92,60 +88,24 @@ export class VideoAddComponent extends FormReactive implements OnInit {
92 this.videoLicences = this.videoService.videoLicences 88 this.videoLicences = this.videoService.videoLicences
93 this.videoLanguages = this.videoService.videoLanguages 89 this.videoLanguages = this.videoService.videoLanguages
94 90
95 this.uploader = new FileUploader({
96 authToken: this.authService.getRequestHeaderValue(),
97 queueLimit: 1,
98 url: API_URL + '/api/v1/videos/upload',
99 removeAfterUpload: true
100 })
101
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
112
113 form.append('name', name)
114 form.append('category', '' + category)
115 form.append('nsfw', '' + nsfw)
116 form.append('licence', '' + licence)
117
118 // Language is optional
119 if (language) {
120 form.append('language', '' + language)
121 }
122
123 form.append('description', description)
124
125 for (let i = 0; i < tags.length; i++) {
126 form.append(`tags[${i}]`, tags[i])
127 }
128 }
129
130 this.buildForm() 91 this.buildForm()
131 } 92 }
132 93
133 checkForm () { 94 // The goal is to keep reactive form validation (required field)
134 this.forceCheck() 95 // https://stackoverflow.com/a/44238894
135 96 fileChange ($event) {
136 if (this.filename === null) { 97 this.form.controls['videofile'].setValue($event.target.files[0].name)
137 this.fileError = 'You did not add a file.'
138 }
139
140 return this.form.valid === true && this.fileError === ''
141 } 98 }
142 99
143 fileChanged () { 100 removeFile () {
144 this.fileError = '' 101 this.videofileInput.nativeElement.value = ''
102 this.form.controls['videofile'].setValue('')
145 } 103 }
146 104
147 removeFile () { 105 checkForm () {
148 this.uploader.clearQueue() 106 this.forceCheck()
107
108 return this.form.valid
149 } 109 }
150 110
151 upload () { 111 upload () {
@@ -153,39 +113,49 @@ export class VideoAddComponent extends FormReactive implements OnInit {
153 return 113 return
154 } 114 }
155 115
156 const item = this.uploader.queue[0] 116 const formValue: VideoCreate = this.form.value
157 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242 117
158 item.alias = 'videofile' 118 const name = formValue.name
159 119 const nsfw = formValue.nsfw
160 item.onSuccess = () => { 120 const category = formValue.category
161 console.log('Video uploaded.') 121 const licence = formValue.licence
162 this.notificationsService.success('Success', 'Video uploaded.') 122 const language = formValue.language
163 123 const description = formValue.description
164 // Print all the videos once it's finished 124 const tags = formValue.tags
165 this.router.navigate(['/videos/list']) 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)
166 } 137 }
167 138
168 item.onError = (response: string, status: number) => { 139 formData.append('description', description)
169 // We need to handle manually these cases because we use the FileUpload component 140
170 if (status === 400) { 141 for (let i = 0; i < tags.length; i++) {
171 this.error = response 142 formData.append(`tags[${i}]`, tags[i])
172 } else if (status === 401) {
173 this.error = 'Access token was expired, refreshing token...'
174 this.authService.refreshAccessToken().subscribe(
175 () => {
176 // Update the uploader request header
177 this.uploader.authToken = this.authService.getRequestHeaderValue()
178 this.error += ' access token refreshed. Please retry your request.'
179 }
180 )
181 } else if (status === 403) {
182 this.error = 'Your video quota is reached, you can\'t upload this video.'
183 } else {
184 this.error = 'Unknown error'
185 console.error(this.error)
186 }
187 } 143 }
188 144
189 this.uploader.uploadAll() 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
154 this.router.navigate([ '/videos/list '])
155 }
156 },
157
158 err => this.error = err.message
159 )
190 } 160 }
191} 161}