Add player mode in watch/embed urls
[oweals/peertube.git] / server / tools / peertube-upload.ts
1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getClient, login } from '../../shared/utils'
5 import { uploadVideo } from '../../shared/utils/'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 import { netrc, getSettings } from './cli'
8
9 program
10   .name('upload')
11   .option('-u, --url <url>', 'Server url')
12   .option('-U, --username <username>', 'Username')
13   .option('-p, --password <token>', 'Password')
14   .option('-n, --video-name <name>', 'Video name')
15   .option('-P, --privacy <privacy_number>', 'Privacy')
16   .option('-N, --nsfw', 'Video is Not Safe For Work')
17   .option('-c, --category <category_number>', 'Category number')
18   .option('-C, --channel-id <channel_id>', 'Channel ID')
19   .option('-m, --comments-enabled', 'Enable comments')
20   .option('-l, --licence <licence_number>', 'Licence number')
21   .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
22   .option('-d, --video-description <description>', 'Video description')
23   .option('-t, --tags <tags>', 'Video tags', list)
24   .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
25   .option('-v, --preview <previewPath>', 'Preview path')
26   .option('-f, --file <file>', 'Video absolute file path')
27   .parse(process.argv)
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()
35   .then(settings => {
36     if (
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
50     if (
51       (!program['url'] ||
52       !program['username'] ||
53       !program['password']) &&
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
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)
72     }
73
74     if (isAbsolute(program['file']) === false) {
75       console.error('File path should be absolute.')
76       process.exit(-1)
77     }
78
79     run().catch(err => {
80       console.error(err)
81       process.exit(-1)
82     })
83   })
84
85 async function run () {
86   const res = await getClient(program[ 'url' ])
87   const client = {
88     id: res.body.client_id,
89     secret: res.body.client_secret
90   }
91
92   const user = {
93     username: program[ 'username' ],
94     password: program[ 'password' ]
95   }
96
97   let accessToken: string
98   try {
99     const res2 = await login(program[ 'url' ], client, user)
100     accessToken = res2.body.access_token
101   } catch (err) {
102     throw new Error('Cannot authenticate. Please check your username/password.')
103   }
104
105   await access(program[ 'file' ], constants.F_OK)
106
107   console.log('Uploading %s video...', program[ 'videoName' ])
108
109   const videoAttributes = {
110     name: program['videoName'],
111     category: program['category'],
112     channelId: program['channelId'],
113     licence: program['licence'],
114     language: program['language'],
115     nsfw: program['nsfw'],
116     description: program['videoDescription'],
117     tags: program['tags'],
118     commentsEnabled: program['commentsEnabled'],
119     fixture: program['file'],
120     thumbnailfile: program['thumbnail'],
121     previewfile: program['preview'],
122     waitTranscoding: true,
123     privacy: program['privacy'],
124     support: undefined
125   }
126
127   await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
128
129   console.log(`Video ${program['videoName']} uploaded.`)
130   process.exit(0)
131 }
132
133 // ----------------------------------------------------------------------------
134
135 function list (val) {
136   return val.split(',')
137 }