]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Add ability to forbid followers
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-upload.ts
CommitLineData
8df87ce7 1import * as program from 'commander'
c9d5c64f 2import { access, constants } from 'fs-extra'
8df87ce7 3import { isAbsolute } from 'path'
9639bd17 4import { getClient, login } from '../../shared/utils'
5import { uploadVideo } from '../../shared/utils/'
72de91cb 6import { VideoPrivacy } from '../../shared/models/videos'
8704acf4 7import { netrc, getSettings } from './cli'
8df87ce7 8
8df87ce7 9program
8704acf4 10 .name('upload')
8df87ce7 11 .option('-u, --url <url>', 'Server url')
a87d467a
C
12 .option('-U, --username <username>', 'Username')
13 .option('-p, --password <token>', 'Password')
14 .option('-n, --video-name <name>', 'Video name')
8704acf4 15 .option('-P, --privacy <privacy_number>', 'Privacy')
8df87ce7 16 .option('-N, --nsfw', 'Video is Not Safe For Work')
8704acf4 17 .option('-c, --category <category_number>', 'Category number')
c01cfce4 18 .option('-C, --channel-id <channel_id>', 'Channel ID')
a87d467a 19 .option('-m, --comments-enabled', 'Enable comments')
8704acf4
RK
20 .option('-l, --licence <licence_number>', 'Licence number')
21 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
a87d467a 22 .option('-d, --video-description <description>', 'Video description')
8df87ce7 23 .option('-t, --tags <tags>', 'Video tags', list)
2422c46b 24 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
02988fdc 25 .option('-v, --preview <previewPath>', 'Preview path')
8df87ce7
C
26 .option('-f, --file <file>', 'Video absolute file path')
27 .parse(process.argv)
28
29if (!program['tags']) program['tags'] = []
30if (!program['nsfw']) program['nsfw'] = false
72de91cb 31if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
a87d467a 32if (!program['commentsEnabled']) program['commentsEnabled'] = false
7f2cfe3a 33if (!program['downloadEnabled']) program['downloadEnabled'] = true
8df87ce7 34
8704acf4
RK
35getSettings()
36 .then(settings => {
37 if (
38 (!program['url'] ||
39 !program['username'] ||
40 !program['password']) &&
41 (settings.remotes.length === 0)
42 ) {
43 if (!program['url']) console.error('--url field is required.')
44 if (!program['username']) console.error('--username field is required.')
45 if (!program['password']) console.error('--password field is required.')
46 if (!program['videoName']) console.error('--video-name field is required.')
47 if (!program['file']) console.error('--file field is required.')
48 process.exit(-1)
49 }
50
51 if (
52 (!program['url'] ||
53 !program['username'] ||
54 !program['password']) &&
55 (settings.remotes.length > 0)
56 ) {
57 if (!program['url']) {
58 program['url'] = (settings.default !== -1) ?
59 settings.remotes[settings.default] :
60 settings.remotes[0]
61 }
62 if (!program['username']) program['username'] = netrc.machines[program['url']].login
63 if (!program['password']) program['password'] = netrc.machines[program['url']].password
64 }
65
66 if (
67 !program['videoName'] ||
68 !program['file']
69 ) {
70 if (!program['videoName']) console.error('--video-name field is required.')
71 if (!program['file']) console.error('--file field is required.')
72 process.exit(-1)
73 }
74
75 if (isAbsolute(program['file']) === false) {
76 console.error('File path should be absolute.')
77 process.exit(-1)
78 }
79
542e73a2
C
80 run().catch(err => {
81 console.error(err)
82 process.exit(-1)
83 })
8704acf4 84 })
8df87ce7 85
a87d467a
C
86async function run () {
87 const res = await getClient(program[ 'url' ])
88 const client = {
89 id: res.body.client_id,
90 secret: res.body.client_secret
91 }
8df87ce7 92
a87d467a
C
93 const user = {
94 username: program[ 'username' ],
95 password: program[ 'password' ]
96 }
97
542e73a2
C
98 let accessToken: string
99 try {
100 const res2 = await login(program[ 'url' ], client, user)
101 accessToken = res2.body.access_token
102 } catch (err) {
103 throw new Error('Cannot authenticate. Please check your username/password.')
104 }
8df87ce7 105
62689b94 106 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
107
108 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7 109
26b41517 110 const videoAttributes = {
a87d467a
C
111 name: program['videoName'],
112 category: program['category'],
26b41517 113 channelId: program['channelId'],
a87d467a
C
114 licence: program['licence'],
115 language: program['language'],
116 nsfw: program['nsfw'],
117 description: program['videoDescription'],
118 tags: program['tags'],
119 commentsEnabled: program['commentsEnabled'],
7f2cfe3a 120 downloadEnabled: program['downloadEnabled'],
2422c46b 121 fixture: program['file'],
d02911fa
C
122 thumbnailfile: program['thumbnail'],
123 previewfile: program['preview'],
2186386c 124 waitTranscoding: true,
320f8d7e 125 privacy: program['privacy'],
ad34cfc8 126 support: undefined
8df87ce7 127 }
a87d467a 128
542e73a2 129 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
a87d467a
C
130
131 console.log(`Video ${program['videoName']} uploaded.`)
132 process.exit(0)
133}
134
135// ----------------------------------------------------------------------------
136
137function list (val) {
138 return val.split(',')
8df87ce7 139}