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

100 lines
3.2 KiB
TypeScript
Raw Normal View History

2017-09-07 11:58:09 -04:00
import * as program from 'commander'
import { access, constants } from 'fs-extra'
2017-09-07 11:58:09 -04:00
import { isAbsolute } from 'path'
2018-02-12 06:48:58 -05:00
import { getClient, login } from '../tests/utils'
import { uploadVideo } from '../tests/utils/index'
2018-04-04 04:47:02 -04:00
import { VideoPrivacy } from '../../shared/models/videos'
2017-09-07 11:58:09 -04:00
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')
.option('-P, --privacy <privacy number>', 'Privacy')
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')
2018-04-23 08:39:52 -04:00
.option('-L, --language <language code>', 'Language ISO 639 code (fr or en...)')
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-04-04 04:47:02 -04:00
if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
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']
) {
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 06:48:58 -05:00
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-08-27 10:23:34 -04:00
await access(program[ 'file' ], constants.F_OK)
2018-02-12 06:48:58 -05:00
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-09-03 05:12:53 -04:00
thumbnailfile: program['thumbnail'],
previewfile: program['preview'],
waitTranscoding: true,
privacy: program['privacy'],
2018-03-05 03:53:16 -05:00
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
}