]>
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' |
db400f44 | 10 | import { Subscription } from 'rxjs' |
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' |
b1d40cff | 18 | import { I18n } from '@ngx-translate/i18n-polyfill' |
1553e15d | 19 | |
dc8bc31b C |
20 | @Component({ |
21 | selector: 'my-videos-add', | |
27e1a06c C |
22 | templateUrl: './video-add.component.html', |
23 | styleUrls: [ | |
24 | './shared/video-edit.component.scss', | |
25 | './video-add.component.scss' | |
26 | ] | |
dc8bc31b | 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 | |
74af5145 | 46 | userVideoChannels: { id: number, label: string, support: string }[] = [] |
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 | 59 | private videoService: VideoService, |
b1d40cff C |
60 | private loadingBar: LoadingBarService, |
61 | private i18n: I18n | |
4b2f33f3 | 62 | ) { |
df98563e | 63 | super() |
4b2f33f3 | 64 | } |
dc8bc31b | 65 | |
108af661 C |
66 | get videoExtensions () { |
67 | return this.serverService.getConfig().video.file.extensions.join(',') | |
68 | } | |
69 | ||
df98563e | 70 | buildForm () { |
27e1a06c | 71 | this.form = this.formBuilder.group({}) |
df98563e | 72 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
73 | } |
74 | ||
df98563e | 75 | ngOnInit () { |
df98563e | 76 | this.buildForm() |
2de96f4d | 77 | |
15a7387d C |
78 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) |
79 | .then(() => this.firstStepChannelId = this.userVideoChannels[0].id) | |
80 | ||
ce5496d6 C |
81 | this.userService.getMyVideoQuotaUsed() |
82 | .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed) | |
83 | ||
c182778e | 84 | this.serverService.videoPrivaciesLoaded |
baeefe22 C |
85 | .subscribe( |
86 | () => { | |
87 | this.videoPrivacies = this.serverService.getVideoPrivacies() | |
cadb46d8 C |
88 | |
89 | // Public by default | |
90 | this.firstStepPrivacyId = VideoPrivacy.PUBLIC | |
baeefe22 | 91 | }) |
e822fdae C |
92 | } |
93 | ||
f6a043df C |
94 | ngOnDestroy () { |
95 | if (this.videoUploadObservable) { | |
96 | this.videoUploadObservable.unsubscribe() | |
97 | } | |
98 | } | |
99 | ||
529479f9 | 100 | canDeactivate () { |
f6a043df C |
101 | let text = '' |
102 | ||
103 | if (this.videoUploaded === true) { | |
b1d40cff C |
104 | // FIXME: cannot concatenate strings inside i18n service :/ |
105 | text = this.i18n('Your video was uploaded in your account and is private.') + | |
106 | this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?') | |
f6a043df | 107 | } else { |
b1d40cff | 108 | text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?') |
f6a043df C |
109 | } |
110 | ||
111 | return { | |
112 | canDeactivate: !this.isUploadingVideo, | |
113 | text | |
114 | } | |
529479f9 DG |
115 | } |
116 | ||
baeefe22 C |
117 | fileChange () { |
118 | this.uploadFirstStep() | |
bf57d5ee C |
119 | } |
120 | ||
bfb3a98f C |
121 | checkForm () { |
122 | this.forceCheck() | |
123 | ||
124 | return this.form.valid | |
e822fdae C |
125 | } |
126 | ||
8c4890cb DG |
127 | cancelUpload () { |
128 | if (this.videoUploadObservable !== null) { | |
129 | this.videoUploadObservable.unsubscribe() | |
130 | this.isUploadingVideo = false | |
131 | this.videoUploadPercents = 0 | |
8c4890cb | 132 | this.videoUploadObservable = null |
b1d40cff | 133 | this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled')) |
8c4890cb DG |
134 | } |
135 | } | |
136 | ||
27e1a06c | 137 | uploadFirstStep () { |
81c263c8 | 138 | const videofile = this.videofileInput.nativeElement.files[0] as File |
a22bfc3e C |
139 | if (!videofile) return |
140 | ||
81c263c8 C |
141 | // Cannot upload videos > 4GB for now |
142 | if (videofile.size > 4 * 1024 * 1024 * 1024) { | |
b1d40cff | 143 | this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 4GB')) |
81c263c8 C |
144 | return |
145 | } | |
146 | ||
ce5496d6 | 147 | const videoQuota = this.authService.getUser().videoQuota |
a22bfc3e | 148 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { |
ce5496d6 C |
149 | const bytePipes = new BytesPipe() |
150 | ||
b1d40cff C |
151 | const msg = this.i18n( |
152 | 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})', | |
153 | { | |
154 | videoSize: bytePipes.transform(videofile.size, 0), | |
155 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
156 | videoQuota: bytePipes.transform(videoQuota, 0) | |
157 | } | |
158 | ) | |
159 | this.notificationsService.error(this.i18n('Error'), msg) | |
ce5496d6 C |
160 | return |
161 | } | |
162 | ||
2f315e2f C |
163 | this.videoFileName = videofile.name |
164 | ||
f2c3f7cd C |
165 | const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '') |
166 | let name: string | |
167 | ||
168 | // If the name of the file is very small, keep the extension | |
169 | if (nameWithoutExtension.length < 3) { | |
170 | name = videofile.name | |
171 | } else { | |
172 | name = nameWithoutExtension | |
173 | } | |
174 | ||
cadb46d8 | 175 | const privacy = this.firstStepPrivacyId.toString() |
baeefe22 | 176 | const nsfw = false |
47564bbe | 177 | const commentsEnabled = true |
cadb46d8 | 178 | const channelId = this.firstStepChannelId.toString() |
bfb3a98f C |
179 | |
180 | const formData = new FormData() | |
181 | formData.append('name', name) | |
fed95155 | 182 | // Put the video "private" -> we are waiting the user validation of the second step |
cadb46d8 | 183 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) |
bfb3a98f | 184 | formData.append('nsfw', '' + nsfw) |
47564bbe | 185 | formData.append('commentsEnabled', '' + commentsEnabled) |
bcd9f81e | 186 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
187 | formData.append('videofile', videofile) |
188 | ||
baeefe22 C |
189 | this.isUploadingVideo = true |
190 | this.form.patchValue({ | |
191 | name, | |
192 | privacy, | |
193 | nsfw, | |
194 | channelId | |
195 | }) | |
e822fdae | 196 | |
8c4890cb | 197 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( |
bfb3a98f C |
198 | event => { |
199 | if (event.type === HttpEventType.UploadProgress) { | |
c182778e | 200 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) |
bfb3a98f | 201 | } else if (event instanceof HttpResponse) { |
baeefe22 | 202 | this.videoUploaded = true |
cadb46d8 | 203 | |
a4b8a4dd | 204 | this.videoUploadedIds = event.body.video |
8c4890cb DG |
205 | |
206 | this.videoUploadObservable = null | |
bfb3a98f C |
207 | } |
208 | }, | |
209 | ||
315cc0cc C |
210 | err => { |
211 | // Reset progress | |
ce5496d6 | 212 | this.isUploadingVideo = false |
c182778e | 213 | this.videoUploadPercents = 0 |
8c4890cb | 214 | this.videoUploadObservable = null |
b1d40cff | 215 | this.notificationsService.error(this.i18n('Error'), err.message) |
315cc0cc | 216 | } |
bfb3a98f | 217 | ) |
dc8bc31b | 218 | } |
27e1a06c C |
219 | |
220 | updateSecondStep () { | |
221 | if (this.checkForm() === false) { | |
222 | return | |
223 | } | |
224 | ||
cadb46d8 C |
225 | const video = new VideoEdit() |
226 | video.patch(this.form.value) | |
a4b8a4dd C |
227 | video.id = this.videoUploadedIds.id |
228 | video.uuid = this.videoUploadedIds.uuid | |
27e1a06c | 229 | |
68e24d72 C |
230 | this.isUpdatingVideo = true |
231 | this.loadingBar.start() | |
27e1a06c C |
232 | this.videoService.updateVideo(video) |
233 | .subscribe( | |
234 | () => { | |
68e24d72 | 235 | this.isUpdatingVideo = false |
66ee325f | 236 | this.isUploadingVideo = false |
68e24d72 C |
237 | this.loadingBar.complete() |
238 | ||
b1d40cff | 239 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.')) |
a4b8a4dd | 240 | this.router.navigate([ '/videos/watch', video.uuid ]) |
27e1a06c C |
241 | }, |
242 | ||
243 | err => { | |
68e24d72 | 244 | this.isUpdatingVideo = false |
b1d40cff | 245 | this.notificationsService.error(this.i18n('Error'), err.message) |
27e1a06c C |
246 | console.error(err) |
247 | } | |
248 | ) | |
249 | ||
250 | } | |
dc8bc31b | 251 | } |