1 import { registerTSPaths } from '../helpers/register-ts-paths'
4 import * as CliTable3 from 'cli-table3'
5 import { Command, program } from 'commander'
6 import { uniq } from 'lodash'
7 import { URL } from 'url'
8 import validator from 'validator'
9 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
10 import { VideoRedundanciesTarget } from '@shared/models'
11 import { assignToken, buildServer, getServerCredentials } from './cli'
13 import bytes = require('bytes')
17 .usage('[command] [options]')
20 .command('list-remote-redundancies')
21 .description('List remote redundancies on your videos')
22 .option('-u, --url <url>', 'Server url')
23 .option('-U, --username <username>', 'Username')
24 .option('-p, --password <token>', 'Password')
25 .action(() => listRedundanciesCLI('my-videos'))
28 .command('list-my-redundancies')
29 .description('List your redundancies of remote videos')
30 .option('-u, --url <url>', 'Server url')
31 .option('-U, --username <username>', 'Username')
32 .option('-p, --password <token>', 'Password')
33 .action(() => listRedundanciesCLI('remote-videos'))
37 .description('Duplicate a video in your redundancy system')
38 .option('-u, --url <url>', 'Server url')
39 .option('-U, --username <username>', 'Username')
40 .option('-p, --password <token>', 'Password')
41 .option('-v, --video <videoId>', 'Video id to duplicate')
42 .action((options, command) => addRedundancyCLI(options, command))
46 .description('Remove a video from your redundancies')
47 .option('-u, --url <url>', 'Server url')
48 .option('-U, --username <username>', 'Username')
49 .option('-p, --password <token>', 'Password')
50 .option('-v, --video <videoId>', 'Video id to remove from redundancies')
51 .action((options, command) => removeRedundancyCLI(options, command))
53 if (!process.argv.slice(2).length) {
57 program.parse(process.argv)
59 // ----------------------------------------------------------------------------
61 async function listRedundanciesCLI (target: VideoRedundanciesTarget) {
62 const { url, username, password } = await getServerCredentials(program)
63 const server = buildServer(url)
64 await assignToken(server, username, password)
66 const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target })
68 const table = new CliTable3({
69 head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
72 for (const redundancy of data) {
73 const webtorrentFiles = redundancy.redundancies.files
74 const streamingPlaylists = redundancy.redundancies.streamingPlaylists
77 if (target === 'remote-videos') {
78 const tmp = webtorrentFiles.concat(streamingPlaylists)
79 .reduce((a, b) => a + b.size, 0)
81 totalSize = bytes(tmp)
84 const instances = uniq(
85 webtorrentFiles.concat(streamingPlaylists)
87 .map(u => new URL(u).host)
91 redundancy.id.toString(),
94 webtorrentFiles.length,
95 streamingPlaylists.length,
101 console.log(table.toString())
105 async function addRedundancyCLI (options: { video: number }, command: Command) {
106 const { url, username, password } = await getServerCredentials(command)
107 const server = buildServer(url)
108 await assignToken(server, username, password)
110 if (!options.video || validator.isInt('' + options.video) === false) {
111 console.error('You need to specify the video id to duplicate and it should be a number.\n')
117 await server.redundancy.addVideo({ videoId: options.video })
119 console.log('Video will be duplicated by your instance!')
123 if (err.message.includes(HttpStatusCode.CONFLICT_409)) {
124 console.error('This video is already duplicated by your instance.')
125 } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) {
126 console.error('This video id does not exist.')
135 async function removeRedundancyCLI (options: { video: number }, command: Command) {
136 const { url, username, password } = await getServerCredentials(command)
137 const server = buildServer(url)
138 await assignToken(server, 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 const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
149 let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)
151 if (!videoRedundancy) {
152 const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
153 videoRedundancy = remoteVideoRedundancies.data.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 server.redundancy.removeVideo({ redundancyId: id })
170 console.log('Video redundancy removed!')