1
0
Fork 0
peertube/server/tools/upload.ts

94 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-09-07 11:58:09 -04:00
import * as program from 'commander'
import { access, constants } from 'fs'
import { isAbsolute } from 'path'
import { promisify } from 'util'
2018-02-12 06:48:58 -05:00
import { getClient, login } from '../tests/utils'
import { uploadVideo } from '../tests/utils/index'
2017-09-07 11:58:09 -04:00
const accessPromise = promisify(access)
program
.option('-u, --url <url>', 'Server url')
2018-02-12 06:48:58 -05:00
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.option('-n, --video-name <name>', 'Video name')
2017-09-07 11:58:09 -04:00
.option('-N, --nsfw', 'Video is Not Safe For Work')
.option('-c, --category <category number>', 'Category number')
2018-02-12 06:48:58 -05:00
.option('-m, --comments-enabled', 'Enable comments')
2017-09-07 11:58:09 -04:00
.option('-l, --licence <licence number>', 'Licence number')
.option('-L, --language <language number>', 'Language number')
2018-02-12 06:48:58 -05:00
.option('-d, --video-description <description>', 'Video description')
2017-09-07 11:58:09 -04:00
.option('-t, --tags <tags>', 'Video tags', list)
.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)
if (!program['tags']) program['tags'] = []
if (!program['nsfw']) program['nsfw'] = false
2018-02-12 06:48:58 -05:00
if (!program['commentsEnabled']) program['commentsEnabled'] = false
2017-09-07 11:58:09 -04:00
if (
!program['url'] ||
2018-02-12 06:48:58 -05:00
!program['username'] ||
!program['password'] ||
!program['videoName'] ||
2017-09-07 11:58:09 -04:00
!program['file']
) {
2018-02-12 06:48:58 -05:00
console.error('Url, username, password, name and input file are required.')
process.exit(-1)
2017-09-07 11:58:09 -04:00
}
if (isAbsolute(program['file']) === false) {
2018-02-12 06:48:58 -05:00
console.error('File path should be absolute.')
process.exit(-1)
2017-09-07 11:58:09 -04:00
}
2018-02-12 06:48:58 -05:00
run().catch(err => console.error(err))
2017-09-07 11:58:09 -04:00
2018-02-12 06:48:58 -05: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 11:58:09 -04:00
2018-02-12 06:48:58 -05: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 11:58:09 -04:00
2018-02-12 06:48:58 -05:00
await accessPromise(program[ 'file' ], constants.F_OK)
console.log('Uploading %s video...', program[ 'videoName' ])
2017-09-07 11:58:09 -04:00
const videoAttributes = {
2018-02-12 06:48:58 -05: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 12:40:24 -05:00
thumbnailfile: program['thumbnailPath'],
2018-03-05 03:53:16 -05:00
previewfile: program['previewPath'],
support: undefined
2017-09-07 11:58:09 -04:00
}
2018-02-12 06:48:58 -05: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 11:58:09 -04:00
}