]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
improve description of the HTTP video import feature
[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')
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
8df87ce7 33
8704acf4
RK
34getSettings()
35 .then(settings => {
36 if (
37 (!program['url'] ||
38 !program['username'] ||
39 !program['password']) &&
40 (settings.remotes.length === 0)
41 ) {
42 if (!program['url']) console.error('--url field is required.')
43 if (!program['username']) console.error('--username field is required.')
44 if (!program['password']) console.error('--password field is required.')
45 if (!program['videoName']) console.error('--video-name field is required.')
46 if (!program['file']) console.error('--file field is required.')
47 process.exit(-1)
48 }
49
50 if (
51 (!program['url'] ||
52 !program['username'] ||
53 !program['password']) &&
54 (settings.remotes.length > 0)
55 ) {
56 if (!program['url']) {
57 program['url'] = (settings.default !== -1) ?
58 settings.remotes[settings.default] :
59 settings.remotes[0]
60 }
61 if (!program['username']) program['username'] = netrc.machines[program['url']].login
62 if (!program['password']) program['password'] = netrc.machines[program['url']].password
63 }
64
65 if (
66 !program['videoName'] ||
67 !program['file']
68 ) {
69 if (!program['videoName']) console.error('--video-name field is required.')
70 if (!program['file']) console.error('--file field is required.')
71 process.exit(-1)
72 }
73
74 if (isAbsolute(program['file']) === false) {
75 console.error('File path should be absolute.')
76 process.exit(-1)
77 }
78
542e73a2
C
79 run().catch(err => {
80 console.error(err)
81 process.exit(-1)
82 })
8704acf4 83 })
8df87ce7 84
a87d467a
C
85async function run () {
86 const res = await getClient(program[ 'url' ])
87 const client = {
88 id: res.body.client_id,
89 secret: res.body.client_secret
90 }
8df87ce7 91
a87d467a
C
92 const user = {
93 username: program[ 'username' ],
94 password: program[ 'password' ]
95 }
96
542e73a2
C
97 let accessToken: string
98 try {
99 const res2 = await login(program[ 'url' ], client, user)
100 accessToken = res2.body.access_token
101 } catch (err) {
102 throw new Error('Cannot authenticate. Please check your username/password.')
103 }
8df87ce7 104
62689b94 105 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
106
107 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7 108
26b41517 109 const videoAttributes = {
a87d467a
C
110 name: program['videoName'],
111 category: program['category'],
26b41517 112 channelId: program['channelId'],
a87d467a
C
113 licence: program['licence'],
114 language: program['language'],
115 nsfw: program['nsfw'],
116 description: program['videoDescription'],
117 tags: program['tags'],
118 commentsEnabled: program['commentsEnabled'],
2422c46b 119 fixture: program['file'],
d02911fa
C
120 thumbnailfile: program['thumbnail'],
121 previewfile: program['preview'],
2186386c 122 waitTranscoding: true,
320f8d7e 123 privacy: program['privacy'],
ad34cfc8 124 support: undefined
8df87ce7 125 }
a87d467a 126
542e73a2 127 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
a87d467a
C
128
129 console.log(`Video ${program['videoName']} uploaded.`)
130 process.exit(0)
131}
132
133// ----------------------------------------------------------------------------
134
135function list (val) {
136 return val.split(',')
8df87ce7 137}