]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Video support field inherits channel support field
[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
C
3import { FormBuilder, FormGroup } from '@angular/forms'
4import { Router } from '@angular/router'
ce5496d6 5import { UserService } from '@app/shared'
f6a043df 6import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
68e24d72 7import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 8import { NotificationsService } from 'angular2-notifications'
ce5496d6 9import { BytesPipe } from 'ngx-pipes'
db400f44 10import { Subscription } from 'rxjs'
cadb46d8 11import { VideoPrivacy } from '../../../../../shared/models/videos'
202f6b6c 12import { AuthService, ServerService } from '../../core'
27e1a06c 13import { FormReactive } from '../../shared'
63c4db6d 14import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
15a7387d 15import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
27e1a06c 16import { VideoEdit } from '../../shared/video/video-edit.model'
63c4db6d 17import { VideoService } from '../../shared/video/video.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 27export 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
df98563e 41 form: FormGroup
27e1a06c
C
42 formErrors: { [ id: string ]: string } = {}
43 validationMessages: ValidatorMessage = {}
baeefe22 44
74af5145 45 userVideoChannels: { id: number, label: string, support: string }[] = []
ce5496d6 46 userVideoQuotaUsed = 0
27e1a06c 47 videoPrivacies = []
cadb46d8
C
48 firstStepPrivacyId = 0
49 firstStepChannelId = 0
dc8bc31b 50
df98563e 51 constructor (
4b2f33f3 52 private formBuilder: FormBuilder,
7ddd02c9 53 private router: Router,
6e07c3de 54 private notificationsService: NotificationsService,
bcd9f81e 55 private authService: AuthService,
ce5496d6 56 private userService: UserService,
db7af09b 57 private serverService: ServerService,
68e24d72
C
58 private videoService: VideoService,
59 private loadingBar: LoadingBarService
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 buildForm () {
27e1a06c 69 this.form = this.formBuilder.group({})
df98563e 70 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
71 }
72
df98563e 73 ngOnInit () {
df98563e 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) {
102 text = 'Your video was uploaded in your account and is private.' +
103 ' But associated data (tags, description...) will be lost, are you sure you want to leave this page?'
104 } else {
105 text = 'Your video is not uploaded yet, are you sure you want to leave this page?'
106 }
107
108 return {
109 canDeactivate: !this.isUploadingVideo,
110 text
111 }
529479f9
DG
112 }
113
baeefe22
C
114 fileChange () {
115 this.uploadFirstStep()
bf57d5ee
C
116 }
117
bfb3a98f
C
118 checkForm () {
119 this.forceCheck()
120
121 return this.form.valid
e822fdae
C
122 }
123
8c4890cb
DG
124 cancelUpload () {
125 if (this.videoUploadObservable !== null) {
126 this.videoUploadObservable.unsubscribe()
127 this.isUploadingVideo = false
128 this.videoUploadPercents = 0
8c4890cb 129 this.videoUploadObservable = null
e494f91e 130 this.notificationsService.info('Info', 'Upload cancelled')
8c4890cb
DG
131 }
132 }
133
27e1a06c 134 uploadFirstStep () {
81c263c8 135 const videofile = this.videofileInput.nativeElement.files[0] as File
a22bfc3e
C
136 if (!videofile) return
137
81c263c8
C
138 // Cannot upload videos > 4GB for now
139 if (videofile.size > 4 * 1024 * 1024 * 1024) {
140 this.notificationsService.error('Error', 'We are sorry but PeerTube cannot handle videos > 4GB')
141 return
142 }
143
ce5496d6 144 const videoQuota = this.authService.getUser().videoQuota
a22bfc3e 145 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
ce5496d6
C
146 const bytePipes = new BytesPipe()
147
148 const msg = 'Your video quota is exceeded with this video ' +
149 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
150 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
151 `quota: ${bytePipes.transform(videoQuota, 0)})`
152 this.notificationsService.error('Error', msg)
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
47564bbe 170 const commentsEnabled = true
cadb46d8 171 const channelId = this.firstStepChannelId.toString()
bfb3a98f
C
172
173 const formData = new FormData()
174 formData.append('name', name)
fed95155 175 // Put the video "private" -> we are waiting the user validation of the second step
cadb46d8 176 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
bfb3a98f 177 formData.append('nsfw', '' + nsfw)
47564bbe 178 formData.append('commentsEnabled', '' + commentsEnabled)
bcd9f81e 179 formData.append('channelId', '' + channelId)
bfb3a98f
C
180 formData.append('videofile', videofile)
181
baeefe22
C
182 this.isUploadingVideo = true
183 this.form.patchValue({
184 name,
185 privacy,
186 nsfw,
187 channelId
188 })
e822fdae 189
8c4890cb 190 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
bfb3a98f
C
191 event => {
192 if (event.type === HttpEventType.UploadProgress) {
c182778e 193 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
bfb3a98f
C
194 } else if (event instanceof HttpResponse) {
195 console.log('Video uploaded.')
bfb3a98f 196
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
ce5496d6 210 this.notificationsService.error('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
27e1a06c 234 this.notificationsService.success('Success', 'Video published.')
a4b8a4dd 235 this.router.navigate([ '/videos/watch', video.uuid ])
27e1a06c
C
236 },
237
238 err => {
68e24d72 239 this.isUpdatingVideo = false
ce5496d6 240 this.notificationsService.error('Error', err.message)
27e1a06c
C
241 console.error(err)
242 }
243 )
244
245 }
dc8bc31b 246}