]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-upload.ts
Merge branch 'develop' into cli-wrapper
[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 => console.error(err))
79 })
80
81 async 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 }
87
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
95
96 await access(program[ 'file' ], constants.F_OK)
97
98 console.log('Uploading %s video...', program[ 'videoName' ])
99
100 const videoAttributes = {
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'],
109 fixture: program['file'],
110 thumbnailfile: program['thumbnail'],
111 previewfile: program['preview'],
112 waitTranscoding: true,
113 privacy: program['privacy'],
114 support: undefined
115 }
116
117 await uploadVideo(program['url'], accessToken, videoAttributes)
118
119 console.log(`Video ${program['videoName']} uploaded.`)
120 process.exit(0)
121 }
122
123 // ----------------------------------------------------------------------------
124
125 function list (val) {
126 return val.split(',')
127 }