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.')
78 run().catch(err => console.error(err))
81 async function run () {
82 const res = await getClient(program[ 'url' ])
84 id: res.body.client_id,
85 secret: res.body.client_secret
89 username: program[ 'username' ],
90 password: program[ 'password' ]
93 const res2 = await login(program[ 'url' ], client, user)
94 const accessToken = res2.body.access_token
96 await access(program[ 'file' ], constants.F_OK)
98 console.log('Uploading %s video...', program[ 'videoName' ])
100 const videoAttributes = {
101 name: program['videoName'],
102 category: program['category'],
103 licence: program['licence'],
104 language: program['language'],
105 nsfw: program['nsfw'],
106 description: program['videoDescription'],
107 tags: program['tags'],
108 commentsEnabled: program['commentsEnabled'],
109 fixture: program['file'],
110 thumbnailfile: program['thumbnail'],
111 previewfile: program['preview'],
112 waitTranscoding: true,
113 privacy: program['privacy'],
117 await uploadVideo(program['url'], accessToken, videoAttributes)
119 console.log(`Video ${program['videoName']} uploaded.`)
123 // ----------------------------------------------------------------------------
125 function list (val) {
126 return val.split(',')