2019-10-21 11:13:07 -04:00
|
|
|
import { registerTSPaths } from '../helpers/register-ts-paths'
|
|
|
|
registerTSPaths()
|
|
|
|
|
2017-09-07 11:58:09 -04:00
|
|
|
import * as program from 'commander'
|
2018-08-27 07:28:49 -04:00
|
|
|
import { access, constants } from 'fs-extra'
|
2017-09-07 11:58:09 -04:00
|
|
|
import { isAbsolute } from 'path'
|
2019-07-11 11:23:24 -04:00
|
|
|
import { getAccessToken } from '../../shared/extra-utils'
|
2019-04-15 09:26:15 -04:00
|
|
|
import { uploadVideo } from '../../shared/extra-utils/'
|
2019-07-11 11:23:24 -04:00
|
|
|
import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
|
2017-09-07 11:58:09 -04:00
|
|
|
|
2019-06-13 07:53:28 -04:00
|
|
|
let command = program
|
2018-09-13 08:27:44 -04:00
|
|
|
.name('upload')
|
2019-06-13 07:53:28 -04:00
|
|
|
|
|
|
|
command = buildCommonVideoOptions(command)
|
|
|
|
|
|
|
|
command
|
2017-09-07 11:58:09 -04:00
|
|
|
.option('-u, --url <url>', 'Server url')
|
2018-02-12 06:48:58 -05:00
|
|
|
.option('-U, --username <username>', 'Username')
|
|
|
|
.option('-p, --password <token>', 'Password')
|
2018-02-15 08:46:26 -05:00
|
|
|
.option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
|
2018-02-15 12:40:24 -05:00
|
|
|
.option('-v, --preview <previewPath>', 'Preview path')
|
2017-09-07 11:58:09 -04:00
|
|
|
.option('-f, --file <file>', 'Video absolute file path')
|
|
|
|
.parse(process.argv)
|
|
|
|
|
2019-07-11 11:23:24 -04:00
|
|
|
getServerCredentials(command)
|
|
|
|
.then(({ url, username, password }) => {
|
2020-01-31 10:56:52 -05:00
|
|
|
if (!program['videoName'] || !program['file']) {
|
|
|
|
if (!program['videoName']) console.error('--video-name is required.')
|
|
|
|
if (!program['file']) console.error('--file is required.')
|
2018-09-13 08:27:44 -04:00
|
|
|
|
2019-07-11 11:23:24 -04:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-09-13 08:27:44 -04:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
if (isAbsolute(program['file']) === false) {
|
2019-07-11 11:23:24 -04:00
|
|
|
console.error('File path should be absolute.')
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-09-13 08:27:44 -04:00
|
|
|
|
2019-07-11 11:23:24 -04:00
|
|
|
run(url, username, password).catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
})
|
2020-01-31 10:56:52 -05:00
|
|
|
.catch(err => console.error(err))
|
2017-09-07 11:58:09 -04:00
|
|
|
|
2019-04-25 07:55:28 -04:00
|
|
|
async function run (url: string, username: string, password: string) {
|
2019-07-11 11:23:24 -04:00
|
|
|
const accessToken = await getAccessToken(url, username, password)
|
2017-09-07 11:58:09 -04:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
await access(program['file'], constants.F_OK)
|
2018-02-12 06:48:58 -05:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
console.log('Uploading %s video...', program['videoName'])
|
2017-09-07 11:58:09 -04:00
|
|
|
|
2019-07-29 08:10:31 -04:00
|
|
|
const videoAttributes = await buildVideoAttributesFromCommander(url, program)
|
2019-06-13 07:53:28 -04:00
|
|
|
|
|
|
|
Object.assign(videoAttributes, {
|
2020-01-31 10:56:52 -05:00
|
|
|
fixture: program['file'],
|
|
|
|
thumbnailfile: program['thumbnail'],
|
|
|
|
previewfile: program['preview']
|
2019-06-13 07:53:28 -04:00
|
|
|
})
|
2018-02-12 06:48:58 -05:00
|
|
|
|
2019-04-25 07:55:28 -04:00
|
|
|
try {
|
|
|
|
await uploadVideo(url, accessToken, videoAttributes)
|
2020-01-31 10:56:52 -05:00
|
|
|
console.log(`Video ${program['videoName']} uploaded.`)
|
2019-04-25 07:55:28 -04:00
|
|
|
process.exit(0)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(require('util').inspect(err))
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-02-12 06:48:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|