PeerTube/server/tools/upload.ts

91 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-09-07 10:58:09 -05:00
import * as program from 'commander'
import { access, constants } from 'fs'
import { isAbsolute } from 'path'
import { promisify } from 'util'
2018-02-12 05:48:58 -06:00
import { getClient, login } from '../tests/utils'
import { uploadVideo } from '../tests/utils/index'
2017-09-07 10:58:09 -05:00
const accessPromise = promisify(access)
program
.option('-u, --url <url>', 'Server url')
2018-02-12 05:48:58 -06:00
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.option('-n, --video-name <name>', 'Video name')
2017-09-07 10:58:09 -05:00
.option('-N, --nsfw', 'Video is Not Safe For Work')
.option('-c, --category <category number>', 'Category number')
2018-02-12 05:48:58 -06:00
.option('-m, --comments-enabled', 'Enable comments')
2017-09-07 10:58:09 -05:00
.option('-l, --licence <licence number>', 'Licence number')
.option('-L, --language <language number>', 'Language number')
2018-02-12 05:48:58 -06:00
.option('-d, --video-description <description>', 'Video description')
2017-09-07 10:58:09 -05:00
.option('-t, --tags <tags>', 'Video tags', list)
.option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
2017-09-07 10:58:09 -05:00
.option('-f, --file <file>', 'Video absolute file path')
.parse(process.argv)
if (!program['tags']) program['tags'] = []
if (!program['nsfw']) program['nsfw'] = false
2018-02-12 05:48:58 -06:00
if (!program['commentsEnabled']) program['commentsEnabled'] = false
2017-09-07 10:58:09 -05:00
if (
!program['url'] ||
2018-02-12 05:48:58 -06:00
!program['username'] ||
!program['password'] ||
!program['videoName'] ||
2017-09-07 10:58:09 -05:00
!program['file']
) {
2018-02-12 05:48:58 -06:00
console.error('Url, username, password, name and input file are required.')
process.exit(-1)
2017-09-07 10:58:09 -05:00
}
if (isAbsolute(program['file']) === false) {
2018-02-12 05:48:58 -06:00
console.error('File path should be absolute.')
process.exit(-1)
2017-09-07 10:58:09 -05:00
}
2018-02-12 05:48:58 -06:00
run().catch(err => console.error(err))
2017-09-07 10:58:09 -05:00
2018-02-12 05:48:58 -06:00
async function run () {
const res = await getClient(program[ 'url' ])
const client = {
id: res.body.client_id,
secret: res.body.client_secret
}
2017-09-07 10:58:09 -05:00
2018-02-12 05:48:58 -06:00
const user = {
username: program[ 'username' ],
password: program[ 'password' ]
}
const res2 = await login(program[ 'url' ], client, user)
const accessToken = res2.body.access_token
2017-09-07 10:58:09 -05:00
2018-02-12 05:48:58 -06:00
await accessPromise(program[ 'file' ], constants.F_OK)
console.log('Uploading %s video...', program[ 'videoName' ])
2017-09-07 10:58:09 -05:00
const videoAttributes = {
2018-02-12 05:48:58 -06:00
name: program['videoName'],
category: program['category'],
licence: program['licence'],
language: program['language'],
nsfw: program['nsfw'],
description: program['videoDescription'],
tags: program['tags'],
commentsEnabled: program['commentsEnabled'],
fixture: program['file'],
thumbnailfile: program['thumbnailPath']
2017-09-07 10:58:09 -05:00
}
2018-02-12 05:48:58 -06:00
await uploadVideo(program['url'], accessToken, videoAttributes)
console.log(`Video ${program['videoName']} uploaded.`)
process.exit(0)
}
// ----------------------------------------------------------------------------
function list (val) {
return val.split(',')
2017-09-07 10:58:09 -05:00
}