]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-upload.ts
Translated using Weblate (Vietnamese)
[github/Chocobozzz/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 const options = command.opts()
26
27 getServerCredentials(command)
28 .then(({ url, username, password }) => {
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.')
32
33 process.exit(-1)
34 }
35
36 if (isAbsolute(options.file) === false) {
37 console.error('File path should be absolute.')
38 process.exit(-1)
39 }
40
41 run(url, username, password).catch(err => {
42 console.error(err)
43 process.exit(-1)
44 })
45 })
46 .catch(err => console.error(err))
47
48 async function run (url: string, username: string, password: string) {
49 const accessToken = await getAccessToken(url, 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(url, 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, 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 // ----------------------------------------------------------------------------