]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Improve overview section titles
[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
78 run().catch(err => console.error(err))
79 })
8df87ce7 80
a87d467a
C
81async function run () {
82 const res = await getClient(program[ 'url' ])
83 const client = {
84 id: res.body.client_id,
85 secret: res.body.client_secret
86 }
8df87ce7 87
a87d467a
C
88 const user = {
89 username: program[ 'username' ],
90 password: program[ 'password' ]
91 }
92
93 const res2 = await login(program[ 'url' ], client, user)
94 const accessToken = res2.body.access_token
8df87ce7 95
62689b94 96 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
97
98 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7
C
99
100 const videoAttributes = {
a87d467a
C
101 name: program['videoName'],
102 category: program['category'],
103 licence: program['licence'],
104 language: program['language'],
105 nsfw: program['nsfw'],
106 description: program['videoDescription'],
107 tags: program['tags'],
108 commentsEnabled: program['commentsEnabled'],
2422c46b 109 fixture: program['file'],
d02911fa
C
110 thumbnailfile: program['thumbnail'],
111 previewfile: program['preview'],
2186386c 112 waitTranscoding: true,
320f8d7e 113 privacy: program['privacy'],
ad34cfc8 114 support: undefined
8df87ce7 115 }
a87d467a
C
116
117 await uploadVideo(program['url'], accessToken, videoAttributes)
118
119 console.log(`Video ${program['videoName']} uploaded.`)
120 process.exit(0)
121}
122
123// ----------------------------------------------------------------------------
124
125function list (val) {
126 return val.split(',')
8df87ce7 127}