]>
Commit | Line | Data |
---|---|---|
202f6b6c | 1 | import { HttpEventType, HttpResponse } from '@angular/common/http' |
bfb3a98f | 2 | import { Component, OnInit, ViewChild } from '@angular/core' |
df98563e C |
3 | import { FormBuilder, FormGroup } from '@angular/forms' |
4 | import { Router } from '@angular/router' | |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
202f6b6c C |
6 | import { VideoService } from 'app/shared/video/video.service' |
7 | import { VideoCreate } from '../../../../../shared' | |
8 | import { AuthService, ServerService } from '../../core' | |
27e1a06c C |
9 | import { FormReactive } from '../../shared' |
10 | import { ValidatorMessage } from '../../shared/forms/form-validators' | |
11 | import { VideoEdit } from '../../shared/video/video-edit.model' | |
1553e15d | 12 | |
dc8bc31b C |
13 | @Component({ |
14 | selector: 'my-videos-add', | |
27e1a06c C |
15 | templateUrl: './video-add.component.html', |
16 | styleUrls: [ | |
17 | './shared/video-edit.component.scss', | |
18 | './video-add.component.scss' | |
19 | ] | |
dc8bc31b C |
20 | }) |
21 | ||
4b2f33f3 | 22 | export class VideoAddComponent extends FormReactive implements OnInit { |
bfb3a98f C |
23 | @ViewChild('videofileInput') videofileInput |
24 | ||
27e1a06c | 25 | isUploadingVideo = false |
bfb3a98f | 26 | progressPercent = 0 |
3758da94 | 27 | |
27e1a06c | 28 | error: string = null |
df98563e | 29 | form: FormGroup |
27e1a06c C |
30 | formErrors: { [ id: string ]: string } = {} |
31 | validationMessages: ValidatorMessage = {} | |
32 | userVideoChannels = [] | |
33 | videoPrivacies = [] | |
34 | firstStepPrivacy = 0 | |
35 | firstStepChannel = 0 | |
dc8bc31b | 36 | |
df98563e | 37 | constructor ( |
4b2f33f3 | 38 | private formBuilder: FormBuilder, |
7ddd02c9 | 39 | private router: Router, |
6e07c3de | 40 | private notificationsService: NotificationsService, |
bcd9f81e | 41 | private authService: AuthService, |
db7af09b | 42 | private serverService: ServerService, |
6e07c3de | 43 | private videoService: VideoService |
4b2f33f3 | 44 | ) { |
df98563e | 45 | super() |
4b2f33f3 | 46 | } |
dc8bc31b | 47 | |
df98563e | 48 | buildForm () { |
27e1a06c | 49 | this.form = this.formBuilder.group({}) |
df98563e | 50 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
51 | } |
52 | ||
df98563e | 53 | ngOnInit () { |
df98563e | 54 | this.buildForm() |
2de96f4d | 55 | |
27e1a06c C |
56 | this.videoPrivacies = this.serverService.getVideoPrivacies() |
57 | this.firstStepPrivacy = this.videoPrivacies[0].id | |
58 | ||
2de96f4d C |
59 | this.authService.userInformationLoaded |
60 | .subscribe( | |
61 | () => { | |
62 | const user = this.authService.getUser() | |
63 | if (!user) return | |
64 | ||
65 | const videoChannels = user.videoChannels | |
66 | if (Array.isArray(videoChannels) === false) return | |
67 | ||
68 | this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name })) | |
27e1a06c | 69 | this.firstStepChannel = this.userVideoChannels[0].id |
2de96f4d C |
70 | } |
71 | ) | |
e822fdae C |
72 | } |
73 | ||
bfb3a98f | 74 | fileChange ($event) { |
27e1a06c | 75 | console.log('uploading file ?') |
bf57d5ee C |
76 | } |
77 | ||
bfb3a98f C |
78 | checkForm () { |
79 | this.forceCheck() | |
80 | ||
81 | return this.form.valid | |
e822fdae C |
82 | } |
83 | ||
27e1a06c | 84 | uploadFirstStep () { |
bfb3a98f C |
85 | const formValue: VideoCreate = this.form.value |
86 | ||
87 | const name = formValue.name | |
fd45e8f4 | 88 | const privacy = formValue.privacy |
bfb3a98f C |
89 | const nsfw = formValue.nsfw |
90 | const category = formValue.category | |
91 | const licence = formValue.licence | |
92 | const language = formValue.language | |
bcd9f81e | 93 | const channelId = formValue.channelId |
bfb3a98f C |
94 | const description = formValue.description |
95 | const tags = formValue.tags | |
96 | const videofile = this.videofileInput.nativeElement.files[0] | |
97 | ||
98 | const formData = new FormData() | |
99 | formData.append('name', name) | |
fd45e8f4 | 100 | formData.append('privacy', privacy.toString()) |
bfb3a98f C |
101 | formData.append('category', '' + category) |
102 | formData.append('nsfw', '' + nsfw) | |
103 | formData.append('licence', '' + licence) | |
bcd9f81e | 104 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
105 | formData.append('videofile', videofile) |
106 | ||
107 | // Language is optional | |
108 | if (language) { | |
109 | formData.append('language', '' + language) | |
df98563e | 110 | } |
e822fdae | 111 | |
bfb3a98f C |
112 | formData.append('description', description) |
113 | ||
114 | for (let i = 0; i < tags.length; i++) { | |
115 | formData.append(`tags[${i}]`, tags[i]) | |
df98563e | 116 | } |
e822fdae | 117 | |
bfb3a98f C |
118 | this.videoService.uploadVideo(formData).subscribe( |
119 | event => { | |
120 | if (event.type === HttpEventType.UploadProgress) { | |
121 | this.progressPercent = Math.round(100 * event.loaded / event.total) | |
122 | } else if (event instanceof HttpResponse) { | |
123 | console.log('Video uploaded.') | |
124 | this.notificationsService.success('Success', 'Video uploaded.') | |
125 | ||
126 | // Display all the videos once it's finished | |
9bf9d2a5 | 127 | this.router.navigate([ '/videos/trending' ]) |
bfb3a98f C |
128 | } |
129 | }, | |
130 | ||
315cc0cc C |
131 | err => { |
132 | // Reset progress | |
133 | this.progressPercent = 0 | |
134 | this.error = err.message | |
135 | } | |
bfb3a98f | 136 | ) |
dc8bc31b | 137 | } |
27e1a06c C |
138 | |
139 | updateSecondStep () { | |
140 | if (this.checkForm() === false) { | |
141 | return | |
142 | } | |
143 | ||
144 | const video = new VideoEdit(this.form.value) | |
145 | ||
146 | this.videoService.updateVideo(video) | |
147 | .subscribe( | |
148 | () => { | |
149 | this.notificationsService.success('Success', 'Video published.') | |
150 | this.router.navigate([ '/videos/watch', video.uuid ]) | |
151 | }, | |
152 | ||
153 | err => { | |
154 | this.error = 'Cannot update the video.' | |
155 | console.error(err) | |
156 | } | |
157 | ) | |
158 | ||
159 | } | |
dc8bc31b | 160 | } |