diff options
Diffstat (limited to 'server/tools/peertube-upload.ts')
-rw-r--r-- | server/tools/peertube-upload.ts | 98 |
1 files changed, 34 insertions, 64 deletions
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index cc7bd9b4c..687f2e60b 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts | |||
@@ -1,10 +1,10 @@ | |||
1 | import * as program from 'commander' | 1 | import * as program from 'commander' |
2 | import { access, constants } from 'fs-extra' | 2 | import { access, constants } from 'fs-extra' |
3 | import { isAbsolute } from 'path' | 3 | import { isAbsolute } from 'path' |
4 | import { getClient, login } from '../../shared/utils' | 4 | import { getClient, login } from '../../shared/extra-utils' |
5 | import { uploadVideo } from '../../shared/utils/' | 5 | import { uploadVideo } from '../../shared/extra-utils/' |
6 | import { VideoPrivacy } from '../../shared/models/videos' | 6 | import { VideoPrivacy } from '../../shared/models/videos' |
7 | import { netrc, getSettings } from './cli' | 7 | import { getRemoteObjectOrDie, getSettings } from './cli' |
8 | 8 | ||
9 | program | 9 | program |
10 | .name('upload') | 10 | .name('upload') |
@@ -26,48 +26,15 @@ program | |||
26 | .option('-f, --file <file>', 'Video absolute file path') | 26 | .option('-f, --file <file>', 'Video absolute file path') |
27 | .parse(process.argv) | 27 | .parse(process.argv) |
28 | 28 | ||
29 | if (!program['tags']) program['tags'] = [] | ||
30 | if (!program['nsfw']) program['nsfw'] = false | ||
31 | if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC | ||
32 | if (!program['commentsEnabled']) program['commentsEnabled'] = false | ||
33 | |||
34 | getSettings() | 29 | getSettings() |
35 | .then(settings => { | 30 | .then(settings => { |
36 | if ( | 31 | const { url, username, password } = getRemoteObjectOrDie(program, settings) |
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 | 32 | ||
50 | if ( | 33 | if (!program['videoName'] || !program['file'] || !program['channelId']) { |
51 | (!program['url'] || | 34 | if (!program['videoName']) console.error('--video-name is required.') |
52 | !program['username'] || | 35 | if (!program['file']) console.error('--file is required.') |
53 | !program['password']) && | 36 | if (!program['channelId']) console.error('--channel-id is required.') |
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 | 37 | ||
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) | 38 | process.exit(-1) |
72 | } | 39 | } |
73 | 40 | ||
@@ -76,28 +43,25 @@ getSettings() | |||
76 | process.exit(-1) | 43 | process.exit(-1) |
77 | } | 44 | } |
78 | 45 | ||
79 | run().catch(err => { | 46 | run(url, username, password).catch(err => { |
80 | console.error(err) | 47 | console.error(err) |
81 | process.exit(-1) | 48 | process.exit(-1) |
82 | }) | 49 | }) |
83 | }) | 50 | }) |
84 | 51 | ||
85 | async function run () { | 52 | async function run (url: string, username: string, password: string) { |
86 | const res = await getClient(program[ 'url' ]) | 53 | const resClient = await getClient(program[ 'url' ]) |
87 | const client = { | 54 | const client = { |
88 | id: res.body.client_id, | 55 | id: resClient.body.client_id, |
89 | secret: res.body.client_secret | 56 | secret: resClient.body.client_secret |
90 | } | 57 | } |
91 | 58 | ||
92 | const user = { | 59 | const user = { username, password } |
93 | username: program[ 'username' ], | ||
94 | password: program[ 'password' ] | ||
95 | } | ||
96 | 60 | ||
97 | let accessToken: string | 61 | let accessToken: string |
98 | try { | 62 | try { |
99 | const res2 = await login(program[ 'url' ], client, user) | 63 | const res = await login(url, client, user) |
100 | accessToken = res2.body.access_token | 64 | accessToken = res.body.access_token |
101 | } catch (err) { | 65 | } catch (err) { |
102 | throw new Error('Cannot authenticate. Please check your username/password.') | 66 | throw new Error('Cannot authenticate. Please check your username/password.') |
103 | } | 67 | } |
@@ -108,26 +72,32 @@ async function run () { | |||
108 | 72 | ||
109 | const videoAttributes = { | 73 | const videoAttributes = { |
110 | name: program['videoName'], | 74 | name: program['videoName'], |
111 | category: program['category'], | 75 | category: program['category'] || undefined, |
112 | channelId: program['channelId'], | 76 | channelId: program['channelId'], |
113 | licence: program['licence'], | 77 | licence: program['licence'] || undefined, |
114 | language: program['language'], | 78 | language: program['language'] || undefined, |
115 | nsfw: program['nsfw'], | 79 | nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false, |
116 | description: program['videoDescription'], | 80 | description: program['videoDescription'] || '', |
117 | tags: program['tags'], | 81 | tags: program['tags'] || [], |
118 | commentsEnabled: program['commentsEnabled'], | 82 | commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true, |
83 | downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true, | ||
119 | fixture: program['file'], | 84 | fixture: program['file'], |
120 | thumbnailfile: program['thumbnail'], | 85 | thumbnailfile: program['thumbnail'], |
121 | previewfile: program['preview'], | 86 | previewfile: program['preview'], |
122 | waitTranscoding: true, | 87 | waitTranscoding: true, |
123 | privacy: program['privacy'], | 88 | privacy: program['privacy'] || VideoPrivacy.PUBLIC, |
124 | support: undefined | 89 | support: undefined |
125 | } | 90 | } |
126 | 91 | ||
127 | await uploadVideo(program[ 'url' ], accessToken, videoAttributes) | 92 | try { |
128 | 93 | await uploadVideo(url, accessToken, videoAttributes) | |
129 | console.log(`Video ${program['videoName']} uploaded.`) | 94 | console.log(`Video ${program['videoName']} uploaded.`) |
130 | process.exit(0) | 95 | process.exit(0) |
96 | } catch (err) { | ||
97 | console.log('coucou') | ||
98 | console.error(require('util').inspect(err)) | ||
99 | process.exit(-1) | ||
100 | } | ||
131 | } | 101 | } |
132 | 102 | ||
133 | // ---------------------------------------------------------------------------- | 103 | // ---------------------------------------------------------------------------- |