1 import { SortMeta } from 'primeng/api'
2 import { finalize } from 'rxjs/operators'
3 import { Component, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'
6 import { prepareIcu } from '@app/helpers'
7 import { AdvancedInputFilter } from '@app/shared/shared-forms'
8 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
9 import { VideoBlockComponent, VideoBlockService } from '@app/shared/shared-moderation'
10 import { VideoActionsDisplayType } from '@app/shared/shared-video-miniature'
11 import { getAllFiles } from '@shared/core-utils'
12 import { UserRight, VideoFile, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
13 import { VideoAdminService } from './video-admin.service'
16 selector: 'my-video-list',
17 templateUrl: './video-list.component.html',
18 styleUrls: [ './video-list.component.scss' ]
20 export class VideoListComponent extends RestTable <Video> implements OnInit {
21 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
26 sort: SortMeta = { field: 'publishedAt', order: -1 }
27 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
29 bulkActions: DropdownAction<Video[]>[][] = []
31 inputFilters: AdvancedInputFilter[]
33 videoActionsOptions: VideoActionsDisplayType = {
52 protected route: ActivatedRoute,
53 protected router: Router,
54 private confirmService: ConfirmService,
55 private auth: AuthService,
56 private notifier: Notifier,
57 private videoService: VideoService,
58 private videoAdminService: VideoAdminService,
59 private videoBlockService: VideoBlockService
65 return this.auth.getUser()
71 this.inputFilters = this.videoAdminService.buildAdminInputFilter()
76 label: $localize`Delete`,
77 handler: videos => this.removeVideos(videos),
78 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO),
82 label: $localize`Block`,
83 handler: videos => this.videoBlockModal.show(videos),
84 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => !v.blacklisted),
88 label: $localize`Unblock`,
89 handler: videos => this.unblockVideos(videos),
90 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => v.blacklisted),
96 label: $localize`Run HLS transcoding`,
97 handler: videos => this.runTranscoding(videos, 'hls'),
98 isDisplayed: videos => videos.every(v => v.canRunTranscoding(this.authUser)),
102 label: $localize`Run WebTorrent transcoding`,
103 handler: videos => this.runTranscoding(videos, 'webtorrent'),
104 isDisplayed: videos => videos.every(v => v.canRunTranscoding(this.authUser)),
108 label: $localize`Delete HLS files`,
109 handler: videos => this.removeVideoFiles(videos, 'hls'),
110 isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
114 label: $localize`Delete WebTorrent files`,
115 handler: videos => this.removeVideoFiles(videos, 'webtorrent'),
116 isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
124 return 'VideoListComponent'
127 getPrivacyBadgeClass (video: Video) {
128 if (video.privacy.id === VideoPrivacy.PUBLIC) return 'badge-green'
130 return 'badge-yellow'
133 isUnpublished (video: Video) {
134 return video.state.id !== VideoState.LIVE_ENDED && video.state.id !== VideoState.PUBLISHED
137 isAccountBlocked (video: Video) {
138 return video.blockedOwner
141 isServerBlocked (video: Video) {
142 return video.blockedServer
145 isVideoBlocked (video: Video) {
146 return video.blacklisted
149 isImport (video: Video) {
150 return video.state.id === VideoState.TO_IMPORT
153 isHLS (video: Video) {
154 const p = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
157 return p.files.length !== 0
160 isWebTorrent (video: Video) {
161 return video.files.length !== 0
164 hasObjectStorage (video: Video) {
165 if (!video.isLocal) return false
167 const files = getAllFiles(video)
169 return files.some(f => !f.fileUrl.startsWith(window.location.origin))
172 canRemoveOneFile (video: Video) {
173 return video.canRemoveOneFile(this.authUser)
176 getFilesSize (video: Video) {
177 let files = video.files
179 if (this.isHLS(video)) {
180 files = files.concat(video.streamingPlaylists[0].files)
183 return files.reduce((p, f) => p += f.size, 0)
186 async removeVideoFile (video: Video, file: VideoFile, type: 'hls' | 'webtorrent') {
187 const message = $localize`Are you sure you want to delete this ${file.resolution.label} file?`
188 const res = await this.confirmService.confirm(message, $localize`Delete file`)
189 if (res === false) return
191 this.videoService.removeFile(video.uuid, file.id, type)
194 this.notifier.success($localize`File removed.`)
198 error: err => this.notifier.error(err.message)
202 protected reloadDataInternal () {
205 this.videoAdminService.getAdminVideos({
206 pagination: this.pagination,
209 }).pipe(finalize(() => this.loading = false))
211 next: resultList => {
212 this.videos = resultList.data
213 this.totalRecords = resultList.total
216 error: err => this.notifier.error(err.message)
220 private async removeVideos (videos: Video[]) {
221 const message = prepareIcu($localize`Are you sure you want to delete {count, plural, =1 {this video} other {these {count} videos}}?`)(
222 { count: videos.length },
223 $localize`Are you sure you want to delete these ${videos.length} videos?`
226 const res = await this.confirmService.confirm(message, $localize`Delete`)
227 if (res === false) return
229 this.videoService.removeVideo(videos.map(v => v.id))
232 this.notifier.success(
233 prepareIcu($localize`Deleted {count, plural, =1 {1 video} other {{count} videos}}.`)(
234 { count: videos.length },
235 $localize`Deleted ${videos.length} videos.`
242 error: err => this.notifier.error(err.message)
246 private unblockVideos (videos: Video[]) {
247 this.videoBlockService.unblockVideo(videos.map(v => v.id))
250 this.notifier.success(
251 prepareIcu($localize`Unblocked {count, plural, =1 {1 video} other {{count} videos}}.`)(
252 { count: videos.length },
253 $localize`Unblocked ${videos.length} videos.`
260 error: err => this.notifier.error(err.message)
264 private async removeVideoFiles (videos: Video[], type: 'hls' | 'webtorrent') {
267 if (type === 'hls') {
268 // eslint-disable-next-line max-len
269 message = prepareIcu($localize`Are you sure you want to delete {count, plural, =1 {1 HLS streaming playlist} other {{count} HLS streaming playlists}}?`)(
270 { count: videos.length },
271 $localize`Are you sure you want to delete ${videos.length} HLS streaming playlists?`
274 // eslint-disable-next-line max-len
275 message = prepareIcu($localize`Are you sure you want to delete WebTorrent files of {count, plural, =1 {1 video} other {{count} videos}}?`)(
276 { count: videos.length },
277 $localize`Are you sure you want to delete WebTorrent files of ${videos.length} videos?`
281 const res = await this.confirmService.confirm(message, $localize`Delete`)
282 if (res === false) return
284 this.videoService.removeVideoFiles(videos.map(v => v.id), type)
287 this.notifier.success($localize`Files were removed.`)
291 error: err => this.notifier.error(err.message)
295 private runTranscoding (videos: Video[], type: 'hls' | 'webtorrent') {
296 this.videoService.runTranscoding(videos.map(v => v.id), type)
299 this.notifier.success($localize`Transcoding jobs created.`)
304 error: err => this.notifier.error(err.message)