]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Use typescript paths in cli scripts too
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-upload.ts
CommitLineData
2aaa1a3f
C
1import { registerTSPaths } from '../helpers/register-ts-paths'
2registerTSPaths()
3
8df87ce7 4import * as 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/'
8d2be0ed 9import { buildCommonVideoOptions, 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
8d2be0ed
C
25getServerCredentials(command)
26 .then(({ url, username, password }) => {
27 if (!program[ 'videoName' ] || !program[ 'file' ]) {
28 if (!program[ 'videoName' ]) console.error('--video-name is required.')
29 if (!program[ 'file' ]) console.error('--file is required.')
8704acf4 30
8d2be0ed
C
31 process.exit(-1)
32 }
8704acf4 33
8d2be0ed
C
34 if (isAbsolute(program[ 'file' ]) === false) {
35 console.error('File path should be absolute.')
36 process.exit(-1)
37 }
8704acf4 38
8d2be0ed
C
39 run(url, username, password).catch(err => {
40 console.error(err)
41 process.exit(-1)
42 })
43 })
8df87ce7 44
2b4dd7e2 45async function run (url: string, username: string, password: string) {
8d2be0ed 46 const accessToken = await getAccessToken(url, username, password)
8df87ce7 47
62689b94 48 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
49
50 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7 51
6f530096 52 const videoAttributes = await buildVideoAttributesFromCommander(url, program)
1205823f
C
53
54 Object.assign(videoAttributes, {
1a12f66d
C
55 fixture: program[ 'file' ],
56 thumbnailfile: program[ 'thumbnail' ],
1205823f
C
57 previewfile: program[ 'preview' ]
58 })
a87d467a 59
2b4dd7e2
C
60 try {
61 await uploadVideo(url, accessToken, videoAttributes)
1a12f66d 62 console.log(`Video ${program[ 'videoName' ]} uploaded.`)
2b4dd7e2
C
63 process.exit(0)
64 } catch (err) {
2b4dd7e2
C
65 console.error(require('util').inspect(err))
66 process.exit(-1)
67 }
a87d467a
C
68}
69
70// ----------------------------------------------------------------------------