]>
Commit | Line | Data |
---|---|---|
1 | import * as program from 'commander' | |
2 | import { access, constants } from 'fs-extra' | |
3 | import { isAbsolute } from 'path' | |
4 | import { getClient, login } from '../../shared/extra-utils' | |
5 | import { uploadVideo } from '../../shared/extra-utils/' | |
6 | import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getNetrc, getRemoteObjectOrDie, getSettings } from './cli' | |
7 | ||
8 | let command = program | |
9 | .name('upload') | |
10 | ||
11 | command = buildCommonVideoOptions(command) | |
12 | ||
13 | command | |
14 | ||
15 | .option('-u, --url <url>', 'Server url') | |
16 | .option('-U, --username <username>', 'Username') | |
17 | .option('-p, --password <token>', 'Password') | |
18 | .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path') | |
19 | .option('-v, --preview <previewPath>', 'Preview path') | |
20 | .option('-f, --file <file>', 'Video absolute file path') | |
21 | .parse(process.argv) | |
22 | ||
23 | Promise.all([ getSettings(), getNetrc() ]) | |
24 | .then(([ settings, netrc ]) => { | |
25 | const { url, username, password } = getRemoteObjectOrDie(program, settings, netrc) | |
26 | ||
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.') | |
30 | ||
31 | process.exit(-1) | |
32 | } | |
33 | ||
34 | if (isAbsolute(program[ 'file' ]) === false) { | |
35 | console.error('File path should be absolute.') | |
36 | process.exit(-1) | |
37 | } | |
38 | ||
39 | run(url, username, password).catch(err => { | |
40 | console.error(err) | |
41 | process.exit(-1) | |
42 | }) | |
43 | }) | |
44 | ||
45 | async function run (url: string, username: string, password: string) { | |
46 | const resClient = await getClient(url) | |
47 | const client = { | |
48 | id: resClient.body.client_id, | |
49 | secret: resClient.body.client_secret | |
50 | } | |
51 | ||
52 | const user = { username, password } | |
53 | ||
54 | let accessToken: string | |
55 | try { | |
56 | const res = await login(url, client, user) | |
57 | accessToken = res.body.access_token | |
58 | } catch (err) { | |
59 | throw new Error('Cannot authenticate. Please check your username/password.') | |
60 | } | |
61 | ||
62 | await access(program[ 'file' ], constants.F_OK) | |
63 | ||
64 | console.log('Uploading %s video...', program[ 'videoName' ]) | |
65 | ||
66 | const defaultAttributes = { | |
67 | tags: command[ 'tags' ], | |
68 | description: command[ 'videoDescription' ] | |
69 | } | |
70 | const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes) | |
71 | ||
72 | Object.assign(videoAttributes, { | |
73 | fixture: program[ 'file' ], | |
74 | thumbnailfile: program[ 'thumbnail' ], | |
75 | previewfile: program[ 'preview' ] | |
76 | }) | |
77 | ||
78 | try { | |
79 | await uploadVideo(url, accessToken, videoAttributes) | |
80 | console.log(`Video ${program[ 'videoName' ]} uploaded.`) | |
81 | process.exit(0) | |
82 | } catch (err) { | |
83 | console.error(require('util').inspect(err)) | |
84 | process.exit(-1) | |
85 | } | |
86 | } | |
87 | ||
88 | // ---------------------------------------------------------------------------- |