Don't stuck on active jobs
[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
6 const accessPromise = promisify(access)
7
8 import { uploadVideo } from '../tests/utils/index'
9
10 program
11   .option('-u, --url <url>', 'Server url')
12   .option('-a, --access-token <token>', 'Access token')
13   .option('-n, --name <name>', 'Video name')
14   .option('-N, --nsfw', 'Video is Not Safe For Work')
15   .option('-c, --category <category number>', 'Category number')
16   .option('-l, --licence <licence number>', 'Licence number')
17   .option('-L, --language <language number>', 'Language number')
18   .option('-d, --description <description>', 'Video description')
19   .option('-t, --tags <tags>', 'Video tags', list)
20   .option('-f, --file <file>', 'Video absolute file path')
21   .parse(process.argv)
22
23 if (!program['tags']) program['tags'] = []
24 if (!program['nsfw']) program['nsfw'] = false
25
26 if (
27   !program['url'] ||
28   !program['accessToken'] ||
29   !program['name'] ||
30   !program['category'] ||
31   !program['licence'] ||
32   !program['description'] ||
33   !program['file']
34 ) {
35   throw new Error('All arguments but tags, language and nsfw are required.')
36 }
37
38 if (isAbsolute(program['file']) === false) {
39   throw new Error('File path should be absolute.')
40 }
41
42 accessPromise(program['file'], constants.F_OK)
43   .then(() => {
44     return upload(
45       program['url'],
46       program['accessToken'],
47       program['name'],
48       program['category'],
49       program['licence'],
50       program['language'],
51       program['nsfw'],
52       program['description'],
53       program['tags'],
54       program['file']
55     )
56   })
57   .then(() => process.exit(0))
58   .catch(err => {
59     console.error(err)
60     process.exit(-1)
61   })
62
63 // ----------------------------------------------------------------------------
64
65 function list (val) {
66   return val.split(',')
67 }
68
69 function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) {
70   console.log('Uploading %s video...', program['name'])
71
72   const videoAttributes = {
73     name,
74     category,
75     licence,
76     language,
77     nsfw,
78     description,
79     tags,
80     fixture
81   }
82   return uploadVideo(url, accessToken, videoAttributes).then(() => {
83     console.log(`Video ${name} uploaded.`)
84   })
85 }