1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getAccessToken } from '../../shared/extra-utils'
5 import { uploadVideo } from '../../shared/extra-utils/'
6 import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
11 command = buildCommonVideoOptions(command)
14 .option('-u, --url <url>', 'Server url')
15 .option('-U, --username <username>', 'Username')
16 .option('-p, --password <token>', 'Password')
17 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
18 .option('-v, --preview <previewPath>', 'Preview path')
19 .option('-f, --file <file>', 'Video absolute file path')
22 getServerCredentials(command)
23 .then(({ url, username, password }) => {
24 if (!program[ 'videoName' ] || !program[ 'file' ]) {
25 if (!program[ 'videoName' ]) console.error('--video-name is required.')
26 if (!program[ 'file' ]) console.error('--file is required.')
31 if (isAbsolute(program[ 'file' ]) === false) {
32 console.error('File path should be absolute.')
36 run(url, username, password).catch(err => {
42 async function run (url: string, username: string, password: string) {
43 const accessToken = await getAccessToken(url, username, password)
45 await access(program[ 'file' ], constants.F_OK)
47 console.log('Uploading %s video...', program[ 'videoName' ])
49 const defaultAttributes = {
50 tags: command[ 'tags' ],
51 description: command[ 'videoDescription' ]
53 const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes)
55 Object.assign(videoAttributes, {
56 fixture: program[ 'file' ],
57 thumbnailfile: program[ 'thumbnail' ],
58 previewfile: program[ 'preview' ]
62 await uploadVideo(url, accessToken, videoAttributes)
63 console.log(`Video ${program[ 'videoName' ]} uploaded.`)
66 console.error(require('util').inspect(err))
71 // ----------------------------------------------------------------------------