1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getClient, login } from '../../shared/extra-utils'
5 import { uploadVideo } from '../../shared/extra-utils/'
6 import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
11 command = buildCommonVideoOptions(command)
15 .option('-u, --url <url>', 'Server url')
16 .option('-U, --username <username>', 'Username')
17 .option('-p, --password <token>', 'Password')
18 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
19 .option('-v, --preview <previewPath>', 'Preview path')
20 .option('-f, --file <file>', 'Video absolute file path')
23 Promise.all([ getSettings(), getNetrc() ])
24 .then(([ settings, netrc ]) => {
25 const { url, username, password } = getRemoteObjectOrDie(program, settings, netrc)
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 resClient = await getClient(url)
48 id: resClient.body.client_id,
49 secret: resClient.body.client_secret
52 const user = { username, password }
54 let accessToken: string
56 const res = await login(url, client, user)
57 accessToken = res.body.access_token
59 throw new Error('Cannot authenticate. Please check your username/password.')
62 await access(program[ 'file' ], constants.F_OK)
64 console.log('Uploading %s video...', program[ 'videoName' ])
66 const defaultAttributes = {
67 tags: command[ 'tags' ],
68 description: command[ 'videoDescription' ]
70 const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes)
72 Object.assign(videoAttributes, {
73 fixture: program[ 'file' ],
74 thumbnailfile: program[ 'thumbnail' ],
75 previewfile: program[ 'preview' ]
79 await uploadVideo(url, accessToken, videoAttributes)
80 console.log(`Video ${program[ 'videoName' ]} uploaded.`)
83 console.error(require('util').inspect(err))
88 // ----------------------------------------------------------------------------