]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Introduce captions command
[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/'
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
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) {
8d2be0ed 49 const accessToken = await getAccessToken(url, username, password)
8df87ce7 50
ba5a8d89 51 await access(options.file, constants.F_OK)
a87d467a 52
ba5a8d89 53 console.log('Uploading %s video...', options.videoName)
8df87ce7 54
6f530096 55 const videoAttributes = await buildVideoAttributesFromCommander(url, program)
1205823f
C
56
57 Object.assign(videoAttributes, {
ba5a8d89
C
58 fixture: options.file,
59 thumbnailfile: options.thumbnail,
60 previewfile: options.preview
1205823f 61 })
a87d467a 62
2b4dd7e2
C
63 try {
64 await uploadVideo(url, accessToken, videoAttributes)
ba5a8d89 65 console.log(`Video ${options.videoName} uploaded.`)
2b4dd7e2
C
66 process.exit(0)
67 } catch (err) {
2b4dd7e2
C
68 console.error(require('util').inspect(err))
69 process.exit(-1)
70 }
a87d467a
C
71}
72
73// ----------------------------------------------------------------------------