]>
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' |
cadb46d8 | 6 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
202f6b6c | 7 | import { AuthService, ServerService } from '../../core' |
27e1a06c | 8 | import { FormReactive } from '../../shared' |
63c4db6d | 9 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' |
27e1a06c | 10 | import { VideoEdit } from '../../shared/video/video-edit.model' |
63c4db6d | 11 | import { VideoService } from '../../shared/video/video.service' |
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 |
baeefe22 | 26 | videoUploaded = false |
c182778e | 27 | videoUploadPercents = 0 |
a4b8a4dd C |
28 | videoUploadedIds = { |
29 | id: 0, | |
30 | uuid: '' | |
31 | } | |
3758da94 | 32 | |
27e1a06c | 33 | error: string = null |
df98563e | 34 | form: FormGroup |
27e1a06c C |
35 | formErrors: { [ id: string ]: string } = {} |
36 | validationMessages: ValidatorMessage = {} | |
baeefe22 | 37 | |
27e1a06c C |
38 | userVideoChannels = [] |
39 | videoPrivacies = [] | |
cadb46d8 C |
40 | firstStepPrivacyId = 0 |
41 | firstStepChannelId = 0 | |
dc8bc31b | 42 | |
df98563e | 43 | constructor ( |
4b2f33f3 | 44 | private formBuilder: FormBuilder, |
7ddd02c9 | 45 | private router: Router, |
6e07c3de | 46 | private notificationsService: NotificationsService, |
bcd9f81e | 47 | private authService: AuthService, |
db7af09b | 48 | private serverService: ServerService, |
6e07c3de | 49 | private videoService: VideoService |
4b2f33f3 | 50 | ) { |
df98563e | 51 | super() |
4b2f33f3 | 52 | } |
dc8bc31b | 53 | |
df98563e | 54 | buildForm () { |
27e1a06c | 55 | this.form = this.formBuilder.group({}) |
df98563e | 56 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
57 | } |
58 | ||
df98563e | 59 | ngOnInit () { |
df98563e | 60 | this.buildForm() |
2de96f4d | 61 | |
c182778e | 62 | this.serverService.videoPrivaciesLoaded |
baeefe22 C |
63 | .subscribe( |
64 | () => { | |
65 | this.videoPrivacies = this.serverService.getVideoPrivacies() | |
cadb46d8 C |
66 | |
67 | // Public by default | |
68 | this.firstStepPrivacyId = VideoPrivacy.PUBLIC | |
baeefe22 | 69 | }) |
27e1a06c | 70 | |
2de96f4d C |
71 | this.authService.userInformationLoaded |
72 | .subscribe( | |
73 | () => { | |
74 | const user = this.authService.getUser() | |
75 | if (!user) return | |
76 | ||
77 | const videoChannels = user.videoChannels | |
78 | if (Array.isArray(videoChannels) === false) return | |
79 | ||
80 | this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name })) | |
cadb46d8 | 81 | this.firstStepChannelId = this.userVideoChannels[0].id |
2de96f4d C |
82 | } |
83 | ) | |
e822fdae C |
84 | } |
85 | ||
baeefe22 C |
86 | fileChange () { |
87 | this.uploadFirstStep() | |
bf57d5ee C |
88 | } |
89 | ||
bfb3a98f C |
90 | checkForm () { |
91 | this.forceCheck() | |
92 | ||
93 | return this.form.valid | |
e822fdae C |
94 | } |
95 | ||
27e1a06c | 96 | uploadFirstStep () { |
bfb3a98f | 97 | const videofile = this.videofileInput.nativeElement.files[0] |
cadb46d8 C |
98 | const name = videofile.name.replace(/\.[^/.]+$/, '') |
99 | const privacy = this.firstStepPrivacyId.toString() | |
baeefe22 | 100 | const nsfw = false |
cadb46d8 | 101 | const channelId = this.firstStepChannelId.toString() |
bfb3a98f C |
102 | |
103 | const formData = new FormData() | |
104 | formData.append('name', name) | |
cadb46d8 C |
105 | // Put the video "private" -> we wait he validates the second step |
106 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
bfb3a98f | 107 | formData.append('nsfw', '' + nsfw) |
bcd9f81e | 108 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
109 | formData.append('videofile', videofile) |
110 | ||
baeefe22 C |
111 | this.isUploadingVideo = true |
112 | this.form.patchValue({ | |
113 | name, | |
114 | privacy, | |
115 | nsfw, | |
116 | channelId | |
117 | }) | |
e822fdae | 118 | |
bfb3a98f C |
119 | this.videoService.uploadVideo(formData).subscribe( |
120 | event => { | |
121 | if (event.type === HttpEventType.UploadProgress) { | |
c182778e | 122 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) |
bfb3a98f C |
123 | } else if (event instanceof HttpResponse) { |
124 | console.log('Video uploaded.') | |
bfb3a98f | 125 | |
baeefe22 | 126 | this.videoUploaded = true |
cadb46d8 | 127 | |
a4b8a4dd | 128 | this.videoUploadedIds = event.body.video |
bfb3a98f C |
129 | } |
130 | }, | |
131 | ||
315cc0cc C |
132 | err => { |
133 | // Reset progress | |
c182778e | 134 | this.videoUploadPercents = 0 |
315cc0cc C |
135 | this.error = err.message |
136 | } | |
bfb3a98f | 137 | ) |
dc8bc31b | 138 | } |
27e1a06c C |
139 | |
140 | updateSecondStep () { | |
141 | if (this.checkForm() === false) { | |
142 | return | |
143 | } | |
144 | ||
cadb46d8 C |
145 | const video = new VideoEdit() |
146 | video.patch(this.form.value) | |
147 | video.channel = this.firstStepChannelId | |
a4b8a4dd C |
148 | video.id = this.videoUploadedIds.id |
149 | video.uuid = this.videoUploadedIds.uuid | |
27e1a06c C |
150 | |
151 | this.videoService.updateVideo(video) | |
152 | .subscribe( | |
153 | () => { | |
154 | this.notificationsService.success('Success', 'Video published.') | |
a4b8a4dd | 155 | this.router.navigate([ '/videos/watch', video.uuid ]) |
27e1a06c C |
156 | }, |
157 | ||
158 | err => { | |
159 | this.error = 'Cannot update the video.' | |
160 | console.error(err) | |
161 | } | |
162 | ) | |
163 | ||
164 | } | |
dc8bc31b | 165 | } |