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