2020-06-23 12:10:17 +00:00
|
|
|
import { Subscription } from 'rxjs'
|
2020-11-23 09:45:42 +00:00
|
|
|
import { HttpErrorResponse, 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'
|
2018-08-02 13:34:09 +00:00
|
|
|
import { Router } from '@angular/router'
|
2020-06-23 12:10:17 +00:00
|
|
|
import { AuthService, CanComponentDeactivate, Notifier, ServerService, UserService } from '@app/core'
|
2020-11-23 09:45:42 +00:00
|
|
|
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'
|
2018-08-02 13:34:09 +00:00
|
|
|
import { LoadingBarService } from '@ngx-loading-bar/core'
|
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'
|
2020-12-08 20:16:10 +00:00
|
|
|
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
|
2018-08-02 13:34:09 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-video-upload',
|
|
|
|
templateUrl: './video-upload.component.html',
|
|
|
|
styleUrls: [
|
2018-08-06 13:30:24 +00:00
|
|
|
'../shared/video-edit.component.scss',
|
2019-01-16 15:05:40 +00:00
|
|
|
'./video-upload.component.scss',
|
|
|
|
'./video-send.scss'
|
2018-08-02 13:34:09 +00:00
|
|
|
]
|
|
|
|
})
|
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>()
|
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>
|
2018-08-02 13:34:09 +00:00
|
|
|
|
|
|
|
// 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-28 07:01:35 +00:00
|
|
|
userVideoQuotaUsedDaily = 0
|
2018-08-06 13:12:54 +00:00
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
isUploadingAudioFile = false
|
2018-08-02 13:34:09 +00:00
|
|
|
isUploadingVideo = false
|
|
|
|
isUpdatingVideo = false
|
2019-05-17 08:45:53 +00:00
|
|
|
|
2018-08-02 13:34:09 +00:00
|
|
|
videoUploaded = false
|
|
|
|
videoUploadObservable: Subscription = null
|
|
|
|
videoUploadPercents = 0
|
|
|
|
videoUploadedIds = {
|
|
|
|
id: 0,
|
|
|
|
uuid: ''
|
|
|
|
}
|
2020-11-23 09:45:42 +00:00
|
|
|
formData: FormData
|
2019-05-17 08:45:53 +00:00
|
|
|
|
|
|
|
previewfileUpload: File
|
2018-08-02 13:34:09 +00:00
|
|
|
|
2018-11-16 09:05:25 +00:00
|
|
|
error: string
|
2020-11-23 09:45:42 +00:00
|
|
|
enableRetryAfterError: boolean
|
2018-08-02 13:34:09 +00:00
|
|
|
|
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,
|
2018-12-19 15:04:34 +00:00
|
|
|
protected notifier: Notifier,
|
2018-08-06 13:12:54 +00:00
|
|
|
protected authService: AuthService,
|
|
|
|
protected serverService: ServerService,
|
|
|
|
protected videoService: VideoService,
|
|
|
|
protected videoCaptionService: VideoCaptionService,
|
2018-08-02 13:34:09 +00:00
|
|
|
private userService: UserService,
|
2020-08-12 08:40:04 +00:00
|
|
|
private router: Router
|
|
|
|
) {
|
2018-08-02 13:34:09 +00:00
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
get videoExtensions () {
|
2020-03-06 23:15:49 +00:00
|
|
|
return this.serverConfig.video.file.extensions.join(', ')
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
2018-08-06 13:12:54 +00:00
|
|
|
super.ngOnInit()
|
2018-08-02 13:34:09 +00:00
|
|
|
|
|
|
|
this.userService.getMyVideoQuotaUsed()
|
2018-09-03 07:30:13 +00:00
|
|
|
.subscribe(data => {
|
|
|
|
this.userVideoQuotaUsed = data.videoQuotaUsed
|
|
|
|
this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
|
|
|
|
})
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-12-03 09:39:43 +00:00
|
|
|
// FIXME: cannot concatenate strings using $localize
|
2020-08-12 08:40:04 +00:00
|
|
|
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?`
|
2018-08-02 13:34:09 +00:00
|
|
|
} else {
|
2020-08-12 08:40:04 +00:00
|
|
|
text = $localize`Your video is not uploaded yet, are you sure you want to leave this page?`
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
canDeactivate: !this.isUploadingVideo,
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
getVideoFile () {
|
|
|
|
return this.videofileInput.nativeElement.files[0]
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:22:04 +00:00
|
|
|
setVideoFile (files: FileList) {
|
|
|
|
this.videofileInput.nativeElement.files = files
|
|
|
|
this.fileChange()
|
|
|
|
}
|
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
getAudioUploadLabel () {
|
|
|
|
const videofile = this.getVideoFile()
|
2020-08-12 08:40:04 +00:00
|
|
|
if (!videofile) return $localize`Upload`
|
2019-05-17 08:45:53 +00:00
|
|
|
|
2020-08-12 08:40:04 +00:00
|
|
|
return $localize`Upload ${videofile.name}`
|
2019-05-17 08:45:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:34:09 +00:00
|
|
|
fileChange () {
|
|
|
|
this.uploadFirstStep()
|
|
|
|
}
|
|
|
|
|
2020-11-23 09:45:42 +00:00
|
|
|
retryUpload () {
|
|
|
|
this.enableRetryAfterError = false
|
|
|
|
this.error = ''
|
|
|
|
this.uploadVideo()
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:34:09 +00:00
|
|
|
cancelUpload () {
|
|
|
|
if (this.videoUploadObservable !== null) {
|
|
|
|
this.videoUploadObservable.unsubscribe()
|
2020-12-08 20:16:10 +00:00
|
|
|
}
|
2020-04-14 07:54:22 +00:00
|
|
|
|
2020-12-08 20:16:10 +00:00
|
|
|
this.isUploadingVideo = false
|
|
|
|
this.videoUploadPercents = 0
|
|
|
|
this.videoUploadObservable = null
|
2020-04-14 07:54:22 +00:00
|
|
|
|
2020-12-08 20:16:10 +00:00
|
|
|
this.firstStepError.emit()
|
|
|
|
this.enableRetryAfterError = false
|
|
|
|
this.error = ''
|
2020-04-14 07:54:22 +00:00
|
|
|
|
2020-12-08 20:16:10 +00:00
|
|
|
this.notifier.info($localize`Upload cancelled`)
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
uploadFirstStep (clickedOnButton = false) {
|
|
|
|
const videofile = this.getVideoFile()
|
2018-08-02 13:34:09 +00:00
|
|
|
if (!videofile) return
|
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
if (!this.checkGlobalUserQuota(videofile)) return
|
|
|
|
if (!this.checkDailyUserQuota(videofile)) return
|
2018-08-02 13:34:09 +00:00
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
if (clickedOnButton === false && this.isAudioFile(videofile.name)) {
|
|
|
|
this.isUploadingAudioFile = true
|
2018-08-28 07:01:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-11 13:52:50 +00:00
|
|
|
// Build name field
|
2018-08-02 13:34:09 +00:00
|
|
|
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
|
2018-08-02 13:34:09 +00:00
|
|
|
const waitTranscoding = true
|
|
|
|
const commentsEnabled = true
|
2018-10-08 12:45:22 +00:00
|
|
|
const downloadEnabled = true
|
2018-08-02 13:34:09 +00:00
|
|
|
const channelId = this.firstStepChannelId.toString()
|
|
|
|
|
2020-11-23 09:45:42 +00:00
|
|
|
this.formData = new FormData()
|
|
|
|
this.formData.append('name', name)
|
2018-08-02 13:34:09 +00:00
|
|
|
// Put the video "private" -> we are waiting the user validation of the second step
|
2020-11-23 09:45:42 +00:00
|
|
|
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)
|
2018-08-02 13:34:09 +00:00
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
if (this.previewfileUpload) {
|
2020-11-23 09:45:42 +00:00
|
|
|
this.formData.append('previewfile', this.previewfileUpload)
|
|
|
|
this.formData.append('thumbnailfile', this.previewfileUpload)
|
2019-05-17 08:45:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:34:09 +00:00
|
|
|
this.isUploadingVideo = true
|
|
|
|
this.firstStepDone.emit(name)
|
|
|
|
|
|
|
|
this.form.patchValue({
|
|
|
|
name,
|
2020-09-04 08:47:52 +00:00
|
|
|
privacy: this.firstStepPrivacyId,
|
2018-08-02 13:34:09 +00:00
|
|
|
nsfw,
|
2020-09-04 08:47:52 +00:00
|
|
|
channelId: this.firstStepChannelId,
|
2019-05-17 08:45:53 +00:00
|
|
|
previewfile: this.previewfileUpload
|
2018-08-02 13:34:09 +00:00
|
|
|
})
|
|
|
|
|
2020-11-23 09:45:42 +00:00
|
|
|
this.uploadVideo()
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadVideo () {
|
|
|
|
this.videoUploadObservable = this.videoService.uploadVideo(this.formData).subscribe(
|
2018-08-02 13:34:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-23 09:45:42 +00:00
|
|
|
(err: HttpErrorResponse) => {
|
|
|
|
// Reset progress (but keep isUploadingVideo true)
|
2018-08-02 13:34:09 +00:00
|
|
|
this.videoUploadPercents = 0
|
|
|
|
this.videoUploadObservable = null
|
2020-11-23 09:45:42 +00:00
|
|
|
this.enableRetryAfterError = true
|
|
|
|
|
|
|
|
this.error = uploadErrorHandler({
|
|
|
|
err,
|
|
|
|
name: $localize`video`,
|
|
|
|
notifier: this.notifier,
|
|
|
|
sticky: false
|
|
|
|
})
|
2020-12-08 20:16:10 +00:00
|
|
|
|
|
|
|
if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413 ||
|
|
|
|
err.status === HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) {
|
|
|
|
this.cancelUpload()
|
|
|
|
}
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-01 07:04:43 +00:00
|
|
|
isPublishingButtonDisabled () {
|
|
|
|
return !this.form.valid ||
|
|
|
|
this.isUpdatingVideo === true ||
|
2020-08-03 14:16:58 +00:00
|
|
|
this.videoUploaded !== true ||
|
|
|
|
!this.videoUploadedIds.id
|
2018-10-01 07:04:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:34:09 +00:00
|
|
|
updateSecondStep () {
|
2020-08-03 14:16:58 +00:00
|
|
|
if (this.isPublishingButtonDisabled() || !this.checkForm()) {
|
2018-08-02 13:34:09 +00:00
|
|
|
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
|
|
|
|
|
2020-08-12 08:40:04 +00:00
|
|
|
this.notifier.success($localize`Video published.`)
|
2018-08-02 13:34:09 +00:00
|
|
|
this.router.navigate([ '/videos/watch', video.uuid ])
|
|
|
|
},
|
|
|
|
|
|
|
|
err => {
|
2018-11-16 09:05:25 +00:00
|
|
|
this.error = err.message
|
|
|
|
scrollToTop()
|
2018-08-02 13:34:09 +00:00
|
|
|
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) {
|
2020-08-12 08:40:04 +00:00
|
|
|
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) {
|
2020-08-12 08:40:04 +00:00
|
|
|
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
|
|
|
}
|
2018-08-02 13:34:09 +00:00
|
|
|
}
|