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