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

324 lines
9.7 KiB
TypeScript
Raw Normal View History

2020-06-23 12:10:17 +00:00
import { Subscription } from 'rxjs'
import { HttpErrorResponse, HttpEventType, HttpResponse } from '@angular/common/http'
import { AfterViewInit, Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService, UserService } from '@app/core'
import { scrollToTop, uploadErrorHandler } from '@app/helpers'
2020-06-23 12:10:17 +00:00
import { FormValidatorService } from '@app/shared/shared-forms'
2020-08-11 14:50:00 +00:00
import { BytesPipe, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
import { LoadingBarService } from '@ngx-loading-bar/core'
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
2020-06-23 12:10:17 +00:00
import { VideoPrivacy } from '@shared/models'
2020-06-23 12:49:20 +00:00
import { VideoSend } from './video-send'
@Component({
selector: 'my-video-upload',
templateUrl: './video-upload.component.html',
styleUrls: [
'../shared/video-edit.component.scss',
'./video-upload.component.scss',
'./video-send.scss'
]
})
export class VideoUploadComponent extends VideoSend implements OnInit, AfterViewInit, 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: ''
}
formData: FormData
2019-05-17 08:45:53 +00:00
previewfileUpload: File
2018-11-16 09:05:25 +00:00
error: string
enableRetryAfterError: boolean
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,
private router: Router,
private hooks: HooksService
) {
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
})
}
ngAfterViewInit () {
this.hooks.runAction('action:video-upload.init', 'video-edit')
}
ngOnDestroy () {
2018-08-06 13:12:54 +00:00
if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
}
canDeactivate () {
let text = ''
if (this.videoUploaded === true) {
2020-12-03 09:39:43 +00:00
// FIXME: cannot concatenate strings using $localize
text = $localize`Your video was uploaded to your account and is private.` + ' ' +
$localize`But associated data (tags, description...) will be lost, are you sure you want to leave this page?`
} else {
text = $localize`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 $localize`Upload`
2019-05-17 08:45:53 +00:00
return $localize`Upload ${videofile.name}`
2019-05-17 08:45:53 +00:00
}
fileChange () {
this.uploadFirstStep()
}
retryUpload () {
this.enableRetryAfterError = false
this.error = ''
this.uploadVideo()
}
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.enableRetryAfterError = false
this.error = ''
2020-04-14 07:54:22 +00:00
this.notifier.info($localize`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
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()
this.formData = new FormData()
this.formData.append('name', name)
// Put the video "private" -> we are waiting the user validation of the second step
this.formData.append('privacy', VideoPrivacy.PRIVATE.toString())
this.formData.append('nsfw', '' + nsfw)
this.formData.append('commentsEnabled', '' + commentsEnabled)
this.formData.append('downloadEnabled', '' + downloadEnabled)
this.formData.append('waitTranscoding', '' + waitTranscoding)
this.formData.append('channelId', '' + channelId)
this.formData.append('videofile', videofile)
2019-05-17 08:45:53 +00:00
if (this.previewfileUpload) {
this.formData.append('previewfile', this.previewfileUpload)
this.formData.append('thumbnailfile', this.previewfileUpload)
2019-05-17 08:45:53 +00:00
}
this.isUploadingVideo = true
this.firstStepDone.emit(name)
this.form.patchValue({
name,
2020-09-04 08:47:52 +00:00
privacy: this.firstStepPrivacyId,
nsfw,
2020-09-04 08:47:52 +00:00
channelId: this.firstStepChannelId,
2019-05-17 08:45:53 +00:00
previewfile: this.previewfileUpload
})
this.uploadVideo()
}
uploadVideo () {
this.videoUploadObservable = this.videoService.uploadVideo(this.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: HttpErrorResponse) => {
// Reset progress (but keep isUploadingVideo true)
this.videoUploadPercents = 0
this.videoUploadObservable = null
this.enableRetryAfterError = true
this.error = uploadErrorHandler({
err,
name: $localize`video`,
notifier: this.notifier,
sticky: false
})
if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413 ||
err.status === HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) {
this.cancelUpload()
}
}
)
}
isPublishingButtonDisabled () {
return !this.form.valid ||
this.isUpdatingVideo === true ||
this.videoUploaded !== true ||
!this.videoUploadedIds.id
}
updateSecondStep () {
if (this.isPublishingButtonDisabled() || !this.checkForm()) {
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($localize`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 videoSizeBytes = bytePipes.transform(videofile.size, 0)
const videoQuotaUsedBytes = bytePipes.transform(this.userVideoQuotaUsed, 0)
const videoQuotaBytes = bytePipes.transform(videoQuota, 0)
const msg = $localize`Your video quota is exceeded with this video (
video size: ${videoSizeBytes}, used: ${videoQuotaUsedBytes}, quota: ${videoQuotaBytes})`
2019-05-17 08:45:53 +00:00
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 videoSizeBytes = bytePipes.transform(videofile.size, 0)
const quotaUsedDailyBytes = bytePipes.transform(this.userVideoQuotaUsedDaily, 0)
const quotaDailyBytes = bytePipes.transform(videoQuotaDaily, 0)
const msg = $localize`Your daily video quota is exceeded with this video (
video size: ${videoSizeBytes}, used: ${quotaUsedDailyBytes}, quota: ${quotaDailyBytes})`
2019-05-17 08:45:53 +00:00
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
}
}