]>
Commit | Line | Data |
---|---|---|
26fcf2ef C |
1 | import { registerTSPaths } from '../helpers/register-ts-paths' |
2 | registerTSPaths() | |
3 | ||
41fb13c3 | 4 | import CliTable3 from 'cli-table3' |
078f17e6 | 5 | import { Command, program } from 'commander' |
26fcf2ef | 6 | import { uniq } from 'lodash' |
078f17e6 C |
7 | import { URL } from 'url' |
8 | import validator from 'validator' | |
4c7e60bc | 9 | import { HttpStatusCode, VideoRedundanciesTarget } from '@shared/models' |
d0a0fa42 | 10 | import { assignToken, buildServer, getServerCredentials } from './cli' |
26fcf2ef | 11 | |
a1587156 C |
12 | import bytes = require('bytes') |
13 | ||
26fcf2ef C |
14 | program |
15 | .name('plugins') | |
16 | .usage('[command] [options]') | |
17 | ||
18 | program | |
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')) | |
25 | ||
26 | program | |
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')) | |
33 | ||
34 | program | |
35 | .command('add') | |
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') | |
ba5a8d89 | 41 | .action((options, command) => addRedundancyCLI(options, command)) |
26fcf2ef C |
42 | |
43 | program | |
44 | .command('remove') | |
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') | |
ba5a8d89 | 50 | .action((options, command) => removeRedundancyCLI(options, command)) |
26fcf2ef C |
51 | |
52 | if (!process.argv.slice(2).length) { | |
53 | program.outputHelp() | |
54 | } | |
55 | ||
56 | program.parse(process.argv) | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | async function listRedundanciesCLI (target: VideoRedundanciesTarget) { | |
61 | const { url, username, password } = await getServerCredentials(program) | |
d0a0fa42 C |
62 | const server = buildServer(url) |
63 | await assignToken(server, username, password) | |
26fcf2ef | 64 | |
89d241a7 | 65 | const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target }) |
26fcf2ef C |
66 | |
67 | const table = new CliTable3({ | |
68 | head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] | |
a1587156 | 69 | }) as any |
26fcf2ef | 70 | |
078f17e6 | 71 | for (const redundancy of data) { |
26fcf2ef C |
72 | const webtorrentFiles = redundancy.redundancies.files |
73 | const streamingPlaylists = redundancy.redundancies.streamingPlaylists | |
74 | ||
75 | let totalSize = '' | |
76 | if (target === 'remote-videos') { | |
77 | const tmp = webtorrentFiles.concat(streamingPlaylists) | |
78 | .reduce((a, b) => a + b.size, 0) | |
79 | ||
80 | totalSize = bytes(tmp) | |
81 | } | |
82 | ||
83 | const instances = uniq( | |
84 | webtorrentFiles.concat(streamingPlaylists) | |
85 | .map(r => r.fileUrl) | |
a1587156 | 86 | .map(u => new URL(u).host) |
26fcf2ef C |
87 | ) |
88 | ||
89 | table.push([ | |
90 | redundancy.id.toString(), | |
91 | redundancy.name, | |
92 | redundancy.url, | |
93 | webtorrentFiles.length, | |
94 | streamingPlaylists.length, | |
95 | instances.join('\n'), | |
96 | totalSize | |
97 | ]) | |
98 | } | |
99 | ||
100 | console.log(table.toString()) | |
101 | process.exit(0) | |
102 | } | |
103 | ||
12152aa0 | 104 | async function addRedundancyCLI (options: { video: number }, command: Command) { |
ba5a8d89 | 105 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
106 | const server = buildServer(url) |
107 | await assignToken(server, username, password) | |
26fcf2ef | 108 | |
ba5a8d89 | 109 | if (!options.video || validator.isInt('' + options.video) === false) { |
26fcf2ef | 110 | console.error('You need to specify the video id to duplicate and it should be a number.\n') |
ba5a8d89 | 111 | command.outputHelp() |
26fcf2ef C |
112 | process.exit(-1) |
113 | } | |
114 | ||
115 | try { | |
89d241a7 | 116 | await server.redundancy.addVideo({ videoId: options.video }) |
26fcf2ef C |
117 | |
118 | console.log('Video will be duplicated by your instance!') | |
119 | ||
120 | process.exit(0) | |
121 | } catch (err) { | |
f2eb23cd | 122 | if (err.message.includes(HttpStatusCode.CONFLICT_409)) { |
26fcf2ef | 123 | console.error('This video is already duplicated by your instance.') |
f2eb23cd | 124 | } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) { |
26fcf2ef C |
125 | console.error('This video id does not exist.') |
126 | } else { | |
127 | console.error(err) | |
128 | } | |
129 | ||
130 | process.exit(-1) | |
131 | } | |
132 | } | |
133 | ||
12152aa0 | 134 | async function removeRedundancyCLI (options: { video: number }, command: Command) { |
ba5a8d89 | 135 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
136 | const server = buildServer(url) |
137 | await assignToken(server, username, password) | |
26fcf2ef | 138 | |
ba5a8d89 | 139 | if (!options.video || validator.isInt('' + options.video) === false) { |
26fcf2ef | 140 | console.error('You need to specify the video id to remove from your redundancies.\n') |
ba5a8d89 | 141 | command.outputHelp() |
26fcf2ef C |
142 | process.exit(-1) |
143 | } | |
144 | ||
ba5a8d89 | 145 | const videoId = parseInt(options.video + '', 10) |
26fcf2ef | 146 | |
89d241a7 | 147 | const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' }) |
078f17e6 | 148 | let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id) |
26fcf2ef C |
149 | |
150 | if (!videoRedundancy) { | |
89d241a7 | 151 | const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' }) |
078f17e6 | 152 | videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) |
26fcf2ef C |
153 | } |
154 | ||
155 | if (!videoRedundancy) { | |
156 | console.error('Video redundancy not found.') | |
157 | process.exit(-1) | |
158 | } | |
159 | ||
160 | try { | |
161 | const ids = videoRedundancy.redundancies.files | |
162 | .concat(videoRedundancy.redundancies.streamingPlaylists) | |
163 | .map(r => r.id) | |
164 | ||
165 | for (const id of ids) { | |
89d241a7 | 166 | await server.redundancy.removeVideo({ redundancyId: id }) |
26fcf2ef C |
167 | } |
168 | ||
169 | console.log('Video redundancy removed!') | |
170 | ||
171 | process.exit(0) | |
172 | } catch (err) { | |
173 | console.error(err) | |
174 | process.exit(-1) | |
175 | } | |
176 | } |