1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
3 import { registerTSPaths } from '../helpers/register-ts-paths'
6 import * as program from 'commander'
7 import { getAdminTokenOrDie, getServerCredentials } from './cli'
8 import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
9 import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy'
10 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
11 import validator from 'validator'
12 import * as CliTable3 from 'cli-table3'
13 import { URL } from 'url'
14 import { uniq } from 'lodash'
16 import bytes = require('bytes')
17 import commander = require('commander')
21 .usage('[command] [options]')
24 .command('list-remote-redundancies')
25 .description('List remote redundancies on your videos')
26 .option('-u, --url <url>', 'Server url')
27 .option('-U, --username <username>', 'Username')
28 .option('-p, --password <token>', 'Password')
29 .action(() => listRedundanciesCLI('my-videos'))
32 .command('list-my-redundancies')
33 .description('List your redundancies of remote videos')
34 .option('-u, --url <url>', 'Server url')
35 .option('-U, --username <username>', 'Username')
36 .option('-p, --password <token>', 'Password')
37 .action(() => listRedundanciesCLI('remote-videos'))
41 .description('Duplicate a video in your redundancy system')
42 .option('-u, --url <url>', 'Server url')
43 .option('-U, --username <username>', 'Username')
44 .option('-p, --password <token>', 'Password')
45 .option('-v, --video <videoId>', 'Video id to duplicate')
46 .action((options, command) => addRedundancyCLI(options, command))
50 .description('Remove a video from your redundancies')
51 .option('-u, --url <url>', 'Server url')
52 .option('-U, --username <username>', 'Username')
53 .option('-p, --password <token>', 'Password')
54 .option('-v, --video <videoId>', 'Video id to remove from redundancies')
55 .action((options, command) => removeRedundancyCLI(options, command))
57 if (!process.argv.slice(2).length) {
61 program.parse(process.argv)
63 // ----------------------------------------------------------------------------
65 async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
66 const { url, username, password } = await getServerCredentials(program)
67 const accessToken = await getAdminTokenOrDie(url, username, password)
69 const redundancies = await listVideoRedundanciesData(url, accessToken, target)
71 const table = new CliTable3({
72 head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
75 for (const redundancy of redundancies) {
76 const webtorrentFiles = redundancy.redundancies.files
77 const streamingPlaylists = redundancy.redundancies.streamingPlaylists
80 if (target === 'remote-videos') {
81 const tmp = webtorrentFiles.concat(streamingPlaylists)
82 .reduce((a, b) => a + b.size, 0)
84 totalSize = bytes(tmp)
87 const instances = uniq(
88 webtorrentFiles.concat(streamingPlaylists)
90 .map(u => new URL(u).host)
94 redundancy.id.toString(),
97 webtorrentFiles.length,
98 streamingPlaylists.length,
104 console.log(table.toString())
108 async function addRedundancyCLI (options: { video: number }, command: commander.CommanderStatic) {
109 const { url, username, password } = await getServerCredentials(command)
110 const accessToken = await getAdminTokenOrDie(url, username, password)
112 if (!options.video || validator.isInt('' + options.video) === false) {
113 console.error('You need to specify the video id to duplicate and it should be a number.\n')
119 await addVideoRedundancy({
122 videoId: options.video
125 console.log('Video will be duplicated by your instance!')
129 if (err.message.includes(HttpStatusCode.CONFLICT_409)) {
130 console.error('This video is already duplicated by your instance.')
131 } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) {
132 console.error('This video id does not exist.')
141 async function removeRedundancyCLI (options: { video: number }, command: commander.CommanderStatic) {
142 const { url, username, password } = await getServerCredentials(command)
143 const accessToken = await getAdminTokenOrDie(url, username, password)
145 if (!options.video || validator.isInt('' + options.video) === false) {
146 console.error('You need to specify the video id to remove from your redundancies.\n')
151 const videoId = parseInt(options.video + '', 10)
153 let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos')
154 let videoRedundancy = redundancies.find(r => videoId === r.id)
156 if (!videoRedundancy) {
157 redundancies = await listVideoRedundanciesData(url, accessToken, 'remote-videos')
158 videoRedundancy = redundancies.find(r => videoId === r.id)
161 if (!videoRedundancy) {
162 console.error('Video redundancy not found.')
167 const ids = videoRedundancy.redundancies.files
168 .concat(videoRedundancy.redundancies.streamingPlaylists)
171 for (const id of ids) {
172 await removeVideoRedundancy({
179 console.log('Video redundancy removed!')
188 async function listVideoRedundanciesData (url: string, accessToken: string, target: VideoRedundanciesTarget) {
189 const res = await listVideoRedundancies({
198 return res.body.data as VideoRedundancy[]