X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fpeertube-redundancy.ts;h=5bc80ddb9df3ca9b82effcfaacaac18a4a47c14a;hb=1e4d2cb5aef11898585fae4053da4ebd0a69b480;hp=a71f48104e6510f9dece3e98a44583d2011efe9a;hpb=26fcf2efebc681104d8e181da42b9ec112a8d28e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index a71f48104..5bc80ddb9 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts @@ -1,3 +1,5 @@ +// eslint-disable @typescript-eslint/no-unnecessary-type-assertion + import { registerTSPaths } from '../helpers/register-ts-paths' registerTSPaths() @@ -5,12 +7,15 @@ import * as program from 'commander' import { getAdminTokenOrDie, getServerCredentials } from './cli' import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' import { addVideoRedundancy, listVideoRedundancies, removeVideoRedundancy } from '@shared/extra-utils/server/redundancy' +import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' import validator from 'validator' -import bytes = require('bytes') import * as CliTable3 from 'cli-table3' -import { parse } from 'url' +import { URL } from 'url' import { uniq } from 'lodash' +import bytes = require('bytes') +import commander = require('commander') + program .name('plugins') .usage('[command] [options]') @@ -38,7 +43,7 @@ program .option('-U, --username ', 'Username') .option('-p, --password ', 'Password') .option('-v, --video ', 'Video id to duplicate') - .action((options) => addRedundancyCLI(options)) + .action((options, command) => addRedundancyCLI(options, command)) program .command('remove') @@ -47,7 +52,7 @@ program .option('-U, --username ', 'Username') .option('-p, --password ', 'Password') .option('-v, --video ', 'Video id to remove from redundancies') - .action((options) => removeRedundancyCLI(options)) + .action((options, command) => removeRedundancyCLI(options, command)) if (!process.argv.slice(2).length) { program.outputHelp() @@ -65,7 +70,7 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const table = new CliTable3({ head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ] - }) as CliTable3.HorizontalTable + }) as any for (const redundancy of redundancies) { const webtorrentFiles = redundancy.redundancies.files @@ -82,7 +87,7 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { const instances = uniq( webtorrentFiles.concat(streamingPlaylists) .map(r => r.fileUrl) - .map(u => parse(u).host) + .map(u => new URL(u).host) ) table.push([ @@ -100,13 +105,13 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { process.exit(0) } -async function addRedundancyCLI (options: { videoId: number }) { - const { url, username, password } = await getServerCredentials(program) +async function addRedundancyCLI (options: { video: number }, command: commander.CommanderStatic) { + const { url, username, password } = await getServerCredentials(command) const accessToken = await getAdminTokenOrDie(url, username, password) - if (!options[ 'video' ] || validator.isInt('' + options[ 'video' ]) === false) { + if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to duplicate and it should be a number.\n') - program.outputHelp() + command.outputHelp() process.exit(-1) } @@ -114,16 +119,16 @@ async function addRedundancyCLI (options: { videoId: number }) { await addVideoRedundancy({ url, accessToken, - videoId: options[ 'video' ] + videoId: options.video }) console.log('Video will be duplicated by your instance!') process.exit(0) } catch (err) { - if (err.message.includes(409)) { + if (err.message.includes(HttpStatusCode.CONFLICT_409)) { console.error('This video is already duplicated by your instance.') - } else if (err.message.includes(404)) { + } else if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) { console.error('This video id does not exist.') } else { console.error(err) @@ -133,17 +138,17 @@ async function addRedundancyCLI (options: { videoId: number }) { } } -async function removeRedundancyCLI (options: { videoId: number }) { - const { url, username, password } = await getServerCredentials(program) +async function removeRedundancyCLI (options: { video: number }, command: commander.CommanderStatic) { + const { url, username, password } = await getServerCredentials(command) const accessToken = await getAdminTokenOrDie(url, username, password) - if (!options[ 'video' ] || validator.isInt('' + options[ 'video' ]) === false) { + if (!options.video || validator.isInt('' + options.video) === false) { console.error('You need to specify the video id to remove from your redundancies.\n') - program.outputHelp() + command.outputHelp() process.exit(-1) } - const videoId = parseInt(options[ 'video' ] + '', 10) + const videoId = parseInt(options.video + '', 10) let redundancies = await listVideoRedundanciesData(url, accessToken, 'my-videos') let videoRedundancy = redundancies.find(r => videoId === r.id)