]>
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 | ||
8d2be0ed C |
25 | getServerCredentials(command) |
26 | .then(({ url, username, password }) => { | |
a1587156 C |
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.') | |
8704acf4 | 30 | |
8d2be0ed C |
31 | process.exit(-1) |
32 | } | |
8704acf4 | 33 | |
a1587156 | 34 | if (isAbsolute(program['file']) === false) { |
8d2be0ed C |
35 | console.error('File path should be absolute.') |
36 | process.exit(-1) | |
37 | } | |
8704acf4 | 38 | |
8d2be0ed C |
39 | run(url, username, password).catch(err => { |
40 | console.error(err) | |
41 | process.exit(-1) | |
42 | }) | |
43 | }) | |
a1587156 | 44 | .catch(err => console.error(err)) |
8df87ce7 | 45 | |
2b4dd7e2 | 46 | async function run (url: string, username: string, password: string) { |
8d2be0ed | 47 | const accessToken = await getAccessToken(url, username, password) |
8df87ce7 | 48 | |
a1587156 | 49 | await access(program['file'], constants.F_OK) |
a87d467a | 50 | |
a1587156 | 51 | console.log('Uploading %s video...', program['videoName']) |
8df87ce7 | 52 | |
6f530096 | 53 | const videoAttributes = await buildVideoAttributesFromCommander(url, program) |
1205823f C |
54 | |
55 | Object.assign(videoAttributes, { | |
a1587156 C |
56 | fixture: program['file'], |
57 | thumbnailfile: program['thumbnail'], | |
58 | previewfile: program['preview'] | |
1205823f | 59 | }) |
a87d467a | 60 | |
2b4dd7e2 C |
61 | try { |
62 | await uploadVideo(url, accessToken, videoAttributes) | |
a1587156 | 63 | console.log(`Video ${program['videoName']} uploaded.`) |
2b4dd7e2 C |
64 | process.exit(0) |
65 | } catch (err) { | |
2b4dd7e2 C |
66 | console.error(require('util').inspect(err)) |
67 | process.exit(-1) | |
68 | } | |
a87d467a C |
69 | } |
70 | ||
71 | // ---------------------------------------------------------------------------- |