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