]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add.component.ts
Add ability for uploaders to schedule video update
[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 { Router } from '@angular/router'
4 import { UserService } from '@app/shared'
5 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
6 import { LoadingBarService } from '@ngx-loading-bar/core'
7 import { NotificationsService } from 'angular2-notifications'
8 import { BytesPipe } from 'ngx-pipes'
9 import { Subscription } from 'rxjs'
10 import { VideoPrivacy } from '../../../../../shared/models/videos'
11 import { AuthService, ServerService } from '../../core'
12 import { FormReactive } from '../../shared'
13 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
14 import { VideoEdit } from '../../shared/video/video-edit.model'
15 import { VideoService } from '../../shared/video/video.service'
16 import { I18n } from '@ngx-translate/i18n-polyfill'
17 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.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 export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
28 @ViewChild('videofileInput') videofileInput
29
30 isUploadingVideo = false
31 isUpdatingVideo = false
32 videoUploaded = false
33 videoUploadObservable: Subscription = null
34 videoUploadPercents = 0
35 videoUploadedIds = {
36 id: 0,
37 uuid: ''
38 }
39 videoFileName: string
40
41 userVideoChannels: { id: number, label: string, support: string }[] = []
42 userVideoQuotaUsed = 0
43 videoPrivacies = []
44 firstStepPrivacyId = 0
45 firstStepChannelId = 0
46
47 constructor (
48 protected formValidatorService: FormValidatorService,
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 private loadingBar: LoadingBarService,
56 private i18n: I18n
57 ) {
58 super()
59 }
60
61 get videoExtensions () {
62 return this.serverService.getConfig().video.file.extensions.join(',')
63 }
64
65 ngOnInit () {
66 this.buildForm({})
67
68 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
69 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
70
71 this.userService.getMyVideoQuotaUsed()
72 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
73
74 this.serverService.videoPrivaciesLoaded
75 .subscribe(
76 () => {
77 this.videoPrivacies = this.serverService.getVideoPrivacies()
78
79 // Public by default
80 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
81 })
82 }
83
84 ngOnDestroy () {
85 if (this.videoUploadObservable) {
86 this.videoUploadObservable.unsubscribe()
87 }
88 }
89
90 canDeactivate () {
91 let text = ''
92
93 if (this.videoUploaded === true) {
94 // FIXME: cannot concatenate strings inside i18n service :/
95 text = this.i18n('Your video was uploaded in your account and is private.') +
96 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
97 } else {
98 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
99 }
100
101 return {
102 canDeactivate: !this.isUploadingVideo,
103 text
104 }
105 }
106
107 fileChange () {
108 this.uploadFirstStep()
109 }
110
111 checkForm () {
112 this.forceCheck()
113
114 return this.form.valid
115 }
116
117 cancelUpload () {
118 if (this.videoUploadObservable !== null) {
119 this.videoUploadObservable.unsubscribe()
120 this.isUploadingVideo = false
121 this.videoUploadPercents = 0
122 this.videoUploadObservable = null
123 this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
124 }
125 }
126
127 uploadFirstStep () {
128 const videofile = this.videofileInput.nativeElement.files[0] as File
129 if (!videofile) return
130
131 // Cannot upload videos > 4GB for now
132 if (videofile.size > 4 * 1024 * 1024 * 1024) {
133 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 4GB'))
134 return
135 }
136
137 const videoQuota = this.authService.getUser().videoQuota
138 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
139 const bytePipes = new BytesPipe()
140
141 const msg = this.i18n(
142 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
143 {
144 videoSize: bytePipes.transform(videofile.size, 0),
145 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
146 videoQuota: bytePipes.transform(videoQuota, 0)
147 }
148 )
149 this.notificationsService.error(this.i18n('Error'), msg)
150 return
151 }
152
153 this.videoFileName = videofile.name
154
155 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
156 let name: string
157
158 // If the name of the file is very small, keep the extension
159 if (nameWithoutExtension.length < 3) {
160 name = videofile.name
161 } else {
162 name = nameWithoutExtension
163 }
164
165 const privacy = this.firstStepPrivacyId.toString()
166 const nsfw = false
167 const waitTranscoding = true
168 const commentsEnabled = true
169 const channelId = this.firstStepChannelId.toString()
170
171 const formData = new FormData()
172 formData.append('name', name)
173 // Put the video "private" -> we are waiting the user validation of the second step
174 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
175 formData.append('nsfw', '' + nsfw)
176 formData.append('commentsEnabled', '' + commentsEnabled)
177 formData.append('waitTranscoding', '' + waitTranscoding)
178 formData.append('channelId', '' + channelId)
179 formData.append('videofile', videofile)
180
181 this.isUploadingVideo = true
182 this.form.patchValue({
183 name,
184 privacy,
185 nsfw,
186 channelId
187 })
188
189 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
190 event => {
191 if (event.type === HttpEventType.UploadProgress) {
192 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
193 } else if (event instanceof HttpResponse) {
194 this.videoUploaded = true
195
196 this.videoUploadedIds = event.body.video
197
198 this.videoUploadObservable = null
199 }
200 },
201
202 err => {
203 // Reset progress
204 this.isUploadingVideo = false
205 this.videoUploadPercents = 0
206 this.videoUploadObservable = null
207 this.notificationsService.error(this.i18n('Error'), err.message)
208 }
209 )
210 }
211
212 updateSecondStep () {
213 if (this.checkForm() === false) {
214 return
215 }
216
217 const video = new VideoEdit()
218 video.patch(this.form.value)
219 video.id = this.videoUploadedIds.id
220 video.uuid = this.videoUploadedIds.uuid
221
222 this.isUpdatingVideo = true
223 this.loadingBar.start()
224 this.videoService.updateVideo(video)
225 .subscribe(
226 () => {
227 this.isUpdatingVideo = false
228 this.isUploadingVideo = false
229 this.loadingBar.complete()
230
231 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
232 this.router.navigate([ '/videos/watch', video.uuid ])
233 },
234
235 err => {
236 this.isUpdatingVideo = false
237 this.notificationsService.error(this.i18n('Error'), err.message)
238 console.error(err)
239 }
240 )
241
242 }
243 }