]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add.component.ts
Add ability to update thumbnail and preview on client
[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 { NotificationsService } from 'angular2-notifications'
8 import { BytesPipe } from 'ngx-pipes'
9 import { Subscription } from 'rxjs/Subscription'
10 import { VideoPrivacy } from '../../../../../shared/models/videos'
11 import { AuthService, ServerService } from '../../core'
12 import { FormReactive } from '../../shared'
13 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
14 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
15 import { VideoEdit } from '../../shared/video/video-edit.model'
16 import { VideoService } from '../../shared/video/video.service'
17
18 @Component({
19 selector: 'my-videos-add',
20 templateUrl: './video-add.component.html',
21 styleUrls: [
22 './shared/video-edit.component.scss',
23 './video-add.component.scss'
24 ]
25 })
26
27 export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
28 @ViewChild('videofileInput') videofileInput
29
30 isUploadingVideo = false
31 videoUploaded = false
32 videoUploadObservable: Subscription = null
33 videoUploadPercents = 0
34 videoUploadedIds = {
35 id: 0,
36 uuid: ''
37 }
38
39 form: FormGroup
40 formErrors: { [ id: string ]: string } = {}
41 validationMessages: ValidatorMessage = {}
42
43 userVideoChannels = []
44 userVideoQuotaUsed = 0
45 videoPrivacies = []
46 firstStepPrivacyId = 0
47 firstStepChannelId = 0
48
49 constructor (
50 private formBuilder: FormBuilder,
51 private router: Router,
52 private notificationsService: NotificationsService,
53 private authService: AuthService,
54 private userService: UserService,
55 private serverService: ServerService,
56 private videoService: VideoService
57 ) {
58 super()
59 }
60
61 get videoExtensions () {
62 return this.serverService.getConfig().video.file.extensions.join(',')
63 }
64
65 buildForm () {
66 this.form = this.formBuilder.group({})
67 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
68 }
69
70 ngOnInit () {
71 this.buildForm()
72
73 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
74 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
75
76 this.userService.getMyVideoQuotaUsed()
77 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
78
79 this.serverService.videoPrivaciesLoaded
80 .subscribe(
81 () => {
82 this.videoPrivacies = this.serverService.getVideoPrivacies()
83
84 // Public by default
85 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
86 })
87 }
88
89 ngOnDestroy () {
90 if (this.videoUploadObservable) {
91 this.videoUploadObservable.unsubscribe()
92 }
93 }
94
95 canDeactivate () {
96 let text = ''
97
98 if (this.videoUploaded === true) {
99 text = 'Your video was uploaded in your account and is private.' +
100 ' But associated data (tags, description...) will be lost, are you sure you want to leave this page?'
101 } else {
102 text = 'Your video is not uploaded yet, are you sure you want to leave this page?'
103 }
104
105 return {
106 canDeactivate: !this.isUploadingVideo,
107 text
108 }
109 }
110
111 fileChange () {
112 this.uploadFirstStep()
113 }
114
115 checkForm () {
116 this.forceCheck()
117
118 return this.form.valid
119 }
120
121 cancelUpload () {
122 if (this.videoUploadObservable !== null) {
123 this.videoUploadObservable.unsubscribe()
124 this.isUploadingVideo = false
125 this.videoUploadPercents = 0
126 this.videoUploadObservable = null
127 this.notificationsService.info('Info', 'Upload cancelled')
128 }
129 }
130
131 uploadFirstStep () {
132 const videofile = this.videofileInput.nativeElement.files[0]
133 if (!videofile) return
134
135 const videoQuota = this.authService.getUser().videoQuota
136 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
137 const bytePipes = new BytesPipe()
138
139 const msg = 'Your video quota is exceeded with this video ' +
140 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
141 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
142 `quota: ${bytePipes.transform(videoQuota, 0)})`
143 this.notificationsService.error('Error', msg)
144 return
145 }
146
147 const name = videofile.name.replace(/\.[^/.]+$/, '')
148 const privacy = this.firstStepPrivacyId.toString()
149 const nsfw = false
150 const commentsEnabled = true
151 const channelId = this.firstStepChannelId.toString()
152
153 const formData = new FormData()
154 formData.append('name', name)
155 // Put the video "private" -> we wait he validates the second step
156 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
157 formData.append('nsfw', '' + nsfw)
158 formData.append('commentsEnabled', '' + commentsEnabled)
159 formData.append('channelId', '' + channelId)
160 formData.append('videofile', videofile)
161
162 this.isUploadingVideo = true
163 this.form.patchValue({
164 name,
165 privacy,
166 nsfw,
167 channelId
168 })
169
170 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
171 event => {
172 if (event.type === HttpEventType.UploadProgress) {
173 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
174 } else if (event instanceof HttpResponse) {
175 console.log('Video uploaded.')
176
177 this.videoUploaded = true
178
179 this.videoUploadedIds = event.body.video
180
181 this.videoUploadObservable = null
182 }
183 },
184
185 err => {
186 // Reset progress
187 this.isUploadingVideo = false
188 this.videoUploadPercents = 0
189 this.videoUploadObservable = null
190 this.notificationsService.error('Error', err.message)
191 }
192 )
193 }
194
195 updateSecondStep () {
196 if (this.checkForm() === false) {
197 return
198 }
199
200 const video = new VideoEdit()
201 video.patch(this.form.value)
202 video.channel = this.firstStepChannelId
203 video.id = this.videoUploadedIds.id
204 video.uuid = this.videoUploadedIds.uuid
205
206 this.videoService.updateVideo(video)
207 .subscribe(
208 () => {
209 this.isUploadingVideo = false
210 this.notificationsService.success('Success', 'Video published.')
211 this.router.navigate([ '/videos/watch', video.uuid ])
212 },
213
214 err => {
215 this.notificationsService.error('Error', err.message)
216 console.error(err)
217 }
218 )
219
220 }
221 }