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