]>
Commit | Line | Data |
---|---|---|
8df87ce7 | 1 | import * as program from 'commander' |
c9d5c64f | 2 | import { access, constants } from 'fs-extra' |
8df87ce7 | 3 | import { isAbsolute } from 'path' |
a87d467a C |
4 | import { getClient, login } from '../tests/utils' |
5 | import { uploadVideo } from '../tests/utils/index' | |
72de91cb | 6 | import { VideoPrivacy } from '../../shared/models/videos' |
8704acf4 | 7 | import { netrc, getSettings } from './cli' |
8df87ce7 | 8 | |
8df87ce7 | 9 | program |
8704acf4 | 10 | .name('upload') |
8df87ce7 | 11 | .option('-u, --url <url>', 'Server url') |
a87d467a C |
12 | .option('-U, --username <username>', 'Username') |
13 | .option('-p, --password <token>', 'Password') | |
14 | .option('-n, --video-name <name>', 'Video name') | |
8704acf4 | 15 | .option('-P, --privacy <privacy_number>', 'Privacy') |
8df87ce7 | 16 | .option('-N, --nsfw', 'Video is Not Safe For Work') |
8704acf4 | 17 | .option('-c, --category <category_number>', 'Category number') |
c01cfce4 | 18 | .option('-C, --channel-id <channel_id>', 'Channel ID') |
a87d467a | 19 | .option('-m, --comments-enabled', 'Enable comments') |
8704acf4 RK |
20 | .option('-l, --licence <licence_number>', 'Licence number') |
21 | .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)') | |
a87d467a | 22 | .option('-d, --video-description <description>', 'Video description') |
8df87ce7 | 23 | .option('-t, --tags <tags>', 'Video tags', list) |
2422c46b | 24 | .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path') |
02988fdc | 25 | .option('-v, --preview <previewPath>', 'Preview path') |
8df87ce7 C |
26 | .option('-f, --file <file>', 'Video absolute file path') |
27 | .parse(process.argv) | |
28 | ||
29 | if (!program['tags']) program['tags'] = [] | |
30 | if (!program['nsfw']) program['nsfw'] = false | |
72de91cb | 31 | if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC |
a87d467a | 32 | if (!program['commentsEnabled']) program['commentsEnabled'] = false |
8df87ce7 | 33 | |
8704acf4 RK |
34 | getSettings() |
35 | .then(settings => { | |
36 | if ( | |
37 | (!program['url'] || | |
38 | !program['username'] || | |
39 | !program['password']) && | |
40 | (settings.remotes.length === 0) | |
41 | ) { | |
42 | if (!program['url']) console.error('--url field is required.') | |
43 | if (!program['username']) console.error('--username field is required.') | |
44 | if (!program['password']) console.error('--password field is required.') | |
45 | if (!program['videoName']) console.error('--video-name field is required.') | |
46 | if (!program['file']) console.error('--file field is required.') | |
47 | process.exit(-1) | |
48 | } | |
49 | ||
50 | if ( | |
51 | (!program['url'] || | |
52 | !program['username'] || | |
53 | !program['password']) && | |
54 | (settings.remotes.length > 0) | |
55 | ) { | |
56 | if (!program['url']) { | |
57 | program['url'] = (settings.default !== -1) ? | |
58 | settings.remotes[settings.default] : | |
59 | settings.remotes[0] | |
60 | } | |
61 | if (!program['username']) program['username'] = netrc.machines[program['url']].login | |
62 | if (!program['password']) program['password'] = netrc.machines[program['url']].password | |
63 | } | |
64 | ||
65 | if ( | |
66 | !program['videoName'] || | |
67 | !program['file'] | |
68 | ) { | |
69 | if (!program['videoName']) console.error('--video-name field is required.') | |
70 | if (!program['file']) console.error('--file field is required.') | |
71 | process.exit(-1) | |
72 | } | |
73 | ||
74 | if (isAbsolute(program['file']) === false) { | |
75 | console.error('File path should be absolute.') | |
76 | process.exit(-1) | |
77 | } | |
78 | ||
542e73a2 C |
79 | run().catch(err => { |
80 | console.error(err) | |
81 | process.exit(-1) | |
82 | }) | |
8704acf4 | 83 | }) |
8df87ce7 | 84 | |
a87d467a C |
85 | async function run () { |
86 | const res = await getClient(program[ 'url' ]) | |
87 | const client = { | |
88 | id: res.body.client_id, | |
89 | secret: res.body.client_secret | |
90 | } | |
8df87ce7 | 91 | |
a87d467a C |
92 | const user = { |
93 | username: program[ 'username' ], | |
94 | password: program[ 'password' ] | |
95 | } | |
96 | ||
542e73a2 C |
97 | let accessToken: string |
98 | try { | |
99 | const res2 = await login(program[ 'url' ], client, user) | |
100 | accessToken = res2.body.access_token | |
101 | } catch (err) { | |
102 | throw new Error('Cannot authenticate. Please check your username/password.') | |
103 | } | |
8df87ce7 | 104 | |
62689b94 | 105 | await access(program[ 'file' ], constants.F_OK) |
a87d467a C |
106 | |
107 | console.log('Uploading %s video...', program[ 'videoName' ]) | |
8df87ce7 | 108 | |
c01cfce4 | 109 | let videoAttributes:any = { |
a87d467a C |
110 | name: program['videoName'], |
111 | category: program['category'], | |
112 | licence: program['licence'], | |
113 | language: program['language'], | |
114 | nsfw: program['nsfw'], | |
115 | description: program['videoDescription'], | |
116 | tags: program['tags'], | |
117 | commentsEnabled: program['commentsEnabled'], | |
2422c46b | 118 | fixture: program['file'], |
d02911fa C |
119 | thumbnailfile: program['thumbnail'], |
120 | previewfile: program['preview'], | |
2186386c | 121 | waitTranscoding: true, |
320f8d7e | 122 | privacy: program['privacy'], |
ad34cfc8 | 123 | support: undefined |
8df87ce7 | 124 | } |
a87d467a | 125 | |
c01cfce4 AM |
126 | if (program['channelId']) { |
127 | videoAttributes.channelId = program['channelId'] | |
128 | } | |
129 | ||
542e73a2 | 130 | await uploadVideo(program[ 'url' ], accessToken, videoAttributes) |
a87d467a C |
131 | |
132 | console.log(`Video ${program['videoName']} uploaded.`) | |
133 | process.exit(0) | |
134 | } | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | function list (val) { | |
139 | return val.split(',') | |
8df87ce7 | 140 | } |