Fix video channel description/support max length
[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 import { VideoPrivacy } from '../../shared/models/videos'
8
9 const accessPromise = promisify(access)
10
11 program
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')
27   .parse(process.argv)
28
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
33
34 if (
35   !program['url'] ||
36   !program['username'] ||
37   !program['password'] ||
38   !program['videoName'] ||
39   !program['file']
40 ) {
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.')
46   process.exit(-1)
47 }
48
49 if (isAbsolute(program['file']) === false) {
50   console.error('File path should be absolute.')
51   process.exit(-1)
52 }
53
54 run().catch(err => console.error(err))
55
56 async function run () {
57   const res = await getClient(program[ 'url' ])
58   const client = {
59     id: res.body.client_id,
60     secret: res.body.client_secret
61   }
62
63   const user = {
64     username: program[ 'username' ],
65     password: program[ 'password' ]
66   }
67
68   const res2 = await login(program[ 'url' ], client, user)
69   const accessToken = res2.body.access_token
70
71   await accessPromise(program[ 'file' ], constants.F_OK)
72
73   console.log('Uploading %s video...', program[ 'videoName' ])
74
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     privacy: program['privacy'],
88     support: undefined
89   }
90
91   await uploadVideo(program['url'], accessToken, videoAttributes)
92
93   console.log(`Video ${program['videoName']} uploaded.`)
94   process.exit(0)
95 }
96
97 // ----------------------------------------------------------------------------
98
99 function list (val) {
100   return val.split(',')
101 }