Add privacy setting to upload.js cli (#422)
[oweals/peertube.git] / server / tools / upload.ts
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
8 const accessPromise = promisify(access)
9
10 program
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 number>', 'Language number')
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')
26   .parse(process.argv)
27
28 if (!program['tags']) program['tags'] = []
29 if (!program['nsfw']) program['nsfw'] = false
30 if (!program['commentsEnabled']) program['commentsEnabled'] = false
31
32 if (
33   !program['url'] ||
34   !program['username'] ||
35   !program['password'] ||
36   !program['videoName'] ||
37   !program['file']
38 ) {
39   if (!program['url']) console.error('--url field is required.')
40   if (!program['username']) console.error('--username field is required.')
41   if (!program['password']) console.error('--password field is required.')
42   if (!program['videoName']) console.error('--video-name field is required.')
43   if (!program['file']) console.error('--file field is required.')
44   process.exit(-1)
45 }
46
47 if (isAbsolute(program['file']) === false) {
48   console.error('File path should be absolute.')
49   process.exit(-1)
50 }
51
52 run().catch(err => console.error(err))
53
54 async function run () {
55   const res = await getClient(program[ 'url' ])
56   const client = {
57     id: res.body.client_id,
58     secret: res.body.client_secret
59   }
60
61   const user = {
62     username: program[ 'username' ],
63     password: program[ 'password' ]
64   }
65
66   const res2 = await login(program[ 'url' ], client, user)
67   const accessToken = res2.body.access_token
68
69   await accessPromise(program[ 'file' ], constants.F_OK)
70
71   console.log('Uploading %s video...', program[ 'videoName' ])
72
73   const videoAttributes = {
74     name: program['videoName'],
75     category: program['category'],
76     licence: program['licence'],
77     language: program['language'],
78     nsfw: program['nsfw'],
79     description: program['videoDescription'],
80     tags: program['tags'],
81     commentsEnabled: program['commentsEnabled'],
82     fixture: program['file'],
83     thumbnailfile: program['thumbnailPath'],
84     previewfile: program['previewPath'],
85     privacy: program['privacy'],
86     support: undefined
87   }
88
89   await uploadVideo(program['url'], accessToken, videoAttributes)
90
91   console.log(`Video ${program['videoName']} uploaded.`)
92   process.exit(0)
93 }
94
95 // ----------------------------------------------------------------------------
96
97 function list (val) {
98   return val.split(',')
99 }