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