1 import { Component, HostListener, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
6 CanComponentDeactivate,
11 import { HTMLServerConfig } from '@shared/models'
12 import { VideoEditType } from './shared/video-edit.type'
13 import { VideoGoLiveComponent } from './video-add-components/video-go-live.component'
14 import { VideoImportTorrentComponent } from './video-add-components/video-import-torrent.component'
15 import { VideoImportUrlComponent } from './video-add-components/video-import-url.component'
16 import { VideoUploadComponent } from './video-add-components/video-upload.component'
19 selector: 'my-videos-add',
20 templateUrl: './video-add.component.html',
21 styleUrls: [ './video-add.component.scss' ]
23 export class VideoAddComponent implements OnInit, CanComponentDeactivate {
24 @ViewChild('videoUpload') videoUpload: VideoUploadComponent
25 @ViewChild('videoImportUrl') videoImportUrl: VideoImportUrlComponent
26 @ViewChild('videoImportTorrent') videoImportTorrent: VideoImportTorrentComponent
27 @ViewChild('videoGoLive') videoGoLive: VideoGoLiveComponent
31 secondStepType: VideoEditType
39 quotaLeftDaily: string
43 hasNoQuotaLeft = false
44 hasNoQuotaLeftDaily = false
46 serverConfig: HTMLServerConfig
49 private auth: AuthService,
50 private userService: UserService,
51 private hooks: HooksService,
52 private serverService: ServerService,
53 private route: ActivatedRoute,
54 private router: Router
57 get isContactFormEnabled () {
58 return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
61 get userInformationLoaded () {
62 return this.auth.userInformationLoaded
66 this.user = this.auth.getUser()
68 this.serverConfig = this.serverService.getHTMLConfig()
70 if (this.route.snapshot.fragment) {
71 this.onNavChange(this.route.snapshot.fragment)
74 this.buildUploadMessages()
76 this.userService.getMyVideoQuotaUsed()
78 // videoQuota left lower than 10%
79 if (data.videoQuotaUsed > this.user.videoQuota * 0.9) {
80 this.hasNoQuotaLeft = true
83 // unlimited videoQuota
84 if (this.user.videoQuota === -1) {
85 this.hasNoQuotaLeft = false
88 // videoQuotaDaily left lower than 10%
89 if (data.videoQuotaUsedDaily > this.user.videoQuotaDaily * 0.9) {
90 this.hasNoQuotaLeftDaily = true
93 // unlimited videoQuotaDaily
94 if (this.user.videoQuotaDaily === -1) {
95 this.hasNoQuotaLeftDaily = false
100 private async buildUploadMessages () {
101 // eslint-disable-next-line max-len
102 const noQuota = $localize`Sorry, the upload feature is disabled for your account. If you want to add videos, an admin must unlock your quota.`
103 // eslint-disable-next-line max-len
104 const autoBlock = $localize`Uploaded videos are reviewed before publishing for your account. If you want to add videos without moderation review, an admin must turn off your videos auto-block.`
105 // eslint-disable-next-line max-len
106 const quotaLeftDaily = $localize`Your daily video quota is insufficient. If you want to add more videos, you must wait for 24 hours or an admin must increase your daily quota.`
107 // eslint-disable-next-line max-len
108 const quotaLeft = $localize`Your video quota is insufficient. If you want to add more videos, an admin must increase your quota.`
110 const uploadMessages = {
117 this.uploadMessages = await this.hooks.wrapObject(uploadMessages, 'common', 'filter:upload.messages.create.result')
120 onNavChange (newActiveNav: string) {
121 this.activeNav = newActiveNav
123 this.router.navigate([], { fragment: this.activeNav })
126 onFirstStepDone (type: VideoEditType, videoName: string) {
127 this.secondStepType = type
128 this.videoName = videoName
132 this.videoName = undefined
133 this.secondStepType = undefined
136 @HostListener('window:beforeunload', [ '$event' ])
137 onUnload (event: any) {
138 const { text, canDeactivate } = this.canDeactivate()
140 if (canDeactivate) return
142 event.returnValue = text
146 canDeactivate (): { canDeactivate: boolean, text?: string } {
147 if (this.secondStepType === 'upload') return this.videoUpload.canDeactivate()
148 if (this.secondStepType === 'import-url') return this.videoImportUrl.canDeactivate()
149 if (this.secondStepType === 'import-torrent') return this.videoImportTorrent.canDeactivate()
150 if (this.secondStepType === 'go-live') return this.videoGoLive.canDeactivate()
152 return { canDeactivate: true }
155 isVideoImportHttpEnabled () {
156 return this.serverConfig.import.videos.http.enabled
159 isVideoImportTorrentEnabled () {
160 return this.serverConfig.import.videos.torrent.enabled
163 isVideoLiveEnabled () {
164 return this.serverConfig.live.enabled
168 return !!this.secondStepType
172 return this.user.username === 'root'