1
0
Fork 0
peertube/client/src/app/videos/+video-edit/video-add.component.ts

202 lines
5.8 KiB
TypeScript
Raw Normal View History

2017-12-01 17:56:26 +00:00
import { HttpEventType, HttpResponse } from '@angular/common/http'
import { Component, OnInit, ViewChild } from '@angular/core'
import { FormBuilder, FormGroup } from '@angular/forms'
import { Router } from '@angular/router'
2018-01-08 11:53:09 +00:00
import { UserService } from '@app/shared'
import { NotificationsService } from 'angular2-notifications'
2018-01-08 11:53:09 +00:00
import { BytesPipe } from 'ngx-pipes'
2017-12-08 07:39:15 +00:00
import { VideoPrivacy } from '../../../../../shared/models/videos'
2017-12-01 17:56:26 +00:00
import { AuthService, ServerService } from '../../core'
2017-12-07 15:32:06 +00:00
import { FormReactive } from '../../shared'
2017-12-11 16:36:46 +00:00
import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
2017-12-20 13:29:55 +00:00
import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
2017-12-07 15:32:06 +00:00
import { VideoEdit } from '../../shared/video/video-edit.model'
2017-12-11 16:36:46 +00:00
import { VideoService } from '../../shared/video/video.service'
import { CanComponentDeactivate } from '@app/shared/can-deactivate-guard.service'
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-videos-add',
2017-12-07 15:32:06 +00:00
templateUrl: './video-add.component.html',
styleUrls: [
'./shared/video-edit.component.scss',
'./video-add.component.scss'
]
2016-03-14 12:50:19 +00:00
})
export class VideoAddComponent extends FormReactive implements OnInit, CanComponentDeactivate {
@ViewChild('videofileInput') videofileInput
2017-12-07 15:32:06 +00:00
isUploadingVideo = false
2017-12-07 16:22:44 +00:00
videoUploaded = false
videoUploadObservable = null
2017-12-07 16:56:59 +00:00
videoUploadPercents = 0
videoUploadedIds = {
id: 0,
uuid: ''
}
form: FormGroup
2017-12-07 15:32:06 +00:00
formErrors: { [ id: string ]: string } = {}
validationMessages: ValidatorMessage = {}
2017-12-07 16:22:44 +00:00
2017-12-07 15:32:06 +00:00
userVideoChannels = []
2018-01-08 11:53:09 +00:00
userVideoQuotaUsed = 0
2017-12-07 15:32:06 +00:00
videoPrivacies = []
2017-12-08 07:39:15 +00:00
firstStepPrivacyId = 0
firstStepChannelId = 0
2016-03-14 12:50:19 +00:00
constructor (
2016-09-09 20:16:51 +00:00
private formBuilder: FormBuilder,
private router: Router,
2017-03-22 20:15:55 +00:00
private notificationsService: NotificationsService,
2017-10-25 15:31:11 +00:00
private authService: AuthService,
2018-01-08 11:53:09 +00:00
private userService: UserService,
private serverService: ServerService,
2017-03-22 20:15:55 +00:00
private videoService: VideoService
2016-09-09 20:16:51 +00:00
) {
super()
2016-09-09 20:16:51 +00:00
}
2016-03-14 12:50:19 +00:00
2018-01-08 10:30:48 +00:00
get videoExtensions () {
return this.serverService.getConfig().video.file.extensions.join(',')
}
buildForm () {
2017-12-07 15:32:06 +00:00
this.form = this.formBuilder.group({})
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
}
ngOnInit () {
this.buildForm()
2017-12-20 13:29:55 +00:00
populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
.then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
2018-01-08 11:53:09 +00:00
this.userService.getMyVideoQuotaUsed()
.subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
2017-12-07 16:56:59 +00:00
this.serverService.videoPrivaciesLoaded
2017-12-07 16:22:44 +00:00
.subscribe(
() => {
this.videoPrivacies = this.serverService.getVideoPrivacies()
2017-12-08 07:39:15 +00:00
// Public by default
this.firstStepPrivacyId = VideoPrivacy.PUBLIC
2017-12-07 16:22:44 +00:00
})
}
canDeactivate () {
return !this.isUploadingVideo
}
2017-12-07 16:22:44 +00:00
fileChange () {
this.uploadFirstStep()
}
checkForm () {
this.forceCheck()
return this.form.valid
}
cancelUpload () {
if (this.videoUploadObservable !== null) {
this.videoUploadObservable.unsubscribe()
this.isUploadingVideo = false
this.videoUploadPercents = 0
this.videoUploadObservable = null
2018-01-23 14:15:03 +00:00
this.notificationsService.info('Info', 'Upload cancelled')
}
}
2017-12-07 15:32:06 +00:00
uploadFirstStep () {
const videofile = this.videofileInput.nativeElement.files[0]
2018-01-09 14:29:05 +00:00
if (!videofile) return
2018-01-08 11:53:09 +00:00
const videoQuota = this.authService.getUser().videoQuota
2018-01-09 14:29:05 +00:00
if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
2018-01-08 11:53:09 +00:00
const bytePipes = new BytesPipe()
const msg = 'Your video quota is exceeded with this video ' +
`(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
`used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
`quota: ${bytePipes.transform(videoQuota, 0)})`
this.notificationsService.error('Error', msg)
return
}
2017-12-08 07:39:15 +00:00
const name = videofile.name.replace(/\.[^/.]+$/, '')
const privacy = this.firstStepPrivacyId.toString()
2017-12-07 16:22:44 +00:00
const nsfw = false
2018-01-03 09:12:36 +00:00
const commentsEnabled = true
2017-12-08 07:39:15 +00:00
const channelId = this.firstStepChannelId.toString()
const formData = new FormData()
formData.append('name', name)
2017-12-08 07:39:15 +00:00
// Put the video "private" -> we wait he validates the second step
formData.append('privacy', VideoPrivacy.PRIVATE.toString())
formData.append('nsfw', '' + nsfw)
2018-01-03 09:12:36 +00:00
formData.append('commentsEnabled', '' + commentsEnabled)
2017-10-25 15:31:11 +00:00
formData.append('channelId', '' + channelId)
formData.append('videofile', videofile)
2017-12-07 16:22:44 +00:00
this.isUploadingVideo = true
this.form.patchValue({
name,
privacy,
nsfw,
channelId
})
this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
event => {
if (event.type === HttpEventType.UploadProgress) {
2017-12-07 16:56:59 +00:00
this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
} else if (event instanceof HttpResponse) {
console.log('Video uploaded.')
2017-12-07 16:22:44 +00:00
this.videoUploaded = true
2017-12-08 07:39:15 +00:00
this.videoUploadedIds = event.body.video
this.videoUploadObservable = null
}
},
err => {
// Reset progress
2018-01-08 11:53:09 +00:00
this.isUploadingVideo = false
2017-12-07 16:56:59 +00:00
this.videoUploadPercents = 0
this.videoUploadObservable = null
2018-01-08 11:53:09 +00:00
this.notificationsService.error('Error', err.message)
}
)
2016-03-14 12:50:19 +00:00
}
2017-12-07 15:32:06 +00:00
updateSecondStep () {
if (this.checkForm() === false) {
return
}
2017-12-08 07:39:15 +00:00
const video = new VideoEdit()
video.patch(this.form.value)
video.channel = this.firstStepChannelId
video.id = this.videoUploadedIds.id
video.uuid = this.videoUploadedIds.uuid
2017-12-07 15:32:06 +00:00
this.videoService.updateVideo(video)
.subscribe(
() => {
this.notificationsService.success('Success', 'Video published.')
this.router.navigate([ '/videos/watch', video.uuid ])
2017-12-07 15:32:06 +00:00
},
err => {
2018-01-08 11:53:09 +00:00
this.notificationsService.error('Error', err.message)
2017-12-07 15:32:06 +00:00
console.error(err)
}
)
}
2016-03-14 12:50:19 +00:00
}