]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-upload.ts
Fix some defaults values + indentation
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-upload.ts
1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getClient, login } from '../tests/utils'
5 import { uploadVideo } from '../tests/utils/index'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 import { netrc, getSettings } from './cli'
8
9 program
10 .name('upload')
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('-C, --channel-id <channel_id>', 'Channel ID')
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 if (!program['downloadingEnabled']) program['downloadingEnabled'] = true
34
35 getSettings()
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
80 run().catch(err => {
81 console.error(err)
82 process.exit(-1)
83 })
84 })
85
86 async 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 }
92
93 const user = {
94 username: program[ 'username' ],
95 password: program[ 'password' ]
96 }
97
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 }
105
106 await access(program[ 'file' ], constants.F_OK)
107
108 console.log('Uploading %s video...', program[ 'videoName' ])
109
110 const videoAttributes = {
111 name: program['videoName'],
112 category: program['category'],
113 channelId: program['channelId'],
114 licence: program['licence'],
115 language: program['language'],
116 nsfw: program['nsfw'],
117 description: program['videoDescription'],
118 tags: program['tags'],
119 commentsEnabled: program['commentsEnabled'],
120 downloadingEnabled: program['downloadingEnabled'],
121 fixture: program['file'],
122 thumbnailfile: program['thumbnail'],
123 previewfile: program['preview'],
124 waitTranscoding: true,
125 privacy: program['privacy'],
126 support: undefined
127 }
128
129 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
130
131 console.log(`Video ${program['videoName']} uploaded.`)
132 process.exit(0)
133 }
134
135 // ----------------------------------------------------------------------------
136
137 function list (val) {
138 return val.split(',')
139 }