1 import * as program from 'commander'
2 import { access, constants } from 'fs'
3 import { isAbsolute } from 'path'
4 import { promisify } from 'util'
5 import { getClient, login } from '../tests/utils'
6 import { uploadVideo } from '../tests/utils/index'
7 import { VideoPrivacy } from '../../shared/models/videos'
9 const accessPromise = promisify(access)
12 .option('-u, --url <url>', 'Server url')
13 .option('-U, --username <username>', 'Username')
14 .option('-p, --password <token>', 'Password')
15 .option('-n, --video-name <name>', 'Video name')
16 .option('-P, --privacy <privacy number>', 'Privacy')
17 .option('-N, --nsfw', 'Video is Not Safe For Work')
18 .option('-c, --category <category number>', 'Category number')
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 if (!program['tags']) program['tags'] = []
30 if (!program['nsfw']) program['nsfw'] = false
31 if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
32 if (!program['commentsEnabled']) program['commentsEnabled'] = false
36 !program['username'] ||
37 !program['password'] ||
38 !program['videoName'] ||
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.')
49 if (isAbsolute(program['file']) === false) {
50 console.error('File path should be absolute.')
54 run().catch(err => console.error(err))
56 async function run () {
57 const res = await getClient(program[ 'url' ])
59 id: res.body.client_id,
60 secret: res.body.client_secret
64 username: program[ 'username' ],
65 password: program[ 'password' ]
68 const res2 = await login(program[ 'url' ], client, user)
69 const accessToken = res2.body.access_token
71 await accessPromise(program[ 'file' ], constants.F_OK)
73 console.log('Uploading %s video...', program[ 'videoName' ])
75 const videoAttributes = {
76 name: program['videoName'],
77 category: program['category'],
78 licence: program['licence'],
79 language: program['language'],
80 nsfw: program['nsfw'],
81 description: program['videoDescription'],
82 tags: program['tags'],
83 commentsEnabled: program['commentsEnabled'],
84 fixture: program['file'],
85 thumbnailfile: program['thumbnailPath'],
86 previewfile: program['previewPath'],
87 waitTranscoding: true,
88 privacy: program['privacy'],
92 await uploadVideo(program['url'], accessToken, videoAttributes)
94 console.log(`Video ${program['videoName']} uploaded.`)
98 // ----------------------------------------------------------------------------
100 function list (val) {
101 return val.split(',')