PeerTube/server/tools/upload.ts

98 lines
3.1 KiB
TypeScript
Raw Normal View History

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