]>
Commit | Line | Data |
---|---|---|
2aaa1a3f C |
1 | import { registerTSPaths } from '../helpers/register-ts-paths' |
2 | registerTSPaths() | |
3 | ||
8df87ce7 | 4 | import * as program from 'commander' |
c9d5c64f | 5 | import { access, constants } from 'fs-extra' |
8df87ce7 | 6 | import { isAbsolute } from 'path' |
8d2be0ed | 7 | import { getAccessToken } from '../../shared/extra-utils' |
94565d52 | 8 | import { uploadVideo } from '../../shared/extra-utils/' |
8d2be0ed | 9 | import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli' |
8df87ce7 | 10 | |
1205823f | 11 | let command = program |
8704acf4 | 12 | .name('upload') |
1205823f C |
13 | |
14 | command = buildCommonVideoOptions(command) | |
15 | ||
16 | command | |
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 |
25 | const options = command.opts() |
26 | ||
8d2be0ed C |
27 | getServerCredentials(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 | 48 | async 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 | // ---------------------------------------------------------------------------- |