]>
Commit | Line | Data |
---|---|---|
1 | import { registerTSPaths } from '../helpers/register-ts-paths' | |
2 | registerTSPaths() | |
3 | ||
4 | import 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, VideoRedundanciesTarget } from '@shared/models' | |
10 | import { assignToken, buildServer, getServerCredentials } from './cli' | |
11 | ||
12 | import bytes = require('bytes') | |
13 | ||
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') | |
41 | .action((options, command) => addRedundancyCLI(options, command)) | |
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') | |
50 | .action((options, command) => removeRedundancyCLI(options, command)) | |
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) | |
62 | const server = buildServer(url) | |
63 | await assignToken(server, username, password) | |
64 | ||
65 | const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target }) | |
66 | ||
67 | const table = new CliTable3({ | |
68 | head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] | |
69 | }) as any | |
70 | ||
71 | for (const redundancy of data) { | |
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) | |
86 | .map(u => new URL(u).host) | |
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 | ||
104 | async function addRedundancyCLI (options: { video: number }, command: Command) { | |
105 | const { url, username, password } = await getServerCredentials(command) | |
106 | const server = buildServer(url) | |
107 | await assignToken(server, username, password) | |
108 | ||
109 | if (!options.video || validator.isInt('' + options.video) === false) { | |
110 | console.error('You need to specify the video id to duplicate and it should be a number.\n') | |
111 | command.outputHelp() | |
112 | process.exit(-1) | |
113 | } | |
114 | ||
115 | try { | |
116 | await server.redundancy.addVideo({ videoId: options.video }) | |
117 | ||
118 | console.log('Video will be duplicated by your instance!') | |
119 | ||
120 | process.exit(0) | |
121 | } catch (err) { | |
122 | if (err.message.includes(HttpStatusCode.CONFLICT_409)) { | |
123 | console.error('This video is already duplicated by your instance.') | |
124 | } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) { | |
125 | console.error('This video id does not exist.') | |
126 | } else { | |
127 | console.error(err) | |
128 | } | |
129 | ||
130 | process.exit(-1) | |
131 | } | |
132 | } | |
133 | ||
134 | async function removeRedundancyCLI (options: { video: number }, command: Command) { | |
135 | const { url, username, password } = await getServerCredentials(command) | |
136 | const server = buildServer(url) | |
137 | await assignToken(server, username, password) | |
138 | ||
139 | if (!options.video || validator.isInt('' + options.video) === false) { | |
140 | console.error('You need to specify the video id to remove from your redundancies.\n') | |
141 | command.outputHelp() | |
142 | process.exit(-1) | |
143 | } | |
144 | ||
145 | const videoId = parseInt(options.video + '', 10) | |
146 | ||
147 | const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' }) | |
148 | let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id) | |
149 | ||
150 | if (!videoRedundancy) { | |
151 | const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' }) | |
152 | videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id) | |
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) { | |
166 | await server.redundancy.removeVideo({ redundancyId: id }) | |
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 | } |