PeerTube/server/tools/peertube-upload.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

import { registerTSPaths } from '../helpers/register-ts-paths'
registerTSPaths()
2017-09-07 17:58:09 +02:00
import * as program from 'commander'
import { access, constants } from 'fs-extra'
2017-09-07 17:58:09 +02:00
import { isAbsolute } from 'path'
import { getAccessToken } from '../../shared/extra-utils'
import { uploadVideo } from '../../shared/extra-utils/'
import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
2017-09-07 17:58:09 +02:00
let command = program
.name('upload')
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')
.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)
getServerCredentials(command)
.then(({ url, username, password }) => {
if (!program[ 'videoName' ] || !program[ 'file' ]) {
if (!program[ 'videoName' ]) console.error('--video-name is required.')
if (!program[ 'file' ]) console.error('--file is required.')
process.exit(-1)
}
if (isAbsolute(program[ 'file' ]) === false) {
console.error('File path should be absolute.')
process.exit(-1)
}
run(url, username, password).catch(err => {
console.error(err)
process.exit(-1)
})
})
2017-09-07 17:58:09 +02:00
async function run (url: string, username: string, password: string) {
const accessToken = await getAccessToken(url, 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-07-29 14:10:31 +02:00
const videoAttributes = await buildVideoAttributesFromCommander(url, program)
Object.assign(videoAttributes, {
2019-06-13 11:09:38 +02:00
fixture: program[ 'file' ],
thumbnailfile: program[ 'thumbnail' ],
previewfile: program[ 'preview' ]
})
2018-02-12 12:48:58 +01:00
try {
await uploadVideo(url, accessToken, videoAttributes)
2019-06-13 11:09:38 +02:00
console.log(`Video ${program[ 'videoName' ]} uploaded.`)
process.exit(0)
} catch (err) {
console.error(require('util').inspect(err))
process.exit(-1)
}
2018-02-12 12:48:58 +01:00
}
// ----------------------------------------------------------------------------