]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/upload.ts
Update changelog
[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'
8df87ce7
C
7
8const accessPromise = promisify(access)
9
8df87ce7
C
10program
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')
8df87ce7
C
15 .option('-N, --nsfw', 'Video is Not Safe For Work')
16 .option('-c, --category <category number>', 'Category number')
a87d467a 17 .option('-m, --comments-enabled', 'Enable comments')
8df87ce7
C
18 .option('-l, --licence <licence number>', 'Licence number')
19 .option('-L, --language <language number>', 'Language number')
a87d467a 20 .option('-d, --video-description <description>', 'Video description')
8df87ce7 21 .option('-t, --tags <tags>', 'Video tags', list)
2422c46b 22 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
02988fdc 23 .option('-v, --preview <previewPath>', 'Preview path')
8df87ce7
C
24 .option('-f, --file <file>', 'Video absolute file path')
25 .parse(process.argv)
26
27if (!program['tags']) program['tags'] = []
28if (!program['nsfw']) program['nsfw'] = false
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
a87d467a
C
68 await accessPromise(program[ 'file' ], constants.F_OK)
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'],
02988fdc 82 thumbnailfile: program['thumbnailPath'],
ad34cfc8
C
83 previewfile: program['previewPath'],
84 support: undefined
8df87ce7 85 }
a87d467a
C
86
87 await uploadVideo(program['url'], accessToken, videoAttributes)
88
89 console.log(`Video ${program['videoName']} uploaded.`)
90 process.exit(0)
91}
92
93// ----------------------------------------------------------------------------
94
95function list (val) {
96 return val.split(',')
8df87ce7 97}