PeerTube/server/tools/peertube-upload.ts

108 lines
3.7 KiB
TypeScript
Raw Normal View History

2017-09-07 17:58:09 +02:00
import * as program from 'commander'
import { access, constants } from 'fs-extra'
2017-09-07 17:58:09 +02:00
import { isAbsolute } from 'path'
import { getClient, login } from '../../shared/extra-utils'
import { uploadVideo } from '../../shared/extra-utils/'
2018-04-04 10:47:02 +02:00
import { VideoPrivacy } from '../../shared/models/videos'
import { getRemoteObjectOrDie, getSettings } from './cli'
2017-09-07 17:58:09 +02:00
program
.name('upload')
2017-09-07 17:58:09 +02:00
.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')
.option('-P, --privacy <privacy_number>', 'Privacy')
2017-09-07 17:58:09 +02:00
.option('-N, --nsfw', 'Video is Not Safe For Work')
.option('-c, --category <category_number>', 'Category number')
.option('-C, --channel-id <channel_id>', 'Channel ID')
2018-02-12 12:48:58 +01:00
.option('-m, --comments-enabled', 'Enable comments')
.option('-l, --licence <licence_number>', 'Licence number')
.option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
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)
getSettings()
.then(settings => {
const { url, username, password } = getRemoteObjectOrDie(program, settings)
if (!program['videoName'] || !program['file'] || !program['channelId']) {
if (!program['videoName']) console.error('--video-name is required.')
if (!program['file']) console.error('--file is required.')
if (!program['channelId']) console.error('--channel-id is required.')
process.exit(-1)
}
if (isAbsolute(program['file']) === false) {
console.error('File path should be absolute.')
process.exit(-1)
}
run(url, username, password).catch(err => {
console.error(err)
process.exit(-1)
})
})
2017-09-07 17:58:09 +02:00
async function run (url: string, username: string, password: string) {
const resClient = await getClient(program[ 'url' ])
2018-02-12 12:48:58 +01:00
const client = {
id: resClient.body.client_id,
secret: resClient.body.client_secret
2018-02-12 12:48:58 +01:00
}
2017-09-07 17:58:09 +02:00
const user = { username, password }
2018-02-12 12:48:58 +01:00
let accessToken: string
try {
const res = await login(url, client, user)
accessToken = res.body.access_token
} catch (err) {
throw new Error('Cannot authenticate. Please check your username/password.')
}
2017-09-07 17:58:09 +02:00
2018-08-27 16:23:34 +02:00
await access(program[ 'file' ], constants.F_OK)
2018-02-12 12:48:58 +01:00
console.log('Uploading %s video...', program[ 'videoName' ])
2017-09-07 17:58:09 +02:00
2018-09-29 02:49:33 +02:00
const videoAttributes = {
2018-02-12 12:48:58 +01:00
name: program['videoName'],
category: program['category'] || undefined,
2018-09-29 02:49:33 +02:00
channelId: program['channelId'],
licence: program['licence'] || undefined,
language: program['language'] || undefined,
nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false,
description: program['videoDescription'] || '',
tags: program['tags'] || [],
commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true,
downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true,
fixture: program['file'],
2018-09-03 11:12:53 +02:00
thumbnailfile: program['thumbnail'],
previewfile: program['preview'],
waitTranscoding: true,
privacy: program['privacy'] || VideoPrivacy.PUBLIC,
2018-03-05 09:53:16 +01:00
support: undefined
2017-09-07 17:58:09 +02:00
}
2018-02-12 12:48:58 +01:00
try {
await uploadVideo(url, accessToken, videoAttributes)
console.log(`Video ${program['videoName']} uploaded.`)
process.exit(0)
} catch (err) {
console.log('coucou')
console.error(require('util').inspect(err))
process.exit(-1)
}
2018-02-12 12:48:58 +01:00
}
// ----------------------------------------------------------------------------
function list (val) {
return val.split(',')
2017-09-07 17:58:09 +02:00
}