PeerTube/server/tools/upload.ts

102 lines
3.3 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'
2018-04-04 03:47:02 -05:00
import { VideoPrivacy } from '../../shared/models/videos'
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')
.option('-P, --privacy <privacy number>', 'Privacy')
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')
2018-04-23 07:39:52 -05:00
.option('-L, --language <language code>', 'Language ISO 639 code (fr or en...)')
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')
2018-02-15 11:40:24 -06:00
.option('-v, --preview <previewPath>', 'Preview 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-04-04 03:47:02 -05:00
if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
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']
) {
if (!program['url']) console.error('--url field is required.')
if (!program['username']) console.error('--username field is required.')
if (!program['password']) console.error('--password field is required.')
if (!program['videoName']) console.error('--video-name field is required.')
if (!program['file']) console.error('--file field is required.')
2018-02-12 05:48:58 -06:00
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'],
2018-02-15 11:40:24 -06:00
thumbnailfile: program['thumbnailPath'],
2018-03-05 02:53:16 -06:00
previewfile: program['previewPath'],
privacy: program['privacy'],
2018-03-05 02:53:16 -06:00
support: undefined
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
}