]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/upload.ts
Improve webtorrent import error message when the torrent has multiple
[github/Chocobozzz/PeerTube.git] / server / tools / upload.ts
CommitLineData
8df87ce7 1import * as program from 'commander'
c9d5c64f 2import { access, constants } from 'fs-extra'
8df87ce7 3import { isAbsolute } from 'path'
a87d467a
C
4import { getClient, login } from '../tests/utils'
5import { uploadVideo } from '../tests/utils/index'
72de91cb 6import { VideoPrivacy } from '../../shared/models/videos'
8df87ce7 7
8df87ce7
C
8program
9 .option('-u, --url <url>', 'Server url')
a87d467a
C
10 .option('-U, --username <username>', 'Username')
11 .option('-p, --password <token>', 'Password')
12 .option('-n, --video-name <name>', 'Video name')
320f8d7e 13 .option('-P, --privacy <privacy number>', 'Privacy')
8df87ce7
C
14 .option('-N, --nsfw', 'Video is Not Safe For Work')
15 .option('-c, --category <category number>', 'Category number')
a87d467a 16 .option('-m, --comments-enabled', 'Enable comments')
8df87ce7 17 .option('-l, --licence <licence number>', 'Licence number')
9d3ef9fe 18 .option('-L, --language <language code>', 'Language ISO 639 code (fr or en...)')
a87d467a 19 .option('-d, --video-description <description>', 'Video description')
8df87ce7 20 .option('-t, --tags <tags>', 'Video tags', list)
2422c46b 21 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
02988fdc 22 .option('-v, --preview <previewPath>', 'Preview path')
8df87ce7
C
23 .option('-f, --file <file>', 'Video absolute file path')
24 .parse(process.argv)
25
26if (!program['tags']) program['tags'] = []
27if (!program['nsfw']) program['nsfw'] = false
72de91cb 28if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
a87d467a 29if (!program['commentsEnabled']) program['commentsEnabled'] = false
8df87ce7
C
30
31if (
32 !program['url'] ||
a87d467a
C
33 !program['username'] ||
34 !program['password'] ||
35 !program['videoName'] ||
8df87ce7
C
36 !program['file']
37) {
48e35415
RK
38 if (!program['url']) console.error('--url field is required.')
39 if (!program['username']) console.error('--username field is required.')
40 if (!program['password']) console.error('--password field is required.')
41 if (!program['videoName']) console.error('--video-name field is required.')
42 if (!program['file']) console.error('--file field is required.')
a87d467a 43 process.exit(-1)
8df87ce7
C
44}
45
46if (isAbsolute(program['file']) === false) {
a87d467a
C
47 console.error('File path should be absolute.')
48 process.exit(-1)
8df87ce7
C
49}
50
a87d467a 51run().catch(err => console.error(err))
8df87ce7 52
a87d467a
C
53async function run () {
54 const res = await getClient(program[ 'url' ])
55 const client = {
56 id: res.body.client_id,
57 secret: res.body.client_secret
58 }
8df87ce7 59
a87d467a
C
60 const user = {
61 username: program[ 'username' ],
62 password: program[ 'password' ]
63 }
64
65 const res2 = await login(program[ 'url' ], client, user)
66 const accessToken = res2.body.access_token
8df87ce7 67
62689b94 68 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
69
70 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7
C
71
72 const videoAttributes = {
a87d467a
C
73 name: program['videoName'],
74 category: program['category'],
75 licence: program['licence'],
76 language: program['language'],
77 nsfw: program['nsfw'],
78 description: program['videoDescription'],
79 tags: program['tags'],
80 commentsEnabled: program['commentsEnabled'],
2422c46b 81 fixture: program['file'],
d02911fa
C
82 thumbnailfile: program['thumbnail'],
83 previewfile: program['preview'],
2186386c 84 waitTranscoding: true,
320f8d7e 85 privacy: program['privacy'],
ad34cfc8 86 support: undefined
8df87ce7 87 }
a87d467a
C
88
89 await uploadVideo(program['url'], accessToken, videoAttributes)
90
91 console.log(`Video ${program['videoName']} uploaded.`)
92 process.exit(0)
93}
94
95// ----------------------------------------------------------------------------
96
97function list (val) {
98 return val.split(',')
8df87ce7 99}