1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getClient, login } from '../tests/utils'
5 import { uploadVideo } from '../tests/utils/index'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 import { netrc, 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('-m, --comments-enabled', 'Enable comments')
19 .option('-l, --licence <licence_number>', 'Licence number')
20 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
21 .option('-d, --video-description <description>', 'Video description')
22 .option('-t, --tags <tags>', 'Video tags', list)
23 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
24 .option('-v, --preview <previewPath>', 'Preview path')
25 .option('-f, --file <file>', 'Video absolute file path')
28 if (!program['tags']) program['tags'] = []
29 if (!program['nsfw']) program['nsfw'] = false
30 if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
31 if (!program['commentsEnabled']) program['commentsEnabled'] = false
37 !program['username'] ||
38 !program['password']) &&
39 (settings.remotes.length === 0)
41 if (!program['url']) console.error('--url field is required.')
42 if (!program['username']) console.error('--username field is required.')
43 if (!program['password']) console.error('--password field is required.')
44 if (!program['videoName']) console.error('--video-name field is required.')
45 if (!program['file']) console.error('--file field is required.')
51 !program['username'] ||
52 !program['password']) &&
53 (settings.remotes.length > 0)
55 if (!program['url']) {
56 program['url'] = (settings.default !== -1) ?
57 settings.remotes[settings.default] :
60 if (!program['username']) program['username'] = netrc.machines[program['url']].login
61 if (!program['password']) program['password'] = netrc.machines[program['url']].password
65 !program['videoName'] ||
68 if (!program['videoName']) console.error('--video-name field is required.')
69 if (!program['file']) console.error('--file field is required.')
73 if (isAbsolute(program['file']) === false) {
74 console.error('File path should be absolute.')
84 async function run () {
85 const res = await getClient(program[ 'url' ])
87 id: res.body.client_id,
88 secret: res.body.client_secret
92 username: program[ 'username' ],
93 password: program[ 'password' ]
96 let accessToken: string
98 const res2 = await login(program[ 'url' ], client, user)
99 accessToken = res2.body.access_token
101 throw new Error('Cannot authenticate. Please check your username/password.')
104 await access(program[ 'file' ], constants.F_OK)
106 console.log('Uploading %s video...', program[ 'videoName' ])
108 const videoAttributes = {
109 name: program['videoName'],
110 category: program['category'],
111 licence: program['licence'],
112 language: program['language'],
113 nsfw: program['nsfw'],
114 description: program['videoDescription'],
115 tags: program['tags'],
116 commentsEnabled: program['commentsEnabled'],
117 fixture: program['file'],
118 thumbnailfile: program['thumbnail'],
119 previewfile: program['preview'],
120 waitTranscoding: true,
121 privacy: program['privacy'],
125 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
127 console.log(`Video ${program['videoName']} uploaded.`)
131 // ----------------------------------------------------------------------------
133 function list (val) {
134 return val.split(',')