1 import { registerTSPaths } from '../helpers/register-ts-paths'
4 import * as program from 'commander'
5 import { access, constants } from 'fs-extra'
6 import { isAbsolute } from 'path'
7 import { getAccessToken } from '../../shared/extra-utils'
8 import { uploadVideo } from '../../shared/extra-utils/'
9 import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
14 command = buildCommonVideoOptions(command)
17 .option('-u, --url <url>', 'Server url')
18 .option('-U, --username <username>', 'Username')
19 .option('-p, --password <token>', 'Password')
20 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
21 .option('-v, --preview <previewPath>', 'Preview path')
22 .option('-f, --file <file>', 'Video absolute file path')
25 getServerCredentials(command)
26 .then(({ url, username, password }) => {
27 if (!program[ 'videoName' ] || !program[ 'file' ]) {
28 if (!program[ 'videoName' ]) console.error('--video-name is required.')
29 if (!program[ 'file' ]) console.error('--file is required.')
34 if (isAbsolute(program[ 'file' ]) === false) {
35 console.error('File path should be absolute.')
39 run(url, username, password).catch(err => {
45 async function run (url: string, username: string, password: string) {
46 const accessToken = await getAccessToken(url, username, password)
48 await access(program[ 'file' ], constants.F_OK)
50 console.log('Uploading %s video...', program[ 'videoName' ])
52 const videoAttributes = await buildVideoAttributesFromCommander(url, program)
54 Object.assign(videoAttributes, {
55 fixture: program[ 'file' ],
56 thumbnailfile: program[ 'thumbnail' ],
57 previewfile: program[ 'preview' ]
61 await uploadVideo(url, accessToken, videoAttributes)
62 console.log(`Video ${program[ 'videoName' ]} uploaded.`)
65 console.error(require('util').inspect(err))
70 // ----------------------------------------------------------------------------