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