Fix remote image fetching
[oweals/peertube.git] / server / tools / upload.ts
1 import * as program from 'commander'
2 import { access, constants } from 'fs'
3 import { isAbsolute } from 'path'
4 import { promisify } from 'util'
5 import { getClient, login } from '../tests/utils'
6 import { uploadVideo } from '../tests/utils/index'
7
8 const accessPromise = promisify(access)
9
10 program
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('-N, --nsfw', 'Video is Not Safe For Work')
16   .option('-c, --category <category number>', 'Category number')
17   .option('-m, --comments-enabled', 'Enable comments')
18   .option('-l, --licence <licence number>', 'Licence number')
19   .option('-L, --language <language number>', 'Language number')
20   .option('-d, --video-description <description>', 'Video description')
21   .option('-t, --tags <tags>', 'Video tags', list)
22   .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
23   .option('-v, --preview <previewPath>', 'Preview path')
24   .option('-f, --file <file>', 'Video absolute file path')
25   .parse(process.argv)
26
27 if (!program['tags']) program['tags'] = []
28 if (!program['nsfw']) program['nsfw'] = false
29 if (!program['commentsEnabled']) program['commentsEnabled'] = false
30
31 if (
32   !program['url'] ||
33   !program['username'] ||
34   !program['password'] ||
35   !program['videoName'] ||
36   !program['file']
37 ) {
38   console.error('Url, username, password, name and input file are required.')
39   process.exit(-1)
40 }
41
42 if (isAbsolute(program['file']) === false) {
43   console.error('File path should be absolute.')
44   process.exit(-1)
45 }
46
47 run().catch(err => console.error(err))
48
49 async function run () {
50   const res = await getClient(program[ 'url' ])
51   const client = {
52     id: res.body.client_id,
53     secret: res.body.client_secret
54   }
55
56   const user = {
57     username: program[ 'username' ],
58     password: program[ 'password' ]
59   }
60
61   const res2 = await login(program[ 'url' ], client, user)
62   const accessToken = res2.body.access_token
63
64   await accessPromise(program[ 'file' ], constants.F_OK)
65
66   console.log('Uploading %s video...', program[ 'videoName' ])
67
68   const videoAttributes = {
69     name: program['videoName'],
70     category: program['category'],
71     licence: program['licence'],
72     language: program['language'],
73     nsfw: program['nsfw'],
74     description: program['videoDescription'],
75     tags: program['tags'],
76     commentsEnabled: program['commentsEnabled'],
77     fixture: program['file'],
78     thumbnailfile: program['thumbnailPath'],
79     previewfile: program['previewPath']
80   }
81
82   await uploadVideo(program['url'], accessToken, videoAttributes)
83
84   console.log(`Video ${program['videoName']} uploaded.`)
85   process.exit(0)
86 }
87
88 // ----------------------------------------------------------------------------
89
90 function list (val) {
91   return val.split(',')
92 }