2017-12-01 17:56:26 +00:00
|
|
|
import { HttpEventType, HttpResponse } from '@angular/common/http'
|
2018-01-25 18:01:13 +00:00
|
|
|
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
|
2017-06-16 12:32:15 +00:00
|
|
|
import { Router } from '@angular/router'
|
2018-01-08 11:53:09 +00:00
|
|
|
import { UserService } from '@app/shared'
|
2018-01-25 18:01:13 +00:00
|
|
|
import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
|
2018-02-16 16:24:47 +00:00
|
|
|
import { LoadingBarService } from '@ngx-loading-bar/core'
|
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'
|
2018-05-15 09:55:51 +00:00
|
|
|
import { Subscription } from 'rxjs'
|
2018-07-16 15:36:42 +00:00
|
|
|
import { VideoConstant, 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-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'
|
2018-06-04 14:21:17 +00:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2018-06-05 08:58:45 +00:00
|
|
|
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
|
2018-07-12 17:02:00 +00:00
|
|
|
import { switchMap } from 'rxjs/operators'
|
|
|
|
import { VideoCaptionService } from '@app/shared/video-caption'
|
2018-07-16 15:36:42 +00:00
|
|
|
import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
|
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
|
|
|
})
|
2018-01-25 18:01:13 +00:00
|
|
|
export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
|
2017-09-14 15:06:31 +00:00
|
|
|
@ViewChild('videofileInput') videofileInput
|
|
|
|
|
2018-06-15 14:52:15 +00:00
|
|
|
// So that it can be accessed in the template
|
|
|
|
readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
|
|
|
|
|
2017-12-07 15:32:06 +00:00
|
|
|
isUploadingVideo = false
|
2018-02-16 16:24:47 +00:00
|
|
|
isUpdatingVideo = false
|
2017-12-07 16:22:44 +00:00
|
|
|
videoUploaded = false
|
2018-01-25 18:01:13 +00:00
|
|
|
videoUploadObservable: Subscription = null
|
2017-12-07 16:56:59 +00:00
|
|
|
videoUploadPercents = 0
|
2017-12-20 10:09:15 +00:00
|
|
|
videoUploadedIds = {
|
|
|
|
id: 0,
|
|
|
|
uuid: ''
|
|
|
|
}
|
2018-02-20 08:53:42 +00:00
|
|
|
videoFileName: string
|
2017-04-16 12:06:48 +00:00
|
|
|
|
2018-05-25 16:32:53 +00:00
|
|
|
userVideoChannels: { id: number, label: string, support: string }[] = []
|
2018-01-08 11:53:09 +00:00
|
|
|
userVideoQuotaUsed = 0
|
2018-07-16 15:36:42 +00:00
|
|
|
videoPrivacies: VideoConstant<string>[] = []
|
2017-12-08 07:39:15 +00:00
|
|
|
firstStepPrivacyId = 0
|
|
|
|
firstStepChannelId = 0
|
2018-07-16 15:36:42 +00:00
|
|
|
videoCaptions: VideoCaptionEdit[] = []
|
2016-03-14 12:50:19 +00:00
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
constructor (
|
2018-06-05 08:58:45 +00:00
|
|
|
protected formValidatorService: FormValidatorService,
|
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,
|
2018-02-16 16:24:47 +00:00
|
|
|
private videoService: VideoService,
|
2018-06-04 14:21:17 +00:00
|
|
|
private loadingBar: LoadingBarService,
|
2018-07-12 17:02:00 +00:00
|
|
|
private i18n: I18n,
|
|
|
|
private videoCaptionService: VideoCaptionService
|
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
|
|
|
ngOnInit () {
|
2018-06-05 08:58:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-25 18:01:13 +00:00
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.videoUploadObservable) {
|
|
|
|
this.videoUploadObservable.unsubscribe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:40:23 +00:00
|
|
|
canDeactivate () {
|
2018-01-25 18:01:13 +00:00
|
|
|
let text = ''
|
|
|
|
|
|
|
|
if (this.videoUploaded === true) {
|
2018-06-04 14:21:17 +00:00
|
|
|
// FIXME: cannot concatenate strings inside i18n service :/
|
|
|
|
text = this.i18n('Your video was uploaded in your account and is private.') +
|
|
|
|
this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
|
2018-01-25 18:01:13 +00:00
|
|
|
} else {
|
2018-06-04 14:21:17 +00:00
|
|
|
text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
|
2018-01-25 18:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
canDeactivate: !this.isUploadingVideo,
|
|
|
|
text
|
|
|
|
}
|
2018-01-25 17:40:23 +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
|
|
|
}
|
|
|
|
|
2018-01-23 14:01:38 +00:00
|
|
|
cancelUpload () {
|
|
|
|
if (this.videoUploadObservable !== null) {
|
|
|
|
this.videoUploadObservable.unsubscribe()
|
|
|
|
this.isUploadingVideo = false
|
|
|
|
this.videoUploadPercents = 0
|
|
|
|
this.videoUploadObservable = null
|
2018-06-04 14:21:17 +00:00
|
|
|
this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
|
2018-01-23 14:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:32:06 +00:00
|
|
|
uploadFirstStep () {
|
2018-02-22 15:54:08 +00:00
|
|
|
const videofile = this.videofileInput.nativeElement.files[0] as File
|
2018-01-09 14:29:05 +00:00
|
|
|
if (!videofile) return
|
|
|
|
|
2018-06-29 13:47:15 +00:00
|
|
|
// Cannot upload videos > 8GB for now
|
|
|
|
if (videofile.size > 8 * 1024 * 1024 * 1024) {
|
|
|
|
this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 8GB'))
|
|
|
|
return
|
|
|
|
}
|
2018-02-22 15:54:08 +00:00
|
|
|
|
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()
|
|
|
|
|
2018-06-04 14:21:17 +00:00
|
|
|
const msg = this.i18n(
|
|
|
|
'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
|
|
|
|
{
|
|
|
|
videoSize: bytePipes.transform(videofile.size, 0),
|
|
|
|
videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
|
|
|
|
videoQuota: bytePipes.transform(videoQuota, 0)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
this.notificationsService.error(this.i18n('Error'), msg)
|
2018-01-08 11:53:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-20 08:53:42 +00:00
|
|
|
this.videoFileName = videofile.name
|
|
|
|
|
2018-03-12 10:29:38 +00:00
|
|
|
const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
|
|
|
|
let name: string
|
|
|
|
|
|
|
|
// If the name of the file is very small, keep the extension
|
2018-07-12 17:02:00 +00:00
|
|
|
if (nameWithoutExtension.length < 3) name = videofile.name
|
|
|
|
else name = nameWithoutExtension
|
2018-03-12 10:29:38 +00:00
|
|
|
|
2017-12-08 07:39:15 +00:00
|
|
|
const privacy = this.firstStepPrivacyId.toString()
|
2017-12-07 16:22:44 +00:00
|
|
|
const nsfw = false
|
2018-06-12 18:04:58 +00:00
|
|
|
const waitTranscoding = true
|
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)
|
2018-02-26 10:44:54 +00:00
|
|
|
// Put the video "private" -> we are waiting the user validation of the second step
|
2017-12-08 07:39:15 +00:00
|
|
|
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)
|
2018-06-12 18:04:58 +00:00
|
|
|
formData.append('waitTranscoding', '' + waitTranscoding)
|
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
|
|
|
|
2018-01-23 14:01:38 +00:00
|
|
|
this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
|
2017-09-14 15:06:31 +00:00
|
|
|
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) {
|
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
|
2018-01-23 14:01:38 +00:00
|
|
|
|
|
|
|
this.videoUploadObservable = null
|
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-23 14:01:38 +00:00
|
|
|
this.videoUploadObservable = null
|
2018-06-04 14:21:17 +00:00
|
|
|
this.notificationsService.error(this.i18n('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)
|
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
|
|
|
|
2018-02-16 16:24:47 +00:00
|
|
|
this.isUpdatingVideo = true
|
|
|
|
this.loadingBar.start()
|
2017-12-07 15:32:06 +00:00
|
|
|
this.videoService.updateVideo(video)
|
2018-07-12 17:02:00 +00:00
|
|
|
.pipe(
|
|
|
|
// Then update captions
|
|
|
|
switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions))
|
|
|
|
)
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.isUpdatingVideo = false
|
|
|
|
this.isUploadingVideo = false
|
|
|
|
this.loadingBar.complete()
|
|
|
|
|
|
|
|
this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
|
|
|
|
this.router.navigate([ '/videos/watch', video.uuid ])
|
|
|
|
},
|
|
|
|
|
|
|
|
err => {
|
|
|
|
this.isUpdatingVideo = false
|
|
|
|
this.notificationsService.error(this.i18n('Error'), err.message)
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
)
|
2017-12-07 15:32:06 +00:00
|
|
|
}
|
2016-03-14 12:50:19 +00:00
|
|
|
}
|