]>
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 |
8c4890cb | 30 | videoUploadObservable = null |
c182778e | 31 | videoUploadPercents = 0 |
a4b8a4dd C |
32 | videoUploadedIds = { |
33 | id: 0, | |
34 | uuid: '' | |
35 | } | |
3758da94 | 36 | |
df98563e | 37 | form: FormGroup |
27e1a06c C |
38 | formErrors: { [ id: string ]: string } = {} |
39 | validationMessages: ValidatorMessage = {} | |
baeefe22 | 40 | |
27e1a06c | 41 | userVideoChannels = [] |
ce5496d6 | 42 | userVideoQuotaUsed = 0 |
27e1a06c | 43 | videoPrivacies = [] |
cadb46d8 C |
44 | firstStepPrivacyId = 0 |
45 | firstStepChannelId = 0 | |
dc8bc31b | 46 | |
df98563e | 47 | constructor ( |
4b2f33f3 | 48 | private formBuilder: FormBuilder, |
7ddd02c9 | 49 | private router: Router, |
6e07c3de | 50 | private notificationsService: NotificationsService, |
bcd9f81e | 51 | private authService: AuthService, |
ce5496d6 | 52 | private userService: UserService, |
db7af09b | 53 | private serverService: ServerService, |
6e07c3de | 54 | private videoService: VideoService |
4b2f33f3 | 55 | ) { |
df98563e | 56 | super() |
4b2f33f3 | 57 | } |
dc8bc31b | 58 | |
108af661 C |
59 | get videoExtensions () { |
60 | return this.serverService.getConfig().video.file.extensions.join(',') | |
61 | } | |
62 | ||
df98563e | 63 | buildForm () { |
27e1a06c | 64 | this.form = this.formBuilder.group({}) |
df98563e | 65 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
66 | } |
67 | ||
df98563e | 68 | ngOnInit () { |
df98563e | 69 | this.buildForm() |
2de96f4d | 70 | |
15a7387d C |
71 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) |
72 | .then(() => this.firstStepChannelId = this.userVideoChannels[0].id) | |
73 | ||
ce5496d6 C |
74 | this.userService.getMyVideoQuotaUsed() |
75 | .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed) | |
76 | ||
c182778e | 77 | this.serverService.videoPrivaciesLoaded |
baeefe22 C |
78 | .subscribe( |
79 | () => { | |
80 | this.videoPrivacies = this.serverService.getVideoPrivacies() | |
cadb46d8 C |
81 | |
82 | // Public by default | |
83 | this.firstStepPrivacyId = VideoPrivacy.PUBLIC | |
baeefe22 | 84 | }) |
e822fdae C |
85 | } |
86 | ||
baeefe22 C |
87 | fileChange () { |
88 | this.uploadFirstStep() | |
bf57d5ee C |
89 | } |
90 | ||
bfb3a98f C |
91 | checkForm () { |
92 | this.forceCheck() | |
93 | ||
94 | return this.form.valid | |
e822fdae C |
95 | } |
96 | ||
8c4890cb DG |
97 | cancelUpload () { |
98 | if (this.videoUploadObservable !== null) { | |
99 | this.videoUploadObservable.unsubscribe() | |
100 | this.isUploadingVideo = false | |
101 | this.videoUploadPercents = 0 | |
8c4890cb | 102 | this.videoUploadObservable = null |
e494f91e | 103 | this.notificationsService.info('Info', 'Upload cancelled') |
8c4890cb DG |
104 | } |
105 | } | |
106 | ||
27e1a06c | 107 | uploadFirstStep () { |
bfb3a98f | 108 | const videofile = this.videofileInput.nativeElement.files[0] |
a22bfc3e C |
109 | if (!videofile) return |
110 | ||
ce5496d6 | 111 | const videoQuota = this.authService.getUser().videoQuota |
a22bfc3e | 112 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { |
ce5496d6 C |
113 | const bytePipes = new BytesPipe() |
114 | ||
115 | const msg = 'Your video quota is exceeded with this video ' + | |
116 | `(video size: ${bytePipes.transform(videofile.size, 0)}, ` + | |
117 | `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` + | |
118 | `quota: ${bytePipes.transform(videoQuota, 0)})` | |
119 | this.notificationsService.error('Error', msg) | |
120 | return | |
121 | } | |
122 | ||
cadb46d8 C |
123 | const name = videofile.name.replace(/\.[^/.]+$/, '') |
124 | const privacy = this.firstStepPrivacyId.toString() | |
baeefe22 | 125 | const nsfw = false |
47564bbe | 126 | const commentsEnabled = true |
cadb46d8 | 127 | const channelId = this.firstStepChannelId.toString() |
bfb3a98f C |
128 | |
129 | const formData = new FormData() | |
130 | formData.append('name', name) | |
cadb46d8 C |
131 | // Put the video "private" -> we wait he validates the second step |
132 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
bfb3a98f | 133 | formData.append('nsfw', '' + nsfw) |
47564bbe | 134 | formData.append('commentsEnabled', '' + commentsEnabled) |
bcd9f81e | 135 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
136 | formData.append('videofile', videofile) |
137 | ||
baeefe22 C |
138 | this.isUploadingVideo = true |
139 | this.form.patchValue({ | |
140 | name, | |
141 | privacy, | |
142 | nsfw, | |
143 | channelId | |
144 | }) | |
e822fdae | 145 | |
8c4890cb | 146 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( |
bfb3a98f C |
147 | event => { |
148 | if (event.type === HttpEventType.UploadProgress) { | |
c182778e | 149 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) |
bfb3a98f C |
150 | } else if (event instanceof HttpResponse) { |
151 | console.log('Video uploaded.') | |
bfb3a98f | 152 | |
baeefe22 | 153 | this.videoUploaded = true |
cadb46d8 | 154 | |
a4b8a4dd | 155 | this.videoUploadedIds = event.body.video |
8c4890cb DG |
156 | |
157 | this.videoUploadObservable = null | |
bfb3a98f C |
158 | } |
159 | }, | |
160 | ||
315cc0cc C |
161 | err => { |
162 | // Reset progress | |
ce5496d6 | 163 | this.isUploadingVideo = false |
c182778e | 164 | this.videoUploadPercents = 0 |
8c4890cb | 165 | this.videoUploadObservable = null |
ce5496d6 | 166 | this.notificationsService.error('Error', err.message) |
315cc0cc | 167 | } |
bfb3a98f | 168 | ) |
dc8bc31b | 169 | } |
27e1a06c C |
170 | |
171 | updateSecondStep () { | |
172 | if (this.checkForm() === false) { | |
173 | return | |
174 | } | |
175 | ||
cadb46d8 C |
176 | const video = new VideoEdit() |
177 | video.patch(this.form.value) | |
178 | video.channel = this.firstStepChannelId | |
a4b8a4dd C |
179 | video.id = this.videoUploadedIds.id |
180 | video.uuid = this.videoUploadedIds.uuid | |
27e1a06c C |
181 | |
182 | this.videoService.updateVideo(video) | |
183 | .subscribe( | |
184 | () => { | |
185 | this.notificationsService.success('Success', 'Video published.') | |
a4b8a4dd | 186 | this.router.navigate([ '/videos/watch', video.uuid ]) |
27e1a06c C |
187 | }, |
188 | ||
189 | err => { | |
ce5496d6 | 190 | this.notificationsService.error('Error', err.message) |
27e1a06c C |
191 | console.error(err) |
192 | } | |
193 | ) | |
194 | ||
195 | } | |
dc8bc31b | 196 | } |