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