]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Upgrade server dependencies
[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'
f6a043df 10import { Subscription } from 'rxjs/Subscription'
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
C
26})
27
f6a043df 28export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
bfb3a98f
C
29 @ViewChild('videofileInput') videofileInput
30
27e1a06c 31 isUploadingVideo = false
68e24d72 32 isUpdatingVideo = false
baeefe22 33 videoUploaded = false
f6a043df 34 videoUploadObservable: Subscription = null
c182778e 35 videoUploadPercents = 0
a4b8a4dd
C
36 videoUploadedIds = {
37 id: 0,
38 uuid: ''
39 }
3758da94 40
df98563e 41 form: FormGroup
27e1a06c
C
42 formErrors: { [ id: string ]: string } = {}
43 validationMessages: ValidatorMessage = {}
baeefe22 44
27e1a06c 45 userVideoChannels = []
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 () {
bfb3a98f 135 const videofile = this.videofileInput.nativeElement.files[0]
a22bfc3e
C
136 if (!videofile) return
137
ce5496d6 138 const videoQuota = this.authService.getUser().videoQuota
a22bfc3e 139 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
ce5496d6
C
140 const bytePipes = new BytesPipe()
141
142 const msg = 'Your video quota is exceeded with this video ' +
143 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
144 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
145 `quota: ${bytePipes.transform(videoQuota, 0)})`
146 this.notificationsService.error('Error', msg)
147 return
148 }
149
cadb46d8
C
150 const name = videofile.name.replace(/\.[^/.]+$/, '')
151 const privacy = this.firstStepPrivacyId.toString()
baeefe22 152 const nsfw = false
47564bbe 153 const commentsEnabled = true
cadb46d8 154 const channelId = this.firstStepChannelId.toString()
bfb3a98f
C
155
156 const formData = new FormData()
157 formData.append('name', name)
cadb46d8
C
158 // Put the video "private" -> we wait he validates the second step
159 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
bfb3a98f 160 formData.append('nsfw', '' + nsfw)
47564bbe 161 formData.append('commentsEnabled', '' + commentsEnabled)
bcd9f81e 162 formData.append('channelId', '' + channelId)
bfb3a98f
C
163 formData.append('videofile', videofile)
164
baeefe22
C
165 this.isUploadingVideo = true
166 this.form.patchValue({
167 name,
168 privacy,
169 nsfw,
170 channelId
171 })
e822fdae 172
8c4890cb 173 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
bfb3a98f
C
174 event => {
175 if (event.type === HttpEventType.UploadProgress) {
c182778e 176 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
bfb3a98f
C
177 } else if (event instanceof HttpResponse) {
178 console.log('Video uploaded.')
bfb3a98f 179
baeefe22 180 this.videoUploaded = true
cadb46d8 181
a4b8a4dd 182 this.videoUploadedIds = event.body.video
8c4890cb
DG
183
184 this.videoUploadObservable = null
bfb3a98f
C
185 }
186 },
187
315cc0cc
C
188 err => {
189 // Reset progress
ce5496d6 190 this.isUploadingVideo = false
c182778e 191 this.videoUploadPercents = 0
8c4890cb 192 this.videoUploadObservable = null
ce5496d6 193 this.notificationsService.error('Error', err.message)
315cc0cc 194 }
bfb3a98f 195 )
dc8bc31b 196 }
27e1a06c
C
197
198 updateSecondStep () {
199 if (this.checkForm() === false) {
200 return
201 }
202
cadb46d8
C
203 const video = new VideoEdit()
204 video.patch(this.form.value)
205 video.channel = this.firstStepChannelId
a4b8a4dd
C
206 video.id = this.videoUploadedIds.id
207 video.uuid = this.videoUploadedIds.uuid
27e1a06c 208
68e24d72
C
209 this.isUpdatingVideo = true
210 this.loadingBar.start()
27e1a06c
C
211 this.videoService.updateVideo(video)
212 .subscribe(
213 () => {
68e24d72 214 this.isUpdatingVideo = false
66ee325f 215 this.isUploadingVideo = false
68e24d72
C
216 this.loadingBar.complete()
217
27e1a06c 218 this.notificationsService.success('Success', 'Video published.')
a4b8a4dd 219 this.router.navigate([ '/videos/watch', video.uuid ])
27e1a06c
C
220 },
221
222 err => {
68e24d72 223 this.isUpdatingVideo = false
ce5496d6 224 this.notificationsService.error('Error', err.message)
27e1a06c
C
225 console.error(err)
226 }
227 )
228
229 }
dc8bc31b 230}