]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Improve upliad script error handling for token
[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'
a87d467a
C
4import { getClient, login } from '../tests/utils'
5import { uploadVideo } from '../tests/utils/index'
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')
a87d467a 18 .option('-m, --comments-enabled', 'Enable comments')
8704acf4
RK
19 .option('-l, --licence <licence_number>', 'Licence number')
20 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
a87d467a 21 .option('-d, --video-description <description>', 'Video description')
8df87ce7 22 .option('-t, --tags <tags>', 'Video tags', list)
2422c46b 23 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
02988fdc 24 .option('-v, --preview <previewPath>', 'Preview path')
8df87ce7
C
25 .option('-f, --file <file>', 'Video absolute file path')
26 .parse(process.argv)
27
28if (!program['tags']) program['tags'] = []
29if (!program['nsfw']) program['nsfw'] = false
72de91cb 30if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
a87d467a 31if (!program['commentsEnabled']) program['commentsEnabled'] = false
8df87ce7 32
8704acf4
RK
33getSettings()
34 .then(settings => {
35 if (
36 (!program['url'] ||
37 !program['username'] ||
38 !program['password']) &&
39 (settings.remotes.length === 0)
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 (
50 (!program['url'] ||
51 !program['username'] ||
52 !program['password']) &&
53 (settings.remotes.length > 0)
54 ) {
55 if (!program['url']) {
56 program['url'] = (settings.default !== -1) ?
57 settings.remotes[settings.default] :
58 settings.remotes[0]
59 }
60 if (!program['username']) program['username'] = netrc.machines[program['url']].login
61 if (!program['password']) program['password'] = netrc.machines[program['url']].password
62 }
63
64 if (
65 !program['videoName'] ||
66 !program['file']
67 ) {
68 if (!program['videoName']) console.error('--video-name field is required.')
69 if (!program['file']) console.error('--file field is required.')
70 process.exit(-1)
71 }
72
73 if (isAbsolute(program['file']) === false) {
74 console.error('File path should be absolute.')
75 process.exit(-1)
76 }
77
542e73a2
C
78 run().catch(err => {
79 console.error(err)
80 process.exit(-1)
81 })
8704acf4 82 })
8df87ce7 83
a87d467a
C
84async function run () {
85 const res = await getClient(program[ 'url' ])
86 const client = {
87 id: res.body.client_id,
88 secret: res.body.client_secret
89 }
8df87ce7 90
a87d467a
C
91 const user = {
92 username: program[ 'username' ],
93 password: program[ 'password' ]
94 }
95
542e73a2
C
96 let accessToken: string
97 try {
98 const res2 = await login(program[ 'url' ], client, user)
99 accessToken = res2.body.access_token
100 } catch (err) {
101 throw new Error('Cannot authenticate. Please check your username/password.')
102 }
8df87ce7 103
62689b94 104 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
105
106 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7
C
107
108 const videoAttributes = {
a87d467a
C
109 name: program['videoName'],
110 category: program['category'],
111 licence: program['licence'],
112 language: program['language'],
113 nsfw: program['nsfw'],
114 description: program['videoDescription'],
115 tags: program['tags'],
116 commentsEnabled: program['commentsEnabled'],
2422c46b 117 fixture: program['file'],
d02911fa
C
118 thumbnailfile: program['thumbnail'],
119 previewfile: program['preview'],
2186386c 120 waitTranscoding: true,
320f8d7e 121 privacy: program['privacy'],
ad34cfc8 122 support: undefined
8df87ce7 123 }
a87d467a 124
542e73a2 125 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
a87d467a
C
126
127 console.log(`Video ${program['videoName']} uploaded.`)
128 process.exit(0)
129}
130
131// ----------------------------------------------------------------------------
132
133function list (val) {
134 return val.split(',')
8df87ce7 135}