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

307 lines
9.2 KiB
TypeScript
Raw Normal View History

2020-06-23 12:10:17 +00:00
import { BytesPipe } from 'ngx-pipes'
import { Subscription } from 'rxjs'
import { HttpEventType, HttpResponse } from '@angular/common/http'
2018-10-18 12:35:31 +00:00
import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
2020-06-23 12:10:17 +00:00
import { AuthService, CanComponentDeactivate, Notifier, ServerService, UserService } from '@app/core'
import { scrollToTop } from '@app/helpers'
import { FormValidatorService } from '@app/shared/shared-forms'
import { VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
import { LoadingBarService } from '@ngx-loading-bar/core'
import { I18n } from '@ngx-translate/i18n-polyfill'
2020-06-23 12:10:17 +00:00
import { VideoPrivacy } from '@shared/models'
@Component({
selector: 'my-video-upload',
templateUrl: './video-upload.component.html',
styleUrls: [
'../shared/video-edit.component.scss',
'./video-upload.component.scss',
'./video-send.scss'
]
})
2018-08-06 13:12:54 +00:00
export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate {
@Output() firstStepDone = new EventEmitter<string>()
2018-11-16 09:05:25 +00:00
@Output() firstStepError = new EventEmitter<void>()
2020-02-07 09:00:34 +00:00
@ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement>
// 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
userVideoQuotaUsedDaily = 0
2018-08-06 13:12:54 +00:00
2019-05-17 08:45:53 +00:00
isUploadingAudioFile = false
isUploadingVideo = false
isUpdatingVideo = false
2019-05-17 08:45:53 +00:00
videoUploaded = false
videoUploadObservable: Subscription = null
videoUploadPercents = 0
videoUploadedIds = {
id: 0,
uuid: ''
}
2019-05-17 08:45:53 +00:00
2018-12-11 13:52:50 +00:00
waitTranscodingEnabled = true
2019-05-17 08:45:53 +00:00
previewfileUpload: File
2018-11-16 09:05:25 +00:00
error: string
2018-08-06 13:12:54 +00:00
protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
constructor (
protected formValidatorService: FormValidatorService,
2018-08-06 13:12:54 +00:00
protected loadingBar: LoadingBarService,
protected notifier: Notifier,
2018-08-06 13:12:54 +00:00
protected authService: AuthService,
protected serverService: ServerService,
protected videoService: VideoService,
protected videoCaptionService: VideoCaptionService,
private userService: UserService,
2018-08-06 13:12:54 +00:00
private router: Router,
private i18n: I18n
) {
super()
}
get videoExtensions () {
2020-03-06 23:15:49 +00:00
return this.serverConfig.video.file.extensions.join(', ')
}
ngOnInit () {
2018-08-06 13:12:54 +00:00
super.ngOnInit()
this.userService.getMyVideoQuotaUsed()
2018-09-03 07:30:13 +00:00
.subscribe(data => {
this.userVideoQuotaUsed = data.videoQuotaUsed
this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
})
}
ngOnDestroy () {
2018-08-06 13:12:54 +00:00
if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
}
canDeactivate () {
let text = ''
if (this.videoUploaded === true) {
// FIXME: cannot concatenate strings inside i18n service :/
text = this.i18n('Your video was uploaded to your account and is private.') + ' ' +
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
}
}
2019-05-17 08:45:53 +00:00
getVideoFile () {
return this.videofileInput.nativeElement.files[0]
}
setVideoFile (files: FileList) {
this.videofileInput.nativeElement.files = files
this.fileChange()
}
2019-05-17 08:45:53 +00:00
getAudioUploadLabel () {
const videofile = this.getVideoFile()
if (!videofile) return this.i18n('Upload')
return this.i18n('Upload {{videofileName}}', { videofileName: videofile.name })
}
fileChange () {
this.uploadFirstStep()
}
cancelUpload () {
if (this.videoUploadObservable !== null) {
this.videoUploadObservable.unsubscribe()
2020-04-14 07:54:22 +00:00
this.isUploadingVideo = false
this.videoUploadPercents = 0
this.videoUploadObservable = null
2020-04-14 07:54:22 +00:00
this.firstStepError.emit()
this.notifier.info(this.i18n('Upload cancelled'))
}
}
2019-05-17 08:45:53 +00:00
uploadFirstStep (clickedOnButton = false) {
const videofile = this.getVideoFile()
if (!videofile) return
2019-05-17 08:45:53 +00:00
if (!this.checkGlobalUserQuota(videofile)) return
if (!this.checkDailyUserQuota(videofile)) return
2019-05-17 08:45:53 +00:00
if (clickedOnButton === false && this.isAudioFile(videofile.name)) {
this.isUploadingAudioFile = true
return
}
2018-12-11 13:52:50 +00:00
// Build name field
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
2018-12-11 13:52:50 +00:00
// Force user to wait transcoding for unsupported video types in web browsers
if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) {
this.waitTranscodingEnabled = false
}
const privacy = this.firstStepPrivacyId.toString()
2019-12-18 14:31:54 +00:00
const nsfw = this.serverConfig.instance.isNSFW
const waitTranscoding = true
const commentsEnabled = true
const downloadEnabled = 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('downloadEnabled', '' + downloadEnabled)
formData.append('waitTranscoding', '' + waitTranscoding)
formData.append('channelId', '' + channelId)
formData.append('videofile', videofile)
2019-05-17 08:45:53 +00:00
if (this.previewfileUpload) {
formData.append('previewfile', this.previewfileUpload)
formData.append('thumbnailfile', this.previewfileUpload)
}
this.isUploadingVideo = true
this.firstStepDone.emit(name)
this.form.patchValue({
name,
privacy,
nsfw,
2019-05-17 08:45:53 +00:00
channelId,
previewfile: this.previewfileUpload
})
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
2018-11-16 09:05:25 +00:00
this.firstStepError.emit()
this.notifier.error(err.message)
}
)
}
isPublishingButtonDisabled () {
return !this.form.valid ||
this.isUpdatingVideo === true ||
this.videoUploaded !== true
}
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)
.subscribe(
() => {
this.isUpdatingVideo = false
this.isUploadingVideo = false
this.notifier.success(this.i18n('Video published.'))
this.router.navigate([ '/videos/watch', video.uuid ])
},
err => {
2018-11-16 09:05:25 +00:00
this.error = err.message
scrollToTop()
console.error(err)
}
)
}
2019-05-17 08:45:53 +00:00
private checkGlobalUserQuota (videofile: File) {
const bytePipes = new BytesPipe()
// Check global user quota
const videoQuota = this.authService.getUser().videoQuota
if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
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.notifier.error(msg)
return false
}
return true
}
private checkDailyUserQuota (videofile: File) {
const bytePipes = new BytesPipe()
// Check daily user quota
const videoQuotaDaily = this.authService.getUser().videoQuotaDaily
if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) {
const msg = this.i18n(
'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})',
{
videoSize: bytePipes.transform(videofile.size, 0),
quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0),
quotaDaily: bytePipes.transform(videoQuotaDaily, 0)
}
)
this.notifier.error(msg)
return false
}
return true
}
private isAudioFile (filename: string) {
2020-02-07 07:51:28 +00:00
const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ]
return extensions.some(e => filename.endsWith(e))
2019-05-17 08:45:53 +00:00
}
}