Implement support field in video and video channel
[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('-N, --nsfw', 'Video is Not Safe For Work')
16   .option('-c, --category <category number>', 'Category number')
17   .option('-m, --comments-enabled', 'Enable comments')
18   .option('-l, --licence <licence number>', 'Licence number')
19   .option('-L, --language <language number>', 'Language number')
20   .option('-d, --video-description <description>', 'Video description')
21   .option('-t, --tags <tags>', 'Video tags', list)
22   .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
23   .option('-f, --file <file>', 'Video absolute file path')
24   .parse(process.argv)
25
26 if (!program['tags']) program['tags'] = []
27 if (!program['nsfw']) program['nsfw'] = false
28 if (!program['commentsEnabled']) program['commentsEnabled'] = false
29
30 if (
31   !program['url'] ||
32   !program['username'] ||
33   !program['password'] ||
34   !program['videoName'] ||
35   !program['file']
36 ) {
37   console.error('Url, username, password, name and input file are required.')
38   process.exit(-1)
39 }
40
41 if (isAbsolute(program['file']) === false) {
42   console.error('File path should be absolute.')
43   process.exit(-1)
44 }
45
46 run().catch(err => console.error(err))
47
48 async function run () {
49   const res = await getClient(program[ 'url' ])
50   const client = {
51     id: res.body.client_id,
52     secret: res.body.client_secret
53   }
54
55   const user = {
56     username: program[ 'username' ],
57     password: program[ 'password' ]
58   }
59
60   const res2 = await login(program[ 'url' ], client, user)
61   const accessToken = res2.body.access_token
62
63   await accessPromise(program[ 'file' ], constants.F_OK)
64
65   console.log('Uploading %s video...', program[ 'videoName' ])
66
67   const videoAttributes = {
68     name: program['videoName'],
69     category: program['category'],
70     licence: program['licence'],
71     language: program['language'],
72     nsfw: program['nsfw'],
73     description: program['videoDescription'],
74     tags: program['tags'],
75     commentsEnabled: program['commentsEnabled'],
76     fixture: program['file'],
77     thumbnailfile: program['thumbnailPath']
78   }
79
80   await uploadVideo(program['url'], accessToken, videoAttributes)
81
82   console.log(`Video ${program['videoName']} uploaded.`)
83   process.exit(0)
84 }
85
86 // ----------------------------------------------------------------------------
87
88 function list (val) {
89   return val.split(',')
90 }