]>
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' | |
ce5496d6 | 5 | import { UserService } from '@app/shared' |
df98563e | 6 | import { NotificationsService } from 'angular2-notifications' |
ce5496d6 | 7 | import { BytesPipe } from 'ngx-pipes' |
cadb46d8 | 8 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
202f6b6c | 9 | import { AuthService, ServerService } from '../../core' |
27e1a06c | 10 | import { FormReactive } from '../../shared' |
63c4db6d | 11 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' |
15a7387d | 12 | import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' |
27e1a06c | 13 | import { VideoEdit } from '../../shared/video/video-edit.model' |
63c4db6d | 14 | import { VideoService } from '../../shared/video/video.service' |
1553e15d | 15 | |
dc8bc31b C |
16 | @Component({ |
17 | selector: 'my-videos-add', | |
27e1a06c C |
18 | templateUrl: './video-add.component.html', |
19 | styleUrls: [ | |
20 | './shared/video-edit.component.scss', | |
21 | './video-add.component.scss' | |
22 | ] | |
dc8bc31b C |
23 | }) |
24 | ||
4b2f33f3 | 25 | export class VideoAddComponent extends FormReactive implements OnInit { |
bfb3a98f C |
26 | @ViewChild('videofileInput') videofileInput |
27 | ||
27e1a06c | 28 | isUploadingVideo = false |
baeefe22 | 29 | videoUploaded = false |
c182778e | 30 | videoUploadPercents = 0 |
a4b8a4dd C |
31 | videoUploadedIds = { |
32 | id: 0, | |
33 | uuid: '' | |
34 | } | |
3758da94 | 35 | |
df98563e | 36 | form: FormGroup |
27e1a06c C |
37 | formErrors: { [ id: string ]: string } = {} |
38 | validationMessages: ValidatorMessage = {} | |
baeefe22 | 39 | |
27e1a06c | 40 | userVideoChannels = [] |
ce5496d6 | 41 | userVideoQuotaUsed = 0 |
27e1a06c | 42 | videoPrivacies = [] |
cadb46d8 C |
43 | firstStepPrivacyId = 0 |
44 | firstStepChannelId = 0 | |
dc8bc31b | 45 | |
df98563e | 46 | constructor ( |
4b2f33f3 | 47 | private formBuilder: FormBuilder, |
7ddd02c9 | 48 | private router: Router, |
6e07c3de | 49 | private notificationsService: NotificationsService, |
bcd9f81e | 50 | private authService: AuthService, |
ce5496d6 | 51 | private userService: UserService, |
db7af09b | 52 | private serverService: ServerService, |
6e07c3de | 53 | private videoService: VideoService |
4b2f33f3 | 54 | ) { |
df98563e | 55 | super() |
4b2f33f3 | 56 | } |
dc8bc31b | 57 | |
108af661 C |
58 | get videoExtensions () { |
59 | return this.serverService.getConfig().video.file.extensions.join(',') | |
60 | } | |
61 | ||
df98563e | 62 | buildForm () { |
27e1a06c | 63 | this.form = this.formBuilder.group({}) |
df98563e | 64 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
65 | } |
66 | ||
df98563e | 67 | ngOnInit () { |
df98563e | 68 | this.buildForm() |
2de96f4d | 69 | |
15a7387d C |
70 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) |
71 | .then(() => this.firstStepChannelId = this.userVideoChannels[0].id) | |
72 | ||
ce5496d6 C |
73 | this.userService.getMyVideoQuotaUsed() |
74 | .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed) | |
75 | ||
c182778e | 76 | this.serverService.videoPrivaciesLoaded |
baeefe22 C |
77 | .subscribe( |
78 | () => { | |
79 | this.videoPrivacies = this.serverService.getVideoPrivacies() | |
cadb46d8 C |
80 | |
81 | // Public by default | |
82 | this.firstStepPrivacyId = VideoPrivacy.PUBLIC | |
baeefe22 | 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] |
a22bfc3e C |
98 | if (!videofile) return |
99 | ||
ce5496d6 | 100 | const videoQuota = this.authService.getUser().videoQuota |
a22bfc3e | 101 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { |
ce5496d6 C |
102 | const bytePipes = new BytesPipe() |
103 | ||
104 | const msg = 'Your video quota is exceeded with this video ' + | |
105 | `(video size: ${bytePipes.transform(videofile.size, 0)}, ` + | |
106 | `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` + | |
107 | `quota: ${bytePipes.transform(videoQuota, 0)})` | |
108 | this.notificationsService.error('Error', msg) | |
109 | return | |
110 | } | |
111 | ||
cadb46d8 C |
112 | const name = videofile.name.replace(/\.[^/.]+$/, '') |
113 | const privacy = this.firstStepPrivacyId.toString() | |
baeefe22 | 114 | const nsfw = false |
47564bbe | 115 | const commentsEnabled = true |
cadb46d8 | 116 | const channelId = this.firstStepChannelId.toString() |
bfb3a98f C |
117 | |
118 | const formData = new FormData() | |
119 | formData.append('name', name) | |
cadb46d8 C |
120 | // Put the video "private" -> we wait he validates the second step |
121 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
bfb3a98f | 122 | formData.append('nsfw', '' + nsfw) |
47564bbe | 123 | formData.append('commentsEnabled', '' + commentsEnabled) |
bcd9f81e | 124 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
125 | formData.append('videofile', videofile) |
126 | ||
baeefe22 C |
127 | this.isUploadingVideo = true |
128 | this.form.patchValue({ | |
129 | name, | |
130 | privacy, | |
131 | nsfw, | |
132 | channelId | |
133 | }) | |
e822fdae | 134 | |
bfb3a98f C |
135 | this.videoService.uploadVideo(formData).subscribe( |
136 | event => { | |
137 | if (event.type === HttpEventType.UploadProgress) { | |
c182778e | 138 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) |
bfb3a98f C |
139 | } else if (event instanceof HttpResponse) { |
140 | console.log('Video uploaded.') | |
bfb3a98f | 141 | |
baeefe22 | 142 | this.videoUploaded = true |
cadb46d8 | 143 | |
a4b8a4dd | 144 | this.videoUploadedIds = event.body.video |
bfb3a98f C |
145 | } |
146 | }, | |
147 | ||
315cc0cc C |
148 | err => { |
149 | // Reset progress | |
ce5496d6 | 150 | this.isUploadingVideo = false |
c182778e | 151 | this.videoUploadPercents = 0 |
ce5496d6 | 152 | this.notificationsService.error('Error', err.message) |
315cc0cc | 153 | } |
bfb3a98f | 154 | ) |
dc8bc31b | 155 | } |
27e1a06c C |
156 | |
157 | updateSecondStep () { | |
158 | if (this.checkForm() === false) { | |
159 | return | |
160 | } | |
161 | ||
cadb46d8 C |
162 | const video = new VideoEdit() |
163 | video.patch(this.form.value) | |
164 | video.channel = this.firstStepChannelId | |
a4b8a4dd C |
165 | video.id = this.videoUploadedIds.id |
166 | video.uuid = this.videoUploadedIds.uuid | |
27e1a06c C |
167 | |
168 | this.videoService.updateVideo(video) | |
169 | .subscribe( | |
170 | () => { | |
171 | this.notificationsService.success('Success', 'Video published.') | |
a4b8a4dd | 172 | this.router.navigate([ '/videos/watch', video.uuid ]) |
27e1a06c C |
173 | }, |
174 | ||
175 | err => { | |
ce5496d6 | 176 | this.notificationsService.error('Error', err.message) |
27e1a06c C |
177 | console.error(err) |
178 | } | |
179 | ) | |
180 | ||
181 | } | |
dc8bc31b | 182 | } |