]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-upload.ts
fix lint errors
[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('-m, --comments-enabled', 'Enable comments')
19 .option('-l, --licence <licence_number>', 'Licence number')
20 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
21 .option('-d, --video-description <description>', 'Video description')
22 .option('-t, --tags <tags>', 'Video tags', list)
23 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
24 .option('-v, --preview <previewPath>', 'Preview path')
25 .option('-f, --file <file>', 'Video absolute file path')
26 .parse(process.argv)
27
28 if (!program['tags']) program['tags'] = []
29 if (!program['nsfw']) program['nsfw'] = false
30 if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
31 if (!program['commentsEnabled']) program['commentsEnabled'] = false
32
33 getSettings()
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 => {
79 console.error(err)
80 process.exit(-1)
81 })
82 })
83
84 async 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 }
90
91 const user = {
92 username: program[ 'username' ],
93 password: program[ 'password' ]
94 }
95
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 }
103
104 await access(program[ 'file' ], constants.F_OK)
105
106 console.log('Uploading %s video...', program[ 'videoName' ])
107
108 const videoAttributes = {
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'],
117 fixture: program['file'],
118 thumbnailfile: program['thumbnail'],
119 previewfile: program['preview'],
120 waitTranscoding: true,
121 privacy: program['privacy'],
122 support: undefined
123 }
124
125 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
126
127 console.log(`Video ${program['videoName']} uploaded.`)
128 process.exit(0)
129 }
130
131 // ----------------------------------------------------------------------------
132
133 function list (val) {
134 return val.split(',')
135 }