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