]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-upload.ts
Fix thumbnail when listing my videos
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-upload.ts
CommitLineData
8df87ce7 1import * as program from 'commander'
c9d5c64f 2import { access, constants } from 'fs-extra'
8df87ce7 3import { isAbsolute } from 'path'
94565d52
C
4import { getClient, login } from '../../shared/extra-utils'
5import { uploadVideo } from '../../shared/extra-utils/'
72de91cb 6import { VideoPrivacy } from '../../shared/models/videos'
2b4dd7e2 7import { getRemoteObjectOrDie, getSettings } from './cli'
8df87ce7 8
8df87ce7 9program
8704acf4 10 .name('upload')
8df87ce7 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')
8704acf4 15 .option('-P, --privacy <privacy_number>', 'Privacy')
8df87ce7 16 .option('-N, --nsfw', 'Video is Not Safe For Work')
8704acf4 17 .option('-c, --category <category_number>', 'Category number')
c01cfce4 18 .option('-C, --channel-id <channel_id>', 'Channel ID')
a87d467a 19 .option('-m, --comments-enabled', 'Enable comments')
8704acf4
RK
20 .option('-l, --licence <licence_number>', 'Licence number')
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
8704acf4
RK
29getSettings()
30 .then(settings => {
2b4dd7e2 31 const { url, username, password } = getRemoteObjectOrDie(program, settings)
8704acf4 32
2b4dd7e2
C
33 if (!program['videoName'] || !program['file'] || !program['channelId']) {
34 if (!program['videoName']) console.error('--video-name is required.')
35 if (!program['file']) console.error('--file is required.')
36 if (!program['channelId']) console.error('--channel-id is required.')
8704acf4 37
8704acf4
RK
38 process.exit(-1)
39 }
40
41 if (isAbsolute(program['file']) === false) {
42 console.error('File path should be absolute.')
43 process.exit(-1)
44 }
45
2b4dd7e2 46 run(url, username, password).catch(err => {
542e73a2
C
47 console.error(err)
48 process.exit(-1)
49 })
8704acf4 50 })
8df87ce7 51
2b4dd7e2
C
52async function run (url: string, username: string, password: string) {
53 const resClient = await getClient(program[ 'url' ])
a87d467a 54 const client = {
2b4dd7e2
C
55 id: resClient.body.client_id,
56 secret: resClient.body.client_secret
a87d467a 57 }
8df87ce7 58
2b4dd7e2 59 const user = { username, password }
a87d467a 60
542e73a2
C
61 let accessToken: string
62 try {
2b4dd7e2
C
63 const res = await login(url, client, user)
64 accessToken = res.body.access_token
542e73a2
C
65 } catch (err) {
66 throw new Error('Cannot authenticate. Please check your username/password.')
67 }
8df87ce7 68
62689b94 69 await access(program[ 'file' ], constants.F_OK)
a87d467a
C
70
71 console.log('Uploading %s video...', program[ 'videoName' ])
8df87ce7 72
26b41517 73 const videoAttributes = {
a87d467a 74 name: program['videoName'],
2b4dd7e2 75 category: program['category'] || undefined,
26b41517 76 channelId: program['channelId'],
2b4dd7e2
C
77 licence: program['licence'] || undefined,
78 language: program['language'] || undefined,
79 nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false,
80 description: program['videoDescription'] || '',
81 tags: program['tags'] || [],
82 commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true,
83 downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true,
2422c46b 84 fixture: program['file'],
d02911fa
C
85 thumbnailfile: program['thumbnail'],
86 previewfile: program['preview'],
2186386c 87 waitTranscoding: true,
2b4dd7e2 88 privacy: program['privacy'] || VideoPrivacy.PUBLIC,
ad34cfc8 89 support: undefined
8df87ce7 90 }
a87d467a 91
2b4dd7e2
C
92 try {
93 await uploadVideo(url, accessToken, videoAttributes)
94 console.log(`Video ${program['videoName']} uploaded.`)
95 process.exit(0)
96 } catch (err) {
97 console.log('coucou')
98 console.error(require('util').inspect(err))
99 process.exit(-1)
100 }
a87d467a
C
101}
102
103// ----------------------------------------------------------------------------
104
105function list (val) {
106 return val.split(',')
8df87ce7 107}