]>
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 | ||
bbe0f064 C |
30 | // So that it can be accessed in the template |
31 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
32 | ||
27e1a06c | 33 | isUploadingVideo = false |
68e24d72 | 34 | isUpdatingVideo = false |
baeefe22 | 35 | videoUploaded = false |
f6a043df | 36 | videoUploadObservable: Subscription = null |
c182778e | 37 | videoUploadPercents = 0 |
a4b8a4dd C |
38 | videoUploadedIds = { |
39 | id: 0, | |
40 | uuid: '' | |
41 | } | |
2f315e2f | 42 | videoFileName: string |
3758da94 | 43 | |
74af5145 | 44 | userVideoChannels: { id: number, label: string, support: string }[] = [] |
ce5496d6 | 45 | userVideoQuotaUsed = 0 |
27e1a06c | 46 | videoPrivacies = [] |
cadb46d8 C |
47 | firstStepPrivacyId = 0 |
48 | firstStepChannelId = 0 | |
dc8bc31b | 49 | |
df98563e | 50 | constructor ( |
d18d6478 | 51 | protected formValidatorService: FormValidatorService, |
7ddd02c9 | 52 | private router: Router, |
6e07c3de | 53 | private notificationsService: NotificationsService, |
bcd9f81e | 54 | private authService: AuthService, |
ce5496d6 | 55 | private userService: UserService, |
db7af09b | 56 | private serverService: ServerService, |
68e24d72 | 57 | private videoService: VideoService, |
b1d40cff C |
58 | private loadingBar: LoadingBarService, |
59 | private i18n: I18n | |
4b2f33f3 | 60 | ) { |
df98563e | 61 | super() |
4b2f33f3 | 62 | } |
dc8bc31b | 63 | |
108af661 C |
64 | get videoExtensions () { |
65 | return this.serverService.getConfig().video.file.extensions.join(',') | |
66 | } | |
67 | ||
df98563e | 68 | ngOnInit () { |
d18d6478 | 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 | ||
f6a043df C |
87 | ngOnDestroy () { |
88 | if (this.videoUploadObservable) { | |
89 | this.videoUploadObservable.unsubscribe() | |
90 | } | |
91 | } | |
92 | ||
529479f9 | 93 | canDeactivate () { |
f6a043df C |
94 | let text = '' |
95 | ||
96 | if (this.videoUploaded === true) { | |
b1d40cff C |
97 | // FIXME: cannot concatenate strings inside i18n service :/ |
98 | text = this.i18n('Your video was uploaded in your account and is private.') + | |
99 | this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?') | |
f6a043df | 100 | } else { |
b1d40cff | 101 | text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?') |
f6a043df C |
102 | } |
103 | ||
104 | return { | |
105 | canDeactivate: !this.isUploadingVideo, | |
106 | text | |
107 | } | |
529479f9 DG |
108 | } |
109 | ||
baeefe22 C |
110 | fileChange () { |
111 | this.uploadFirstStep() | |
bf57d5ee C |
112 | } |
113 | ||
bfb3a98f C |
114 | checkForm () { |
115 | this.forceCheck() | |
116 | ||
117 | return this.form.valid | |
e822fdae C |
118 | } |
119 | ||
8c4890cb DG |
120 | cancelUpload () { |
121 | if (this.videoUploadObservable !== null) { | |
122 | this.videoUploadObservable.unsubscribe() | |
123 | this.isUploadingVideo = false | |
124 | this.videoUploadPercents = 0 | |
8c4890cb | 125 | this.videoUploadObservable = null |
b1d40cff | 126 | this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled')) |
8c4890cb DG |
127 | } |
128 | } | |
129 | ||
27e1a06c | 130 | uploadFirstStep () { |
81c263c8 | 131 | const videofile = this.videofileInput.nativeElement.files[0] as File |
a22bfc3e C |
132 | if (!videofile) return |
133 | ||
34b19192 C |
134 | // Cannot upload videos > 8GB for now |
135 | if (videofile.size > 8 * 1024 * 1024 * 1024) { | |
136 | this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 8GB')) | |
137 | return | |
138 | } | |
81c263c8 | 139 | |
ce5496d6 | 140 | const videoQuota = this.authService.getUser().videoQuota |
a22bfc3e | 141 | if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) { |
ce5496d6 C |
142 | const bytePipes = new BytesPipe() |
143 | ||
b1d40cff C |
144 | const msg = this.i18n( |
145 | 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})', | |
146 | { | |
147 | videoSize: bytePipes.transform(videofile.size, 0), | |
148 | videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0), | |
149 | videoQuota: bytePipes.transform(videoQuota, 0) | |
150 | } | |
151 | ) | |
152 | this.notificationsService.error(this.i18n('Error'), msg) | |
ce5496d6 C |
153 | return |
154 | } | |
155 | ||
2f315e2f C |
156 | this.videoFileName = videofile.name |
157 | ||
f2c3f7cd C |
158 | const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '') |
159 | let name: string | |
160 | ||
161 | // If the name of the file is very small, keep the extension | |
162 | if (nameWithoutExtension.length < 3) { | |
163 | name = videofile.name | |
164 | } else { | |
165 | name = nameWithoutExtension | |
166 | } | |
167 | ||
cadb46d8 | 168 | const privacy = this.firstStepPrivacyId.toString() |
baeefe22 | 169 | const nsfw = false |
2186386c | 170 | const waitTranscoding = true |
47564bbe | 171 | const commentsEnabled = true |
cadb46d8 | 172 | const channelId = this.firstStepChannelId.toString() |
bfb3a98f C |
173 | |
174 | const formData = new FormData() | |
175 | formData.append('name', name) | |
fed95155 | 176 | // Put the video "private" -> we are waiting the user validation of the second step |
cadb46d8 | 177 | formData.append('privacy', VideoPrivacy.PRIVATE.toString()) |
bfb3a98f | 178 | formData.append('nsfw', '' + nsfw) |
47564bbe | 179 | formData.append('commentsEnabled', '' + commentsEnabled) |
2186386c | 180 | formData.append('waitTranscoding', '' + waitTranscoding) |
bcd9f81e | 181 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
182 | formData.append('videofile', videofile) |
183 | ||
baeefe22 C |
184 | this.isUploadingVideo = true |
185 | this.form.patchValue({ | |
186 | name, | |
187 | privacy, | |
188 | nsfw, | |
189 | channelId | |
190 | }) | |
e822fdae | 191 | |
8c4890cb | 192 | this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe( |
bfb3a98f C |
193 | event => { |
194 | if (event.type === HttpEventType.UploadProgress) { | |
c182778e | 195 | this.videoUploadPercents = Math.round(100 * event.loaded / event.total) |
bfb3a98f | 196 | } else if (event instanceof HttpResponse) { |
baeefe22 | 197 | this.videoUploaded = true |
cadb46d8 | 198 | |
a4b8a4dd | 199 | this.videoUploadedIds = event.body.video |
8c4890cb DG |
200 | |
201 | this.videoUploadObservable = null | |
bfb3a98f C |
202 | } |
203 | }, | |
204 | ||
315cc0cc C |
205 | err => { |
206 | // Reset progress | |
ce5496d6 | 207 | this.isUploadingVideo = false |
c182778e | 208 | this.videoUploadPercents = 0 |
8c4890cb | 209 | this.videoUploadObservable = null |
b1d40cff | 210 | this.notificationsService.error(this.i18n('Error'), err.message) |
315cc0cc | 211 | } |
bfb3a98f | 212 | ) |
dc8bc31b | 213 | } |
27e1a06c C |
214 | |
215 | updateSecondStep () { | |
216 | if (this.checkForm() === false) { | |
217 | return | |
218 | } | |
219 | ||
cadb46d8 C |
220 | const video = new VideoEdit() |
221 | video.patch(this.form.value) | |
a4b8a4dd C |
222 | video.id = this.videoUploadedIds.id |
223 | video.uuid = this.videoUploadedIds.uuid | |
27e1a06c | 224 | |
68e24d72 C |
225 | this.isUpdatingVideo = true |
226 | this.loadingBar.start() | |
27e1a06c C |
227 | this.videoService.updateVideo(video) |
228 | .subscribe( | |
229 | () => { | |
68e24d72 | 230 | this.isUpdatingVideo = false |
66ee325f | 231 | this.isUploadingVideo = false |
68e24d72 C |
232 | this.loadingBar.complete() |
233 | ||
b1d40cff | 234 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.')) |
a4b8a4dd | 235 | this.router.navigate([ '/videos/watch', video.uuid ]) |
27e1a06c C |
236 | }, |
237 | ||
238 | err => { | |
68e24d72 | 239 | this.isUpdatingVideo = false |
b1d40cff | 240 | this.notificationsService.error(this.i18n('Error'), err.message) |
27e1a06c C |
241 | console.error(err) |
242 | } | |
243 | ) | |
244 | ||
245 | } | |
dc8bc31b | 246 | } |