1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getClient, login } from '../../shared/extra-utils'
5 import { uploadVideo } from '../../shared/extra-utils/'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 import { getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
11 .option('-u, --url <url>', 'Server url')
12 .option('-U, --username <username>', 'Username')
13 .option('-p, --password <token>', 'Password')
14 .option('-n, --video-name <name>', 'Video name')
15 .option('-P, --privacy <privacy_number>', 'Privacy')
16 .option('-N, --nsfw', 'Video is Not Safe For Work')
17 .option('-c, --category <category_number>', 'Category number')
18 .option('-C, --channel-id <channel_id>', 'Channel ID')
19 .option('-m, --comments-enabled', 'Enable comments')
20 .option('-l, --licence <licence_number>', 'Licence number')
21 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
22 .option('-d, --video-description <description>', 'Video description')
23 .option('-t, --tags <tags>', 'Video tags', list)
24 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
25 .option('-v, --preview <previewPath>', 'Preview path')
26 .option('-f, --file <file>', 'Video absolute file path')
29 Promise.all([ getSettings(), getNetrc() ])
30 .then(([ settings, netrc ]) => {
31 const { url, username, password } = getRemoteObjectOrDie(program, settings, netrc)
33 if (!program[ 'videoName' ] || !program[ 'file' ] || !program[ 'channelId' ]) {
34 if (!program[ 'videoName' ]) console.error('--video-name is required.')
35 if (!program[ 'file' ]) console.error('--file is required.')
36 if (!program[ 'channelId' ]) console.error('--channel-id is required.')
41 if (isAbsolute(program[ 'file' ]) === false) {
42 console.error('File path should be absolute.')
46 run(url, username, password).catch(err => {
52 async function run (url: string, username: string, password: string) {
53 const resClient = await getClient(url)
55 id: resClient.body.client_id,
56 secret: resClient.body.client_secret
59 const user = { username, password }
61 let accessToken: string
63 const res = await login(url, client, user)
64 accessToken = res.body.access_token
66 throw new Error('Cannot authenticate. Please check your username/password.')
69 await access(program[ 'file' ], constants.F_OK)
71 console.log('Uploading %s video...', program[ 'videoName' ])
73 const videoAttributes = {
74 name: program[ 'videoName' ],
75 category: program[ 'category' ] || undefined,
76 channelId: program[ 'channelId' ],
77 licence: program[ 'licence' ] || undefined,
78 language: program[ 'language' ] || undefined,
79 nsfw: program[ 'nsfw' ] !== undefined ? program[ 'nsfw' ] : false,
80 description: program[ 'videoDescription' ] || undefined,
81 tags: program[ 'tags' ] || [],
82 commentsEnabled: program[ 'commentsEnabled' ] !== undefined ? program[ 'commentsEnabled' ] : true,
83 downloadEnabled: program[ 'downloadEnabled' ] !== undefined ? program[ 'downloadEnabled' ] : true,
84 fixture: program[ 'file' ],
85 thumbnailfile: program[ 'thumbnail' ],
86 previewfile: program[ 'preview' ],
87 waitTranscoding: true,
88 privacy: program[ 'privacy' ] || VideoPrivacy.PUBLIC,
93 await uploadVideo(url, accessToken, videoAttributes)
94 console.log(`Video ${program[ 'videoName' ]} uploaded.`)
97 console.error(require('util').inspect(err))
102 // ----------------------------------------------------------------------------
104 function list (val) {
105 return val.split(',')