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 const options = command.opts()
27 getServerCredentials(command)
28 .then(({ url, username, password }) => {
29 if (!options.videoName || !options.file) {
30 if (!options.videoName) console.error('--video-name is required.')
31 if (!options.file) console.error('--file is required.')
36 if (isAbsolute(options.file) === false) {
37 console.error('File path should be absolute.')
41 run(url, username, password).catch(err => {
46 .catch(err => console.error(err))
48 async function run (url: string, username: string, password: string) {
49 const accessToken = await getAccessToken(url, username, password)
51 await access(options.file, constants.F_OK)
53 console.log('Uploading %s video...', options.videoName)
55 const videoAttributes = await buildVideoAttributesFromCommander(url, program)
57 Object.assign(videoAttributes, {
58 fixture: options.file,
59 thumbnailfile: options.thumbnail,
60 previewfile: options.preview
64 await uploadVideo(url, accessToken, videoAttributes)
65 console.log(`Video ${options.videoName} uploaded.`)
68 console.error(require('util').inspect(err))
73 // ----------------------------------------------------------------------------