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