]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Hack 459 regarding Angular & i18n
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
202f6b6c 1import { HttpEventType, HttpResponse } from '@angular/common/http'
f6a043df 2import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e 3import { Router } from '@angular/router'
ce5496d6 4import { UserService } from '@app/shared'
f6a043df 5import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
68e24d72 6import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 7import { NotificationsService } from 'angular2-notifications'
ce5496d6 8import { BytesPipe } from 'ngx-pipes'
db400f44 9import { Subscription } from 'rxjs'
ef4c78da 10import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
202f6b6c 11import { AuthService, ServerService } from '../../core'
27e1a06c 12import { FormReactive } from '../../shared'
15a7387d 13import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
27e1a06c 14import { VideoEdit } from '../../shared/video/video-edit.model'
63c4db6d 15import { VideoService } from '../../shared/video/video.service'
b1d40cff 16import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 17import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
40e87e9e
C
18import { switchMap } from 'rxjs/operators'
19import { VideoCaptionService } from '@app/shared/video-caption'
ef4c78da 20import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
1553e15d 21
dc8bc31b
C
22@Component({
23 selector: 'my-videos-add',
27e1a06c
C
24 templateUrl: './video-add.component.html',
25 styleUrls: [
26 './shared/video-edit.component.scss',
27 './video-add.component.scss'
28 ]
dc8bc31b 29})
f6a043df 30export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
bfb3a98f
C
31 @ViewChild('videofileInput') videofileInput
32
bbe0f064
C
33 // So that it can be accessed in the template
34 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
35
27e1a06c 36 isUploadingVideo = false
68e24d72 37 isUpdatingVideo = false
baeefe22 38 videoUploaded = false
f6a043df 39 videoUploadObservable: Subscription = null
c182778e 40 videoUploadPercents = 0
a4b8a4dd
C
41 videoUploadedIds = {
42 id: 0,
43 uuid: ''
44 }
2f315e2f 45 videoFileName: string
3758da94 46
74af5145 47 userVideoChannels: { id: number, label: string, support: string }[] = []
ce5496d6 48 userVideoQuotaUsed = 0
ef4c78da 49 videoPrivacies: VideoConstant<string>[] = []
cadb46d8
C
50 firstStepPrivacyId = 0
51 firstStepChannelId = 0
ef4c78da 52 videoCaptions: VideoCaptionEdit[] = []
dc8bc31b 53
df98563e 54 constructor (
d18d6478 55 protected formValidatorService: FormValidatorService,
7ddd02c9 56 private router: Router,
6e07c3de 57 private notificationsService: NotificationsService,
bcd9f81e 58 private authService: AuthService,
ce5496d6 59 private userService: UserService,
db7af09b 60 private serverService: ServerService,
68e24d72 61 private videoService: VideoService,
b1d40cff 62 private loadingBar: LoadingBarService,
40e87e9e
C
63 private i18n: I18n,
64 private videoCaptionService: VideoCaptionService
4b2f33f3 65 ) {
df98563e 66 super()
4b2f33f3 67 }
dc8bc31b 68
108af661
C
69 get videoExtensions () {
70 return this.serverService.getConfig().video.file.extensions.join(',')
71 }
72
df98563e 73 ngOnInit () {
d18d6478 74 this.buildForm({})
2de96f4d 75
15a7387d
C
76 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
77 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
78
ce5496d6
C
79 this.userService.getMyVideoQuotaUsed()
80 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
81
c182778e 82 this.serverService.videoPrivaciesLoaded
baeefe22
C
83 .subscribe(
84 () => {
85 this.videoPrivacies = this.serverService.getVideoPrivacies()
cadb46d8
C
86
87 // Public by default
88 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
baeefe22 89 })
e822fdae
C
90 }
91
f6a043df
C
92 ngOnDestroy () {
93 if (this.videoUploadObservable) {
94 this.videoUploadObservable.unsubscribe()
95 }
96 }
97
529479f9 98 canDeactivate () {
f6a043df
C
99 let text = ''
100
101 if (this.videoUploaded === true) {
b1d40cff
C
102 // FIXME: cannot concatenate strings inside i18n service :/
103 text = this.i18n('Your video was uploaded in your account and is private.') +
104 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
f6a043df 105 } else {
b1d40cff 106 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
f6a043df
C
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
b1d40cff 131 this.notificationsService.info(this.i18n('Info'), this.i18n('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
34b19192
C
139 // Cannot upload videos > 8GB for now
140 if (videofile.size > 8 * 1024 * 1024 * 1024) {
141 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 8GB'))
142 return
143 }
81c263c8 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
b1d40cff
C
149 const msg = this.i18n(
150 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
151 {
152 videoSize: bytePipes.transform(videofile.size, 0),
153 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
154 videoQuota: bytePipes.transform(videoQuota, 0)
155 }
156 )
157 this.notificationsService.error(this.i18n('Error'), msg)
ce5496d6
C
158 return
159 }
160
2f315e2f
C
161 this.videoFileName = videofile.name
162
f2c3f7cd
C
163 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
164 let name: string
165
166 // If the name of the file is very small, keep the extension
40e87e9e
C
167 if (nameWithoutExtension.length < 3) name = videofile.name
168 else name = nameWithoutExtension
f2c3f7cd 169
cadb46d8 170 const privacy = this.firstStepPrivacyId.toString()
baeefe22 171 const nsfw = false
2186386c 172 const waitTranscoding = true
47564bbe 173 const commentsEnabled = true
cadb46d8 174 const channelId = this.firstStepChannelId.toString()
bfb3a98f
C
175
176 const formData = new FormData()
177 formData.append('name', name)
fed95155 178 // Put the video "private" -> we are waiting the user validation of the second step
cadb46d8 179 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
bfb3a98f 180 formData.append('nsfw', '' + nsfw)
47564bbe 181 formData.append('commentsEnabled', '' + commentsEnabled)
2186386c 182 formData.append('waitTranscoding', '' + waitTranscoding)
bcd9f81e 183 formData.append('channelId', '' + channelId)
bfb3a98f
C
184 formData.append('videofile', videofile)
185
baeefe22
C
186 this.isUploadingVideo = true
187 this.form.patchValue({
188 name,
189 privacy,
190 nsfw,
191 channelId
192 })
e822fdae 193
8c4890cb 194 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
bfb3a98f
C
195 event => {
196 if (event.type === HttpEventType.UploadProgress) {
c182778e 197 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
bfb3a98f 198 } else if (event instanceof HttpResponse) {
baeefe22 199 this.videoUploaded = true
cadb46d8 200
a4b8a4dd 201 this.videoUploadedIds = event.body.video
8c4890cb
DG
202
203 this.videoUploadObservable = null
bfb3a98f
C
204 }
205 },
206
315cc0cc
C
207 err => {
208 // Reset progress
ce5496d6 209 this.isUploadingVideo = false
c182778e 210 this.videoUploadPercents = 0
8c4890cb 211 this.videoUploadObservable = null
b1d40cff 212 this.notificationsService.error(this.i18n('Error'), err.message)
315cc0cc 213 }
bfb3a98f 214 )
dc8bc31b 215 }
27e1a06c
C
216
217 updateSecondStep () {
218 if (this.checkForm() === false) {
219 return
220 }
221
cadb46d8
C
222 const video = new VideoEdit()
223 video.patch(this.form.value)
a4b8a4dd
C
224 video.id = this.videoUploadedIds.id
225 video.uuid = this.videoUploadedIds.uuid
27e1a06c 226
68e24d72
C
227 this.isUpdatingVideo = true
228 this.loadingBar.start()
27e1a06c 229 this.videoService.updateVideo(video)
40e87e9e
C
230 .pipe(
231 // Then update captions
232 switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions))
233 )
234 .subscribe(
235 () => {
236 this.isUpdatingVideo = false
237 this.isUploadingVideo = false
238 this.loadingBar.complete()
239
240 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
241 this.router.navigate([ '/videos/watch', video.uuid ])
242 },
243
244 err => {
245 this.isUpdatingVideo = false
246 this.notificationsService.error(this.i18n('Error'), err.message)
247 console.error(err)
248 }
249 )
27e1a06c 250 }
dc8bc31b 251}