2017-09-07 17:58:09 +02:00
|
|
|
import * as program from 'commander'
|
2018-08-27 13:28:49 +02:00
|
|
|
import { access, constants } from 'fs-extra'
|
2017-09-07 17:58:09 +02:00
|
|
|
import { isAbsolute } from 'path'
|
2019-04-15 15:26:15 +02:00
|
|
|
import { getClient, login } from '../../shared/extra-utils'
|
|
|
|
import { uploadVideo } from '../../shared/extra-utils/'
|
2019-06-13 13:53:28 +02:00
|
|
|
import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2019-06-13 13:53:28 +02:00
|
|
|
let command = program
|
2018-09-13 14:27:44 +02:00
|
|
|
.name('upload')
|
2019-06-13 13:53:28 +02:00
|
|
|
|
|
|
|
command = buildCommonVideoOptions(command)
|
|
|
|
|
|
|
|
command
|
|
|
|
|
2017-09-07 17:58:09 +02:00
|
|
|
.option('-u, --url <url>', 'Server url')
|
2018-02-12 12:48:58 +01:00
|
|
|
.option('-U, --username <username>', 'Username')
|
|
|
|
.option('-p, --password <token>', 'Password')
|
2018-02-15 14:46:26 +01:00
|
|
|
.option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
|
2018-02-15 18:40:24 +01:00
|
|
|
.option('-v, --preview <previewPath>', 'Preview path')
|
2017-09-07 17:58:09 +02:00
|
|
|
.option('-f, --file <file>', 'Video absolute file path')
|
|
|
|
.parse(process.argv)
|
|
|
|
|
2019-06-13 11:09:38 +02:00
|
|
|
Promise.all([ getSettings(), getNetrc() ])
|
|
|
|
.then(([ settings, netrc ]) => {
|
|
|
|
const { url, username, password } = getRemoteObjectOrDie(program, settings, netrc)
|
2018-09-13 14:27:44 +02:00
|
|
|
|
2019-06-13 13:53:28 +02:00
|
|
|
if (!program[ 'videoName' ] || !program[ 'file' ]) {
|
2019-06-13 11:09:38 +02:00
|
|
|
if (!program[ 'videoName' ]) console.error('--video-name is required.')
|
|
|
|
if (!program[ 'file' ]) console.error('--file is required.')
|
2018-09-13 14:27:44 +02:00
|
|
|
|
2019-06-13 11:09:38 +02:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-09-13 14:27:44 +02:00
|
|
|
|
2019-06-13 11:09:38 +02:00
|
|
|
if (isAbsolute(program[ 'file' ]) === false) {
|
|
|
|
console.error('File path should be absolute.')
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-09-13 14:27:44 +02:00
|
|
|
|
2019-06-13 11:09:38 +02:00
|
|
|
run(url, username, password).catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
})
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2019-04-25 13:55:28 +02:00
|
|
|
async function run (url: string, username: string, password: string) {
|
2019-06-13 11:09:38 +02:00
|
|
|
const resClient = await getClient(url)
|
2018-02-12 12:48:58 +01:00
|
|
|
const client = {
|
2019-04-25 13:55:28 +02:00
|
|
|
id: resClient.body.client_id,
|
|
|
|
secret: resClient.body.client_secret
|
2018-02-12 12:48:58 +01:00
|
|
|
}
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2019-04-25 13:55:28 +02:00
|
|
|
const user = { username, password }
|
2018-02-12 12:48:58 +01:00
|
|
|
|
2018-09-26 15:16:48 +02:00
|
|
|
let accessToken: string
|
|
|
|
try {
|
2019-04-25 13:55:28 +02:00
|
|
|
const res = await login(url, client, user)
|
|
|
|
accessToken = res.body.access_token
|
2018-09-26 15:16:48 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error('Cannot authenticate. Please check your username/password.')
|
|
|
|
}
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2018-08-27 16:23:34 +02:00
|
|
|
await access(program[ 'file' ], constants.F_OK)
|
2018-02-12 12:48:58 +01:00
|
|
|
|
|
|
|
console.log('Uploading %s video...', program[ 'videoName' ])
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2019-06-13 13:53:28 +02:00
|
|
|
const defaultAttributes = {
|
|
|
|
tags: command[ 'tags' ],
|
|
|
|
description: command[ 'videoDescription' ]
|
|
|
|
}
|
|
|
|
const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes)
|
|
|
|
|
|
|
|
Object.assign(videoAttributes, {
|
2019-06-13 11:09:38 +02:00
|
|
|
fixture: program[ 'file' ],
|
|
|
|
thumbnailfile: program[ 'thumbnail' ],
|
2019-06-13 13:53:28 +02:00
|
|
|
previewfile: program[ 'preview' ]
|
|
|
|
})
|
2018-02-12 12:48:58 +01:00
|
|
|
|
2019-04-25 13:55:28 +02:00
|
|
|
try {
|
|
|
|
await uploadVideo(url, accessToken, videoAttributes)
|
2019-06-13 11:09:38 +02:00
|
|
|
console.log(`Video ${program[ 'videoName' ]} uploaded.`)
|
2019-04-25 13:55:28 +02:00
|
|
|
process.exit(0)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(require('util').inspect(err))
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-02-12 12:48:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|