]>
Commit | Line | Data |
---|---|---|
202f6b6c | 1 | import { HttpEventType, HttpResponse } from '@angular/common/http' |
f6a043df | 2 | import { Component, OnDestroy, 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' |
f6a043df | 6 | import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service' |
df98563e | 7 | import { NotificationsService } from 'angular2-notifications' |
ce5496d6 | 8 | import { BytesPipe } from 'ngx-pipes' |
f6a043df | 9 | import { Subscription } from 'rxjs/Subscription' |
cadb46d8 | 10 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
202f6b6c | 11 | import { AuthService, ServerService } from '../../core' |
27e1a06c | 12 | import { FormReactive } from '../../shared' |
63c4db6d | 13 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' |
15a7387d | 14 | import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' |
27e1a06c | 15 | import { VideoEdit } from '../../shared/video/video-edit.model' |
63c4db6d | 16 | import { VideoService } from '../../shared/video/video.service' |
1553e15d | 17 | |
dc8bc31b C |
18 | @Component({ |
19 | selector: 'my-videos-add', | |
27e1a06c C |
20 | templateUrl: './video-add.component.html', |
21 | styleUrls: [ | |
22 | './shared/video-edit.component.scss', | |
23 | './video-add.component.scss' | |
24 | ] | |
dc8bc31b C |
25 | }) |
26 | ||
f6a043df | 27 | export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate { |
bfb3a98f C |
28 | @ViewChild('videofileInput') videofileInput |
29 | ||
27e1a06c | 30 | isUploadingVideo = false |
baeefe22 | 31 | videoUploaded = false |
f6a043df | 32 | videoUploadObservable: Subscription = null |
c182778e | 33 | videoUploadPercents = 0 |
a4b8a4dd C |
34 | videoUploadedIds = { |
35 | id: 0, | |
36 | uuid: '' | |
37 | } | |
3758da94 | 38 | |
df98563e | 39 | form: FormGroup |
27e1a06c C |
40 | formErrors: { [ id: string ]: string } = {} |
41 | validationMessages: ValidatorMessage = {} | |
baeefe22 | 42 | |
27e1a06c | 43 | userVideoChannels = [] |
ce5496d6 | 44 | userVideoQuotaUsed = 0 |
27e1a06c | 45 | videoPrivacies = [] |
cadb46d8 C |
46 | firstStepPrivacyId = 0 |
47 | firstStepChannelId = 0 | |
dc8bc31b | 48 | |
df98563e | 49 | constructor ( |
4b2f33f3 | 50 | private formBuilder: FormBuilder, |
7ddd02c9 | 51 | private router: Router, |
6e07c3de | 52 | private notificationsService: NotificationsService, |
bcd9f81e | 53 | private authService: AuthService, |
ce5496d6 | 54 | private userService: UserService, |
db7af09b | 55 | private serverService: ServerService, |
6e07c3de | 56 | private videoService: VideoService |
4b2f33f3 | 57 | ) { |
df98563e | 58 | super() |
4b2f33f3 | 59 | } |
dc8bc31b | 60 | |
108af661 C |
61 | get videoExtensions () { |
62 | return this.serverService.getConfig().video.file.extensions.join(',') | |
63 | } | |
64 | ||
df98563e | 65 | buildForm () { |
27e1a06c | 66 | this.form = this.formBuilder.group({}) |
df98563e | 67 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
68 | } |
69 | ||
df98563e | 70 | ngOnInit () { |
df98563e | 71 | this.buildForm() |
2de96f4d | 72 | |
15a7387d C |
73 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) |
74 | .then(() => this.firstStepChannelId = this.userVideoChannels[0].id) | |
75 | ||
ce5496d6 C |
76 | this.userService.getMyVideoQuotaUsed() |
77 | .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed) | |
78 | ||
c182778e | 79 | this.serverService.videoPrivaciesLoaded |
baeefe22 C |
80 | .subscribe( |
81 | () => { | |
82 | this.videoPrivacies = this.serverService.getVideoPrivacies() | |
cadb46d8 C |
83 | |
84 | // Public by default | |
85 | this.firstStepPrivacyId = VideoPrivacy.PUBLIC | |
baeefe22 | 86 | }) |
e822fdae C |
87 | } |
88 | ||
f6a043df C |
89 | ngOnDestroy () { |
90 | if (this.videoUploadObservable) { | |
91 | this.videoUploadObservable.unsubscribe() | |
92 | } | |
93 | } | |
94 | ||
529479f9 | 95 | canDeactivate () { |
f6a043df C |
96 | let text = '' |
97 | ||
98 | if (this.videoUploaded === true) { | |
99 | text = 'Your video was uploaded in your account and is private.' + | |
100 | ' But associated data (tags, description...) will be lost, are you sure you want to leave this page?' | |
101 | } else { | |
102 | text = 'Your video is not uploaded yet, are you sure you want to leave this page?' | |
103 | } | |
104 | ||
105 | return { | |
106 | canDeactivate: !this.isUploadingVideo, | |
107 | text | |
108 | } | |
529479f9 DG |
109 | } |
110 | ||
baeefe22 C |
111 | fileChange () { |
112 | this.uploadFirstStep() | |
bf57d5ee C |
113 | } |
114 | ||
bfb3a98f C |
115 | checkForm () { |
116 | this.forceCheck() | |
117 | ||
118 | return this.form.valid | |
e822fdae C |
119 | } |
120 | ||
8c4890cb DG |
121 | cancelUpload () { |
122 | if (this.videoUploadObservable !== null) { | |
123 | this.videoUploadObservable.unsubscribe() | |
124 | this.isUploadingVideo = false | |
125 | this.videoUploadPercents = 0 | |
8c4890cb | 126 | this.videoUploadObservable = null |
e494f91e | 127 | this.notificationsService.info('Info', 'Upload cancelled') |
8c4890cb DG |
128 | } |
129 | } | |
130 | ||
27e1a06c | 131 | uploadFirstStep () { |
bfb3a98f | 132 | const videofile = this.videofileInput.nativeElement.files[0] |
a22bfc3e C |
133 | if (!videofile) return |
134 | ||
ce5496d6 | 135 | const videoQuota = this.authService.getUser().videoQuota |
a22bfc3e | 136 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { |
ce5496d6 C |
137 | const bytePipes = new BytesPipe() |
138 | ||
139 | const msg = 'Your video quota is exceeded with this video ' + | |
140 | `(video size: ${bytePipes.transform(videofile.size, 0)}, ` + | |
141 | `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` + | |
142 | `quota: ${bytePipes.transform(videoQuota, 0)})` | |
143 | this.notificationsService.error('Error', msg) | |
144 | return | |
145 | } | |
146 | ||
cadb46d8 C |
147 | const name = videofile.name.replace(/\.[^/.]+$/, '') |
148 | const privacy = this.firstStepPrivacyId.toString() | |
baeefe22 | 149 | const nsfw = false |
47564bbe | 150 | const commentsEnabled = true |
cadb46d8 | 151 | const channelId = this.firstStepChannelId.toString() |
bfb3a98f C |
152 | |
153 | const formData = new FormData() | |
154 | formData.append('name', name) | |
cadb46d8 C |
155 | // Put the video "private" -> we wait he validates the second step |
156 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) | |
bfb3a98f | 157 | formData.append('nsfw', '' + nsfw) |
47564bbe | 158 | formData.append('commentsEnabled', '' + commentsEnabled) |
bcd9f81e | 159 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
160 | formData.append('videofile', videofile) |
161 | ||
baeefe22 C |
162 | this.isUploadingVideo = true |
163 | this.form.patchValue({ | |
164 | name, | |
165 | privacy, | |
166 | nsfw, | |
167 | channelId | |
168 | }) | |
e822fdae | 169 | |
8c4890cb | 170 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( |
bfb3a98f C |
171 | event => { |
172 | if (event.type === HttpEventType.UploadProgress) { | |
c182778e | 173 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) |
bfb3a98f C |
174 | } else if (event instanceof HttpResponse) { |
175 | console.log('Video uploaded.') | |
bfb3a98f | 176 | |
baeefe22 | 177 | this.videoUploaded = true |
cadb46d8 | 178 | |
a4b8a4dd | 179 | this.videoUploadedIds = event.body.video |
8c4890cb DG |
180 | |
181 | this.videoUploadObservable = null | |
bfb3a98f C |
182 | } |
183 | }, | |
184 | ||
315cc0cc C |
185 | err => { |
186 | // Reset progress | |
ce5496d6 | 187 | this.isUploadingVideo = false |
c182778e | 188 | this.videoUploadPercents = 0 |
8c4890cb | 189 | this.videoUploadObservable = null |
ce5496d6 | 190 | this.notificationsService.error('Error', err.message) |
315cc0cc | 191 | } |
bfb3a98f | 192 | ) |
dc8bc31b | 193 | } |
27e1a06c C |
194 | |
195 | updateSecondStep () { | |
196 | if (this.checkForm() === false) { | |
197 | return | |
198 | } | |
199 | ||
cadb46d8 C |
200 | const video = new VideoEdit() |
201 | video.patch(this.form.value) | |
202 | video.channel = this.firstStepChannelId | |
a4b8a4dd C |
203 | video.id = this.videoUploadedIds.id |
204 | video.uuid = this.videoUploadedIds.uuid | |
27e1a06c C |
205 | |
206 | this.videoService.updateVideo(video) | |
207 | .subscribe( | |
208 | () => { | |
66ee325f | 209 | this.isUploadingVideo = false |
27e1a06c | 210 | this.notificationsService.success('Success', 'Video published.') |
a4b8a4dd | 211 | this.router.navigate([ '/videos/watch', video.uuid ]) |
27e1a06c C |
212 | }, |
213 | ||
214 | err => { | |
ce5496d6 | 215 | this.notificationsService.error('Error', err.message) |
27e1a06c C |
216 | console.error(err) |
217 | } | |
218 | ) | |
219 | ||
220 | } | |
dc8bc31b | 221 | } |