]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Fix CLI tools
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-upload.ts
CommitLineData
2aaa1a3f
C
1import { registerTSPaths } from '../helpers/register-ts-paths'
2registerTSPaths()
3
12152aa0 4import { program } from 'commander'
c9d5c64f 5import { access, constants } from 'fs-extra'
8df87ce7 6import { isAbsolute } from 'path'
8d2be0ed 7import { getAccessToken } from '../../shared/extra-utils'
94565d52 8import { uploadVideo } from '../../shared/extra-utils/'
078f17e6 9import { buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
8df87ce7 10
1205823f 11let command = program
8704acf4 12 .name('upload')
1205823f
C
13
14command = buildCommonVideoOptions(command)
15
16command
8df87ce7 17 .option('-u, --url <url>', 'Server url')
a87d467a
C
18 .option('-U, --username <username>', 'Username')
19 .option('-p, --password <token>', 'Password')
2422c46b 20 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
02988fdc 21 .option('-v, --preview <previewPath>', 'Preview path')
8df87ce7
C
22 .option('-f, --file <file>', 'Video absolute file path')
23 .parse(process.argv)
24
ba5a8d89
C
25const options = command.opts()
26
8d2be0ed
C
27getServerCredentials(command)
28 .then(({ url, username, password }) => {
ba5a8d89
C
29 if (!options.videoName || !options.file) {
30 if (!options.videoName) console.error('--video-name is required.')
31 if (!options.file) console.error('--file is required.')
8704acf4 32
8d2be0ed
C
33 process.exit(-1)
34 }
8704acf4 35
ba5a8d89 36 if (isAbsolute(options.file) === false) {
8d2be0ed
C
37 console.error('File path should be absolute.')
38 process.exit(-1)
39 }
8704acf4 40
8d2be0ed
C
41 run(url, username, password).catch(err => {
42 console.error(err)
43 process.exit(-1)
44 })
45 })
a1587156 46 .catch(err => console.error(err))
8df87ce7 47
2b4dd7e2 48async function run (url: string, username: string, password: string) {
078f17e6
C
49 const token = await getAccessToken(url, username, password)
50 const server = buildServer(url, token)
8df87ce7 51
ba5a8d89 52 await access(options.file, constants.F_OK)
a87d467a 53
ba5a8d89 54 console.log('Uploading %s video...', options.videoName)
8df87ce7 55
078f17e6 56 const videoAttributes = await buildVideoAttributesFromCommander(server, program)
1205823f
C
57
58 Object.assign(videoAttributes, {
ba5a8d89
C
59 fixture: options.file,
60 thumbnailfile: options.thumbnail,
61 previewfile: options.preview
1205823f 62 })
a87d467a 63
2b4dd7e2 64 try {
078f17e6 65 await uploadVideo(url, token, videoAttributes)
ba5a8d89 66 console.log(`Video ${options.videoName} uploaded.`)
2b4dd7e2
C
67 process.exit(0)
68 } catch (err) {
2b4dd7e2
C
69 console.error(require('util').inspect(err))
70 process.exit(-1)
71 }
a87d467a
C
72}
73
74// ----------------------------------------------------------------------------