2017-12-01 17:56:26 +00:00
|
|
|
import { HttpEventType, HttpResponse } from '@angular/common/http'
|
2017-09-14 15:06:31 +00:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core'
|
2017-06-16 12:32:15 +00:00
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms'
|
|
|
|
import { Router } from '@angular/router'
|
2018-01-08 11:53:09 +00:00
|
|
|
import { UserService } from '@app/shared'
|
2017-06-16 12:32:15 +00:00
|
|
|
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'
|
2016-04-14 20:07:46 +00:00
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2016-09-09 20:16:51 +00:00
|
|
|
export class VideoAddComponent extends FormReactive implements OnInit {
|
2017-09-14 15:06:31 +00:00
|
|
|
@ViewChild('videofileInput') videofileInput
|
|
|
|
|
2017-12-07 15:32:06 +00:00
|
|
|
isUploadingVideo = false
|
2017-12-07 16:22:44 +00:00
|
|
|
videoUploaded = false
|
2017-12-07 16:56:59 +00:00
|
|
|
videoUploadPercents = 0
|
2017-12-20 10:09:15 +00:00
|
|
|
videoUploadedIds = {
|
|
|
|
id: 0,
|
|
|
|
uuid: ''
|
|
|
|
}
|
2017-04-16 12:06:48 +00:00
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
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
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
constructor (
|
2016-09-09 20:16:51 +00:00
|
|
|
private formBuilder: FormBuilder,
|
2017-01-27 15:14:11 +00:00
|
|
|
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,
|
2017-10-09 17:12:40 +00:00
|
|
|
private serverService: ServerService,
|
2017-03-22 20:15:55 +00:00
|
|
|
private videoService: VideoService
|
2016-09-09 20:16:51 +00:00
|
|
|
) {
|
2017-06-16 12:32:15 +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(',')
|
|
|
|
}
|
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
buildForm () {
|
2017-12-07 15:32:06 +00:00
|
|
|
this.form = this.formBuilder.group({})
|
2017-06-16 12:32:15 +00:00
|
|
|
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
|
2016-06-07 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
ngOnInit () {
|
|
|
|
this.buildForm()
|
2017-10-30 19:26:06 +00:00
|
|
|
|
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
|
|
|
})
|
2016-06-07 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 16:22:44 +00:00
|
|
|
fileChange () {
|
|
|
|
this.uploadFirstStep()
|
2016-11-27 17:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 15:06:31 +00:00
|
|
|
checkForm () {
|
|
|
|
this.forceCheck()
|
|
|
|
|
|
|
|
return this.form.valid
|
2016-06-07 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:32:06 +00:00
|
|
|
uploadFirstStep () {
|
2017-09-14 15:06:31 +00:00
|
|
|
const videofile = this.videofileInput.nativeElement.files[0]
|
2018-01-08 11:53:09 +00:00
|
|
|
const videoQuota = this.authService.getUser().videoQuota
|
|
|
|
if ((this.userVideoQuotaUsed + videofile.size) > videoQuota) {
|
|
|
|
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()
|
2017-09-14 15:06:31 +00:00
|
|
|
|
|
|
|
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())
|
2017-09-14 15:06:31 +00:00
|
|
|
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)
|
2017-09-14 15:06:31 +00:00
|
|
|
formData.append('videofile', videofile)
|
|
|
|
|
2017-12-07 16:22:44 +00:00
|
|
|
this.isUploadingVideo = true
|
|
|
|
this.form.patchValue({
|
|
|
|
name,
|
|
|
|
privacy,
|
|
|
|
nsfw,
|
|
|
|
channelId
|
|
|
|
})
|
2016-06-07 20:34:02 +00:00
|
|
|
|
2017-09-14 15:06:31 +00:00
|
|
|
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)
|
2017-09-14 15:06:31 +00:00
|
|
|
} 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
|
|
|
|
2017-12-20 10:09:15 +00:00
|
|
|
this.videoUploadedIds = event.body.video
|
2017-09-14 15:06:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-14 21:03:43 +00:00
|
|
|
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
|
2018-01-08 11:53:09 +00:00
|
|
|
this.notificationsService.error('Error', err.message)
|
2017-09-14 21:03:43 +00:00
|
|
|
}
|
2017-09-14 15:06:31 +00:00
|
|
|
)
|
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
|
2017-12-20 10:09:15 +00:00
|
|
|
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.')
|
2017-12-20 10:09:15 +00:00
|
|
|
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
|
|
|
}
|