]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add.component.ts
Add filename in upload form title
[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/Subscription'
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
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
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 = []
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 ) {
62 super()
63 }
64
65 get videoExtensions () {
66 return this.serverService.getConfig().video.file.extensions.join(',')
67 }
68
69 buildForm () {
70 this.form = this.formBuilder.group({})
71 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
72 }
73
74 ngOnInit () {
75 this.buildForm()
76
77 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
78 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
79
80 this.userService.getMyVideoQuotaUsed()
81 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
82
83 this.serverService.videoPrivaciesLoaded
84 .subscribe(
85 () => {
86 this.videoPrivacies = this.serverService.getVideoPrivacies()
87
88 // Public by default
89 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
90 })
91 }
92
93 ngOnDestroy () {
94 if (this.videoUploadObservable) {
95 this.videoUploadObservable.unsubscribe()
96 }
97 }
98
99 canDeactivate () {
100 let text = ''
101
102 if (this.videoUploaded === true) {
103 text = 'Your video was uploaded in your account and is private.' +
104 ' But associated data (tags, description...) will be lost, are you sure you want to leave this page?'
105 } else {
106 text = 'Your video is not uploaded yet, are you sure you want to leave this page?'
107 }
108
109 return {
110 canDeactivate: !this.isUploadingVideo,
111 text
112 }
113 }
114
115 fileChange () {
116 this.uploadFirstStep()
117 }
118
119 checkForm () {
120 this.forceCheck()
121
122 return this.form.valid
123 }
124
125 cancelUpload () {
126 if (this.videoUploadObservable !== null) {
127 this.videoUploadObservable.unsubscribe()
128 this.isUploadingVideo = false
129 this.videoUploadPercents = 0
130 this.videoUploadObservable = null
131 this.notificationsService.info('Info', 'Upload cancelled')
132 }
133 }
134
135 uploadFirstStep () {
136 const videofile = this.videofileInput.nativeElement.files[0]
137 if (!videofile) return
138
139 const videoQuota = this.authService.getUser().videoQuota
140 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
141 const bytePipes = new BytesPipe()
142
143 const msg = 'Your video quota is exceeded with this video ' +
144 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
145 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
146 `quota: ${bytePipes.transform(videoQuota, 0)})`
147 this.notificationsService.error('Error', msg)
148 return
149 }
150
151 this.videoFileName = videofile.name
152
153 const name = videofile.name.replace(/\.[^/.]+$/, '')
154 const privacy = this.firstStepPrivacyId.toString()
155 const nsfw = false
156 const commentsEnabled = true
157 const channelId = this.firstStepChannelId.toString()
158
159 const formData = new FormData()
160 formData.append('name', name)
161 // Put the video "private" -> we wait he validates the second step
162 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
163 formData.append('nsfw', '' + nsfw)
164 formData.append('commentsEnabled', '' + commentsEnabled)
165 formData.append('channelId', '' + channelId)
166 formData.append('videofile', videofile)
167
168 this.isUploadingVideo = true
169 this.form.patchValue({
170 name,
171 privacy,
172 nsfw,
173 channelId
174 })
175
176 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
177 event => {
178 if (event.type === HttpEventType.UploadProgress) {
179 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
180 } else if (event instanceof HttpResponse) {
181 console.log('Video uploaded.')
182
183 this.videoUploaded = true
184
185 this.videoUploadedIds = event.body.video
186
187 this.videoUploadObservable = null
188 }
189 },
190
191 err => {
192 // Reset progress
193 this.isUploadingVideo = false
194 this.videoUploadPercents = 0
195 this.videoUploadObservable = null
196 this.notificationsService.error('Error', err.message)
197 }
198 )
199 }
200
201 updateSecondStep () {
202 if (this.checkForm() === false) {
203 return
204 }
205
206 const video = new VideoEdit()
207 video.patch(this.form.value)
208 video.channel = this.firstStepChannelId
209 video.id = this.videoUploadedIds.id
210 video.uuid = this.videoUploadedIds.uuid
211
212 this.isUpdatingVideo = true
213 this.loadingBar.start()
214 this.videoService.updateVideo(video)
215 .subscribe(
216 () => {
217 this.isUpdatingVideo = false
218 this.isUploadingVideo = false
219 this.loadingBar.complete()
220
221 this.notificationsService.success('Success', 'Video published.')
222 this.router.navigate([ '/videos/watch', video.uuid ])
223 },
224
225 err => {
226 this.isUpdatingVideo = false
227 this.notificationsService.error('Error', err.message)
228 console.error(err)
229 }
230 )
231
232 }
233 }