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