]>
Commit | Line | Data |
---|---|---|
1 | import { registerTSPaths } from '../helpers/register-ts-paths' | |
2 | registerTSPaths() | |
3 | ||
4 | import { program } from 'commander' | |
5 | import { access, constants } from 'fs-extra' | |
6 | import { isAbsolute } from 'path' | |
7 | import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' | |
8 | ||
9 | let command = program | |
10 | .name('upload') | |
11 | ||
12 | command = buildCommonVideoOptions(command) | |
13 | ||
14 | command | |
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 | const options = command.opts() | |
24 | ||
25 | getServerCredentials(command) | |
26 | .then(({ url, username, password }) => { | |
27 | if (!options.videoName || !options.file) { | |
28 | if (!options.videoName) console.error('--video-name is required.') | |
29 | if (!options.file) console.error('--file is required.') | |
30 | ||
31 | process.exit(-1) | |
32 | } | |
33 | ||
34 | if (isAbsolute(options.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 | .catch(err => console.error(err)) | |
45 | ||
46 | async function run (url: string, username: string, password: string) { | |
47 | const server = buildServer(url) | |
48 | await assignToken(server, username, password) | |
49 | ||
50 | await access(options.file, constants.F_OK) | |
51 | ||
52 | console.log('Uploading %s video...', options.videoName) | |
53 | ||
54 | const baseAttributes = await buildVideoAttributesFromCommander(server, program) | |
55 | ||
56 | const attributes = { | |
57 | ...baseAttributes, | |
58 | ||
59 | fixture: options.file, | |
60 | thumbnailfile: options.thumbnail, | |
61 | previewfile: options.preview | |
62 | } | |
63 | ||
64 | try { | |
65 | await server.videos.upload({ attributes }) | |
66 | console.log(`Video ${options.videoName} uploaded.`) | |
67 | process.exit(0) | |
68 | } catch (err) { | |
69 | console.error(require('util').inspect(err)) | |
70 | process.exit(-1) | |
71 | } | |
72 | } | |
73 | ||
74 | // ---------------------------------------------------------------------------- |