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 implements OnInit {
21 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
26 sort: SortMeta = { field: 'publishedAt', order: -1 }
27 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
29 bulkVideoActions: DropdownAction<Video[]>[][] = []
31 selectedVideos: Video[] = []
33 inputFilters: AdvancedInputFilter[]
35 videoActionsOptions: VideoActionsDisplayType = {
54 protected route: ActivatedRoute,
55 protected router: Router,
56 private confirmService: ConfirmService,
57 private auth: AuthService,
58 private notifier: Notifier,
59 private videoService: VideoService,
60 private videoAdminService: VideoAdminService,
61 private videoBlockService: VideoBlockService
67 return this.auth.getUser()
73 this.inputFilters = this.videoAdminService.buildAdminInputFilter()
75 this.bulkVideoActions = [
78 label: $localize`Delete`,
79 handler: videos => this.removeVideos(videos),
80 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO),
84 label: $localize`Block`,
85 handler: videos => this.videoBlockModal.show(videos),
86 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => !v.blacklisted),
90 label: $localize`Unblock`,
91 handler: videos => this.unblockVideos(videos),
92 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => v.blacklisted),
98 label: $localize`Run HLS transcoding`,
99 handler: videos => this.runTranscoding(videos, 'hls'),
100 isDisplayed: videos => videos.every(v => v.canRunTranscoding(this.authUser)),
104 label: $localize`Run WebTorrent transcoding`,
105 handler: videos => this.runTranscoding(videos, 'webtorrent'),
106 isDisplayed: videos => videos.every(v => v.canRunTranscoding(this.authUser)),
110 label: $localize`Delete HLS files`,
111 handler: videos => this.removeVideoFiles(videos, 'hls'),
112 isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
116 label: $localize`Delete WebTorrent files`,
117 handler: videos => this.removeVideoFiles(videos, 'webtorrent'),
118 isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
126 return 'VideoListComponent'
129 isInSelectionMode () {
130 return this.selectedVideos.length !== 0
133 getPrivacyBadgeClass (video: Video) {
134 if (video.privacy.id === VideoPrivacy.PUBLIC) return 'badge-green'
136 return 'badge-yellow'
139 isUnpublished (video: Video) {
140 return video.state.id !== VideoState.LIVE_ENDED && video.state.id !== VideoState.PUBLISHED
143 isAccountBlocked (video: Video) {
144 return video.blockedOwner
147 isServerBlocked (video: Video) {
148 return video.blockedServer
151 isVideoBlocked (video: Video) {
152 return video.blacklisted
155 isImport (video: Video) {
156 return video.state.id === VideoState.TO_IMPORT
159 isHLS (video: Video) {
160 const p = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
163 return p.files.length !== 0
166 isWebTorrent (video: Video) {
167 return video.files.length !== 0
170 hasObjectStorage (video: Video) {
171 if (!video.isLocal) return false
173 const files = getAllFiles(video)
175 return files.some(f => !f.fileUrl.startsWith(window.location.origin))
178 canRemoveOneFile (video: Video) {
179 return video.canRemoveOneFile(this.authUser)
182 getFilesSize (video: Video) {
183 let files = video.files
185 if (this.isHLS(video)) {
186 files = files.concat(video.streamingPlaylists[0].files)
189 return files.reduce((p, f) => p += f.size, 0)
193 this.selectedVideos = []
197 this.videoAdminService.getAdminVideos({
198 pagination: this.pagination,
201 }).pipe(finalize(() => this.loading = false))
203 next: resultList => {
204 this.videos = resultList.data
205 this.totalRecords = resultList.total
208 error: err => this.notifier.error(err.message)
212 async removeVideoFile (video: Video, file: VideoFile, type: 'hls' | 'webtorrent') {
213 const message = $localize`Are you sure you want to delete this ${file.resolution.label} file?`
214 const res = await this.confirmService.confirm(message, $localize`Delete file`)
215 if (res === false) return
217 this.videoService.removeFile(video.uuid, file.id, type)
220 this.notifier.success($localize`File removed.`)
224 error: err => this.notifier.error(err.message)
228 private async removeVideos (videos: Video[]) {
229 const message = prepareIcu($localize`Are you sure you want to delete {count, plural, =1 {this video} other {these {count} videos}}?`)(
230 { count: videos.length },
231 $localize`Are you sure you want to delete these ${videos.length} videos?`
234 const res = await this.confirmService.confirm(message, $localize`Delete`)
235 if (res === false) return
237 this.videoService.removeVideo(videos.map(v => v.id))
240 this.notifier.success(
241 prepareIcu($localize`Deleted {count, plural, =1 {1 video} other {{count} videos}}.`)(
242 { count: videos.length },
243 $localize`Deleted ${videos.length} videos.`
250 error: err => this.notifier.error(err.message)
254 private unblockVideos (videos: Video[]) {
255 this.videoBlockService.unblockVideo(videos.map(v => v.id))
258 this.notifier.success(
259 prepareIcu($localize`Unblocked {count, plural, =1 {1 video} other {{count} videos}}.`)(
260 { count: videos.length },
261 $localize`Unblocked ${videos.length} videos.`
268 error: err => this.notifier.error(err.message)
272 private async removeVideoFiles (videos: Video[], type: 'hls' | 'webtorrent') {
275 if (type === 'hls') {
276 // eslint-disable-next-line max-len
277 message = prepareIcu($localize`Are you sure you want to delete {count, plural, =1 {1 HLS streaming playlist} other {{count} HLS streaming playlists}}?`)(
278 { count: videos.length },
279 $localize`Are you sure you want to delete ${videos.length} HLS streaming playlists?`
282 // eslint-disable-next-line max-len
283 message = prepareIcu($localize`Are you sure you want to delete WebTorrent files of {count, plural, =1 {1 video} other {{count} videos}}?`)(
284 { count: videos.length },
285 $localize`Are you sure you want to delete WebTorrent files of ${videos.length} videos?`
289 const res = await this.confirmService.confirm(message, $localize`Delete`)
290 if (res === false) return
292 this.videoService.removeVideoFiles(videos.map(v => v.id), type)
295 this.notifier.success($localize`Files were removed.`)
299 error: err => this.notifier.error(err.message)
303 private runTranscoding (videos: Video[], type: 'hls' | 'webtorrent') {
304 this.videoService.runTranscoding(videos.map(v => v.id), type)
307 this.notifier.success($localize`Transcoding jobs created.`)
312 error: err => this.notifier.error(err.message)