1 import { registerTSPaths } from '../helpers/register-ts-paths'
4 import * as program from 'commander'
5 import { getAdminTokenOrDie, getServerCredentials } from './cli'
6 import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
7 import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy'
8 import validator from 'validator'
9 import bytes = require('bytes')
10 import * as CliTable3 from 'cli-table3'
11 import { parse } from 'url'
12 import { uniq } from 'lodash'
16 .usage('[command] [options]')
19 .command('list-remote-redundancies')
20 .description('List remote redundancies on your videos')
21 .option('-u, --url <url>', 'Server url')
22 .option('-U, --username <username>', 'Username')
23 .option('-p, --password <token>', 'Password')
24 .action(() => listRedundanciesCLI('my-videos'))
27 .command('list-my-redundancies')
28 .description('List your redundancies of remote videos')
29 .option('-u, --url <url>', 'Server url')
30 .option('-U, --username <username>', 'Username')
31 .option('-p, --password <token>', 'Password')
32 .action(() => listRedundanciesCLI('remote-videos'))
36 .description('Duplicate a video in your redundancy system')
37 .option('-u, --url <url>', 'Server url')
38 .option('-U, --username <username>', 'Username')
39 .option('-p, --password <token>', 'Password')
40 .option('-v, --video <videoId>', 'Video id to duplicate')
41 .action((options) => addRedundancyCLI(options))
45 .description('Remove a video from your redundancies')
46 .option('-u, --url <url>', 'Server url')
47 .option('-U, --username <username>', 'Username')
48 .option('-p, --password <token>', 'Password')
49 .option('-v, --video <videoId>', 'Video id to remove from redundancies')
50 .action((options) => removeRedundancyCLI(options))
52 if (!process.argv.slice(2).length) {
56 program.parse(process.argv)
58 // ----------------------------------------------------------------------------
60 async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
61 const { url, username, password } = await getServerCredentials(program)
62 const accessToken = await getAdminTokenOrDie(url, username, password)
64 const redundancies = await listVideoRedundanciesData(url, accessToken, target)
66 const table = new CliTable3({
67 head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
68 }) as CliTable3.HorizontalTable
70 for (const redundancy of redundancies) {
71 const webtorrentFiles = redundancy.redundancies.files
72 const streamingPlaylists = redundancy.redundancies.streamingPlaylists
75 if (target === 'remote-videos') {
76 const tmp = webtorrentFiles.concat(streamingPlaylists)
77 .reduce((a, b) => a + b.size, 0)
79 totalSize = bytes(tmp)
82 const instances = uniq(
83 webtorrentFiles.concat(streamingPlaylists)
85 .map(u => parse(u).host)
89 redundancy.id.toString(),
92 webtorrentFiles.length,
93 streamingPlaylists.length,
99 console.log(table.toString())
103 async function addRedundancyCLI (options: { videoId: number }) {
104 const { url, username, password } = await getServerCredentials(program)
105 const accessToken = await getAdminTokenOrDie(url, username, password)
107 if (!options[ 'video' ] || validator.isInt('' + options[ 'video' ]) === false) {
108 console.error('You need to specify the video id to duplicate and it should be a number.\n')
114 await addVideoRedundancy({
117 videoId: options[ 'video' ]
120 console.log('Video will be duplicated by your instance!')
124 if (err.message.includes(409)) {
125 console.error('This video is already duplicated by your instance.')
126 } else if (err.message.includes(404)) {
127 console.error('This video id does not exist.')
136 async function removeRedundancyCLI (options: { videoId: number }) {
137 const { url, username, password } = await getServerCredentials(program)
138 const accessToken = await getAdminTokenOrDie(url, username, password)
140 if (!options[ 'video' ] || validator.isInt('' + options[ 'video' ]) === false) {
141 console.error('You need to specify the video id to remove from your redundancies.\n')
146 const videoId = parseInt(options[ 'video' ] + '', 10)
148 let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos')
149 let videoRedundancy = redundancies.find(r => videoId === r.id)
151 if (!videoRedundancy) {
152 redundancies = await listVideoRedundanciesData(url, accessToken, 'remote-videos')
153 videoRedundancy = redundancies.find(r => videoId === r.id)
156 if (!videoRedundancy) {
157 console.error('Video redundancy not found.')
162 const ids = videoRedundancy.redundancies.files
163 .concat(videoRedundancy.redundancies.streamingPlaylists)
166 for (const id of ids) {
167 await removeVideoRedundancy({
174 console.log('Video redundancy removed!')
183 async function listVideoRedundanciesData (url: string, accessToken: string, target: VideoRedundanciesTarget) {
184 const res = await listVideoRedundancies({
193 return res.body.data as VideoRedundancy[]