2018-08-02 13:34:09 +00:00
|
|
|
import { HttpEventType, HttpResponse } from '@angular/common/http'
|
|
|
|
import { Component, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
|
|
|
|
import { Router } from '@angular/router'
|
|
|
|
import { LoadingBarService } from '@ngx-loading-bar/core'
|
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
|
|
|
import { BytesPipe } from 'ngx-pipes'
|
|
|
|
import { Subscription } from 'rxjs'
|
2018-08-06 13:30:24 +00:00
|
|
|
import { VideoPrivacy } from '../../../../../../shared/models/videos'
|
|
|
|
import { AuthService, ServerService } from '../../../core'
|
|
|
|
import { VideoEdit } from '../../../shared/video/video-edit.model'
|
|
|
|
import { VideoService } from '../../../shared/video/video.service'
|
2018-08-02 13:34:09 +00:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2018-08-06 13:30:24 +00:00
|
|
|
import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
|
|
|
|
import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
|
|
|
|
import { FormValidatorService, UserService } from '@app/shared'
|
2018-08-02 13:34:09 +00:00
|
|
|
import { VideoCaptionService } from '@app/shared/video-caption'
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-video-upload',
|
|
|
|
templateUrl: './video-upload.component.html',
|
|
|
|
styleUrls: [
|
2018-08-06 13:30:24 +00:00
|
|
|
'../shared/video-edit.component.scss',
|
2018-08-02 13:34:09 +00:00
|
|
|
'./video-upload.component.scss'
|
|
|
|
]
|
|
|
|
})
|
2018-08-06 13:12:54 +00:00
|
|
|
export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate {
|
2018-08-02 13:34:09 +00:00
|
|
|
@Output() firstStepDone = new EventEmitter<string>()
|
|
|
|
@ViewChild('videofileInput') videofileInput
|
|
|
|
|
|
|
|
// So that it can be accessed in the template
|
|
|
|
readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
|
|
|
|
|
2018-08-06 13:12:54 +00:00
|
|
|
userVideoQuotaUsed = 0
|
|
|
|
|
2018-08-02 13:34:09 +00:00
|
|
|
isUploadingVideo = false
|
|
|
|
isUpdatingVideo = false
|
|
|
|
videoUploaded = false
|
|
|
|
videoUploadObservable: Subscription = null
|
|
|
|
videoUploadPercents = 0
|
|
|
|
videoUploadedIds = {
|
|
|
|
id: 0,
|
|
|
|
uuid: ''
|
|
|
|
}
|
|
|
|
|
2018-08-06 13:12:54 +00:00
|
|
|
protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
|
2018-08-02 13:34:09 +00:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
protected formValidatorService: FormValidatorService,
|
2018-08-06 13:12:54 +00:00
|
|
|
protected loadingBar: LoadingBarService,
|
|
|
|
protected notificationsService: NotificationsService,
|
|
|
|
protected authService: AuthService,
|
|
|
|
protected serverService: ServerService,
|
|
|
|
protected videoService: VideoService,
|
|
|
|
protected videoCaptionService: VideoCaptionService,
|
2018-08-02 13:34:09 +00:00
|
|
|
private userService: UserService,
|
2018-08-06 13:12:54 +00:00
|
|
|
private router: Router,
|
|
|
|
private i18n: I18n
|
2018-08-02 13:34:09 +00:00
|
|
|
) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
get videoExtensions () {
|
|
|
|
return this.serverService.getConfig().video.file.extensions.join(',')
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
2018-08-06 13:12:54 +00:00
|
|
|
super.ngOnInit()
|
2018-08-02 13:34:09 +00:00
|
|
|
|
|
|
|
this.userService.getMyVideoQuotaUsed()
|
|
|
|
.subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy () {
|
2018-08-06 13:12:54 +00:00
|
|
|
if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canDeactivate () {
|
|
|
|
let text = ''
|
|
|
|
|
|
|
|
if (this.videoUploaded === true) {
|
|
|
|
// FIXME: cannot concatenate strings inside i18n service :/
|
2018-08-06 08:05:06 +00:00
|
|
|
text = this.i18n('Your video was uploaded to your account and is private.') +
|
2018-08-02 13:34:09 +00:00
|
|
|
this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
|
|
|
|
} else {
|
|
|
|
text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
canDeactivate: !this.isUploadingVideo,
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileChange () {
|
|
|
|
this.uploadFirstStep()
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelUpload () {
|
|
|
|
if (this.videoUploadObservable !== null) {
|
|
|
|
this.videoUploadObservable.unsubscribe()
|
|
|
|
this.isUploadingVideo = false
|
|
|
|
this.videoUploadPercents = 0
|
|
|
|
this.videoUploadObservable = null
|
|
|
|
this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadFirstStep () {
|
|
|
|
const videofile = this.videofileInput.nativeElement.files[0] as File
|
|
|
|
if (!videofile) return
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
const videoQuota = this.authService.getUser().videoQuota
|
|
|
|
if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
|
|
|
|
const bytePipes = new BytesPipe()
|
|
|
|
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
|
|
|
|
let name: string
|
|
|
|
|
|
|
|
// If the name of the file is very small, keep the extension
|
|
|
|
if (nameWithoutExtension.length < 3) name = videofile.name
|
|
|
|
else name = nameWithoutExtension
|
|
|
|
|
|
|
|
const privacy = this.firstStepPrivacyId.toString()
|
|
|
|
const nsfw = false
|
|
|
|
const waitTranscoding = true
|
|
|
|
const commentsEnabled = true
|
|
|
|
const channelId = this.firstStepChannelId.toString()
|
|
|
|
|
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('name', name)
|
|
|
|
// Put the video "private" -> we are waiting the user validation of the second step
|
|
|
|
formData.append('privacy', VideoPrivacy.PRIVATE.toString())
|
|
|
|
formData.append('nsfw', '' + nsfw)
|
|
|
|
formData.append('commentsEnabled', '' + commentsEnabled)
|
|
|
|
formData.append('waitTranscoding', '' + waitTranscoding)
|
|
|
|
formData.append('channelId', '' + channelId)
|
|
|
|
formData.append('videofile', videofile)
|
|
|
|
|
|
|
|
this.isUploadingVideo = true
|
|
|
|
this.firstStepDone.emit(name)
|
|
|
|
|
|
|
|
this.form.patchValue({
|
|
|
|
name,
|
|
|
|
privacy,
|
|
|
|
nsfw,
|
|
|
|
channelId
|
|
|
|
})
|
|
|
|
|
|
|
|
this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
|
|
|
|
event => {
|
|
|
|
if (event.type === HttpEventType.UploadProgress) {
|
|
|
|
this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
|
|
|
|
} else if (event instanceof HttpResponse) {
|
|
|
|
this.videoUploaded = true
|
|
|
|
|
|
|
|
this.videoUploadedIds = event.body.video
|
|
|
|
|
|
|
|
this.videoUploadObservable = null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
err => {
|
|
|
|
// Reset progress
|
|
|
|
this.isUploadingVideo = false
|
|
|
|
this.videoUploadPercents = 0
|
|
|
|
this.videoUploadObservable = null
|
|
|
|
this.notificationsService.error(this.i18n('Error'), err.message)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSecondStep () {
|
|
|
|
if (this.checkForm() === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const video = new VideoEdit()
|
|
|
|
video.patch(this.form.value)
|
|
|
|
video.id = this.videoUploadedIds.id
|
|
|
|
video.uuid = this.videoUploadedIds.uuid
|
|
|
|
|
|
|
|
this.isUpdatingVideo = true
|
2018-08-06 13:12:54 +00:00
|
|
|
|
|
|
|
this.updateVideoAndCaptions(video)
|
2018-08-02 13:34:09 +00:00
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.isUpdatingVideo = false
|
|
|
|
this.isUploadingVideo = false
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|