]>
Commit | Line | Data |
---|---|---|
8df87ce7 | 1 | import * as program from 'commander' |
c9d5c64f | 2 | import { access, constants } from 'fs-extra' |
8df87ce7 | 3 | import { isAbsolute } from 'path' |
8d2be0ed | 4 | import { getAccessToken } from '../../shared/extra-utils' |
94565d52 | 5 | import { uploadVideo } from '../../shared/extra-utils/' |
8d2be0ed | 6 | import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli' |
8df87ce7 | 7 | |
1205823f | 8 | let command = program |
8704acf4 | 9 | .name('upload') |
1205823f C |
10 | |
11 | command = buildCommonVideoOptions(command) | |
12 | ||
13 | command | |
8df87ce7 | 14 | .option('-u, --url <url>', 'Server url') |
a87d467a C |
15 | .option('-U, --username <username>', 'Username') |
16 | .option('-p, --password <token>', 'Password') | |
2422c46b | 17 | .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path') |
02988fdc | 18 | .option('-v, --preview <previewPath>', 'Preview path') |
8df87ce7 C |
19 | .option('-f, --file <file>', 'Video absolute file path') |
20 | .parse(process.argv) | |
21 | ||
8d2be0ed C |
22 | getServerCredentials(command) |
23 | .then(({ url, username, password }) => { | |
24 | if (!program[ 'videoName' ] || !program[ 'file' ]) { | |
25 | if (!program[ 'videoName' ]) console.error('--video-name is required.') | |
26 | if (!program[ 'file' ]) console.error('--file is required.') | |
8704acf4 | 27 | |
8d2be0ed C |
28 | process.exit(-1) |
29 | } | |
8704acf4 | 30 | |
8d2be0ed C |
31 | if (isAbsolute(program[ 'file' ]) === false) { |
32 | console.error('File path should be absolute.') | |
33 | process.exit(-1) | |
34 | } | |
8704acf4 | 35 | |
8d2be0ed C |
36 | run(url, username, password).catch(err => { |
37 | console.error(err) | |
38 | process.exit(-1) | |
39 | }) | |
40 | }) | |
8df87ce7 | 41 | |
2b4dd7e2 | 42 | async function run (url: string, username: string, password: string) { |
8d2be0ed | 43 | const accessToken = await getAccessToken(url, username, password) |
8df87ce7 | 44 | |
62689b94 | 45 | await access(program[ 'file' ], constants.F_OK) |
a87d467a C |
46 | |
47 | console.log('Uploading %s video...', program[ 'videoName' ]) | |
8df87ce7 | 48 | |
1205823f C |
49 | const defaultAttributes = { |
50 | tags: command[ 'tags' ], | |
51 | description: command[ 'videoDescription' ] | |
52 | } | |
53 | const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes) | |
54 | ||
55 | Object.assign(videoAttributes, { | |
1a12f66d C |
56 | fixture: program[ 'file' ], |
57 | thumbnailfile: program[ 'thumbnail' ], | |
1205823f C |
58 | previewfile: program[ 'preview' ] |
59 | }) | |
a87d467a | 60 | |
2b4dd7e2 C |
61 | try { |
62 | await uploadVideo(url, accessToken, videoAttributes) | |
1a12f66d | 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 | // ---------------------------------------------------------------------------- |