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