1 import CliTable3 from 'cli-table3'
2 import { Command, program } from 'commander'
3 import { uniq } from 'lodash'
4 import { URL } from 'url'
5 import validator from 'validator'
6 import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models'
7 import { assignToken, buildServer, getServerCredentials } from './cli'
9 import bytes = require('bytes')
13 .usage('[command] [options]')
16 .command('list-remote-redundancies')
17 .description('List remote redundancies on your videos')
18 .option('-u, --url <url>', 'Server url')
19 .option('-U, --username <username>', 'Username')
20 .option('-p, --password <token>', 'Password')
21 .action(() => listRedundanciesCLI('my-videos'))
24 .command('list-my-redundancies')
25 .description('List your redundancies of remote videos')
26 .option('-u, --url <url>', 'Server url')
27 .option('-U, --username <username>', 'Username')
28 .option('-p, --password <token>', 'Password')
29 .action(() => listRedundanciesCLI('remote-videos'))
33 .description('Duplicate a video in your redundancy system')
34 .option('-u, --url <url>', 'Server url')
35 .option('-U, --username <username>', 'Username')
36 .option('-p, --password <token>', 'Password')
37 .option('-v, --video <videoId>', 'Video id to duplicate')
38 .action((options, command) => addRedundancyCLI(options, command))
42 .description('Remove a video from your redundancies')
43 .option('-u, --url <url>', 'Server url')
44 .option('-U, --username <username>', 'Username')
45 .option('-p, --password <token>', 'Password')
46 .option('-v, --video <videoId>', 'Video id to remove from redundancies')
47 .action((options, command) => removeRedundancyCLI(options, command))
49 if (!process.argv.slice(2).length) {
53 program.parse(process.argv)
55 // ----------------------------------------------------------------------------
57 async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
58 const { url, username, password } = await getServerCredentials(program)
59 const server = buildServer(url)
60 await assignToken(server, username, password)
62 const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target })
64 const table = new CliTable3({
65 head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
68 for (const redundancy of data) {
69 const webtorrentFiles = redundancy.redundancies.files
70 const streamingPlaylists = redundancy.redundancies.streamingPlaylists
73 if (target === 'remote-videos') {
74 const tmp = webtorrentFiles.concat(streamingPlaylists)
75 .reduce((a, b) => a + b.size, 0)
77 totalSize = bytes(tmp)
80 const instances = uniq(
81 webtorrentFiles.concat(streamingPlaylists)
83 .map(u => new URL(u).host)
87 redundancy.id.toString(),
90 webtorrentFiles.length,
91 streamingPlaylists.length,
97 console.log(table.toString())
101 async function addRedundancyCLI (options: { video: number }, command: Command) {
102 const { url, username, password } = await getServerCredentials(command)
103 const server = buildServer(url)
104 await assignToken(server, username, password)
106 if (!options.video || validator.isInt('' + options.video) === false) {
107 console.error('You need to specify the video id to duplicate and it should be a number.\n')
113 await server.redundancy.addVideo({ videoId: options.video })
115 console.log('Video will be duplicated by your instance!')
119 if (err.message.includes(HttpStatusCode.CONFLICT_409)) {
120 console.error('This video is already duplicated by your instance.')
121 } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) {
122 console.error('This video id does not exist.')
131 async function removeRedundancyCLI (options: { video: number }, command: Command) {
132 const { url, username, password } = await getServerCredentials(command)
133 const server = buildServer(url)
134 await assignToken(server, username, password)
136 if (!options.video || validator.isInt('' + options.video) === false) {
137 console.error('You need to specify the video id to remove from your redundancies.\n')
142 const videoId = parseInt(options.video + '', 10)
144 const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
145 let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)
147 if (!videoRedundancy) {
148 const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
149 videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id)
152 if (!videoRedundancy) {
153 console.error('Video redundancy not found.')
158 const ids = videoRedundancy.redundancies.files
159 .concat(videoRedundancy.redundancies.streamingPlaylists)
162 for (const id of ids) {
163 await server.redundancy.removeVideo({ redundancyId: id })
166 console.log('Video redundancy removed!')