]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add.component.ts
Add i18n attributes
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { Router } from '@angular/router'
5 import { UserService } from '@app/shared'
6 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { NotificationsService } from 'angular2-notifications'
9 import { BytesPipe } from 'ngx-pipes'
10 import { Subscription } from 'rxjs'
11 import { VideoPrivacy } from '../../../../../shared/models/videos'
12 import { AuthService, ServerService } from '../../core'
13 import { FormReactive } from '../../shared'
14 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
15 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
16 import { VideoEdit } from '../../shared/video/video-edit.model'
17 import { VideoService } from '../../shared/video/video.service'
18 import { I18n } from '@ngx-translate/i18n-polyfill'
19
20 @Component({
21 selector: 'my-videos-add',
22 templateUrl: './video-add.component.html',
23 styleUrls: [
24 './shared/video-edit.component.scss',
25 './video-add.component.scss'
26 ]
27 })
28 export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
29 @ViewChild('videofileInput') videofileInput
30
31 isUploadingVideo = false
32 isUpdatingVideo = false
33 videoUploaded = false
34 videoUploadObservable: Subscription = null
35 videoUploadPercents = 0
36 videoUploadedIds = {
37 id: 0,
38 uuid: ''
39 }
40 videoFileName: string
41
42 form: FormGroup
43 formErrors: { [ id: string ]: string } = {}
44 validationMessages: ValidatorMessage = {}
45
46 userVideoChannels: { id: number, label: string, support: string }[] = []
47 userVideoQuotaUsed = 0
48 videoPrivacies = []
49 firstStepPrivacyId = 0
50 firstStepChannelId = 0
51
52 constructor (
53 private formBuilder: FormBuilder,
54 private router: Router,
55 private notificationsService: NotificationsService,
56 private authService: AuthService,
57 private userService: UserService,
58 private serverService: ServerService,
59 private videoService: VideoService,
60 private loadingBar: LoadingBarService,
61 private i18n: I18n
62 ) {
63 super()
64 }
65
66 get videoExtensions () {
67 return this.serverService.getConfig().video.file.extensions.join(',')
68 }
69
70 buildForm () {
71 this.form = this.formBuilder.group({})
72 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
73 }
74
75 ngOnInit () {
76 this.buildForm()
77
78 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
79 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
80
81 this.userService.getMyVideoQuotaUsed()
82 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
83
84 this.serverService.videoPrivaciesLoaded
85 .subscribe(
86 () => {
87 this.videoPrivacies = this.serverService.getVideoPrivacies()
88
89 // Public by default
90 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
91 })
92 }
93
94 ngOnDestroy () {
95 if (this.videoUploadObservable) {
96 this.videoUploadObservable.unsubscribe()
97 }
98 }
99
100 canDeactivate () {
101 let text = ''
102
103 if (this.videoUploaded === true) {
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?')
107 } else {
108 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
109 }
110
111 return {
112 canDeactivate: !this.isUploadingVideo,
113 text
114 }
115 }
116
117 fileChange () {
118 this.uploadFirstStep()
119 }
120
121 checkForm () {
122 this.forceCheck()
123
124 return this.form.valid
125 }
126
127 cancelUpload () {
128 if (this.videoUploadObservable !== null) {
129 this.videoUploadObservable.unsubscribe()
130 this.isUploadingVideo = false
131 this.videoUploadPercents = 0
132 this.videoUploadObservable = null
133 this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
134 }
135 }
136
137 uploadFirstStep () {
138 const videofile = this.videofileInput.nativeElement.files[0] as File
139 if (!videofile) return
140
141 // Cannot upload videos > 4GB for now
142 if (videofile.size > 4 * 1024 * 1024 * 1024) {
143 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 4GB'))
144 return
145 }
146
147 const videoQuota = this.authService.getUser().videoQuota
148 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
149 const bytePipes = new BytesPipe()
150
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)
160 return
161 }
162
163 this.videoFileName = videofile.name
164
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
175 const privacy = this.firstStepPrivacyId.toString()
176 const nsfw = false
177 const commentsEnabled = true
178 const channelId = this.firstStepChannelId.toString()
179
180 const formData = new FormData()
181 formData.append('name', name)
182 // Put the video "private" -> we are waiting the user validation of the second step
183 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
184 formData.append('nsfw', '' + nsfw)
185 formData.append('commentsEnabled', '' + commentsEnabled)
186 formData.append('channelId', '' + channelId)
187 formData.append('videofile', videofile)
188
189 this.isUploadingVideo = true
190 this.form.patchValue({
191 name,
192 privacy,
193 nsfw,
194 channelId
195 })
196
197 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
198 event => {
199 if (event.type === HttpEventType.UploadProgress) {
200 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
201 } else if (event instanceof HttpResponse) {
202 this.videoUploaded = true
203
204 this.videoUploadedIds = event.body.video
205
206 this.videoUploadObservable = null
207 }
208 },
209
210 err => {
211 // Reset progress
212 this.isUploadingVideo = false
213 this.videoUploadPercents = 0
214 this.videoUploadObservable = null
215 this.notificationsService.error(this.i18n('Error'), err.message)
216 }
217 )
218 }
219
220 updateSecondStep () {
221 if (this.checkForm() === false) {
222 return
223 }
224
225 const video = new VideoEdit()
226 video.patch(this.form.value)
227 video.id = this.videoUploadedIds.id
228 video.uuid = this.videoUploadedIds.uuid
229
230 this.isUpdatingVideo = true
231 this.loadingBar.start()
232 this.videoService.updateVideo(video)
233 .subscribe(
234 () => {
235 this.isUpdatingVideo = false
236 this.isUploadingVideo = false
237 this.loadingBar.complete()
238
239 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
240 this.router.navigate([ '/videos/watch', video.uuid ])
241 },
242
243 err => {
244 this.isUpdatingVideo = false
245 this.notificationsService.error(this.i18n('Error'), err.message)
246 console.error(err)
247 }
248 )
249
250 }
251 }