aboutsummaryrefslogtreecommitdiffhomepage
path: root/apps/peertube-cli/src/peertube-upload.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/peertube-cli/src/peertube-upload.ts')
-rw-r--r--apps/peertube-cli/src/peertube-upload.ts167
1 files changed, 167 insertions, 0 deletions
diff --git a/apps/peertube-cli/src/peertube-upload.ts b/apps/peertube-cli/src/peertube-upload.ts
new file mode 100644
index 000000000..443f8ce1f
--- /dev/null
+++ b/apps/peertube-cli/src/peertube-upload.ts
@@ -0,0 +1,167 @@
1import { access, constants } from 'fs/promises'
2import { isAbsolute } from 'path'
3import { inspect } from 'util'
4import { Command } from '@commander-js/extra-typings'
5import { VideoPrivacy } from '@peertube/peertube-models'
6import { PeerTubeServer } from '@peertube/peertube-server-commands'
7import { assignToken, buildServer, getServerCredentials, listOptions } from './shared/index.js'
8
9type UploadOptions = {
10 url?: string
11 username?: string
12 password?: string
13 thumbnail?: string
14 preview?: string
15 file?: string
16 videoName?: string
17 category?: string
18 licence?: string
19 language?: string
20 tags?: string
21 nsfw?: true
22 videoDescription?: string
23 privacy?: number
24 channelName?: string
25 noCommentsEnabled?: true
26 support?: string
27 noWaitTranscoding?: true
28 noDownloadEnabled?: true
29}
30
31export function defineUploadProgram () {
32 const program = new Command('upload')
33 .description('Upload a video on a PeerTube instance')
34 .alias('up')
35
36 program
37 .option('-u, --url <url>', 'Server url')
38 .option('-U, --username <username>', 'Username')
39 .option('-p, --password <token>', 'Password')
40 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
41 .option('-v, --preview <previewPath>', 'Preview path')
42 .option('-f, --file <file>', 'Video absolute file path')
43 .option('-n, --video-name <name>', 'Video name')
44 .option('-c, --category <category_number>', 'Category number')
45 .option('-l, --licence <licence_number>', 'Licence number')
46 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
47 .option('-t, --tags <tags>', 'Video tags', listOptions)
48 .option('-N, --nsfw', 'Video is Not Safe For Work')
49 .option('-d, --video-description <description>', 'Video description')
50 .option('-P, --privacy <privacy_number>', 'Privacy', parseInt)
51 .option('-C, --channel-name <channel_name>', 'Channel name')
52 .option('--no-comments-enabled', 'Disable video comments')
53 .option('-s, --support <support>', 'Video support text')
54 .option('--no-wait-transcoding', 'Do not wait transcoding before publishing the video')
55 .option('--no-download-enabled', 'Disable video download')
56 .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
57 .action(async options => {
58 try {
59 const { url, username, password } = await getServerCredentials(options)
60
61 if (!options.videoName || !options.file) {
62 if (!options.videoName) console.error('--video-name is required.')
63 if (!options.file) console.error('--file is required.')
64
65 process.exit(-1)
66 }
67
68 if (isAbsolute(options.file) === false) {
69 console.error('File path should be absolute.')
70 process.exit(-1)
71 }
72
73 await run({ ...options, url, username, password })
74 } catch (err) {
75 console.error('Cannot upload video: ' + err.message)
76 process.exit(-1)
77 }
78 })
79
80 return program
81}
82
83// ---------------------------------------------------------------------------
84// Private
85// ---------------------------------------------------------------------------
86
87async function run (options: UploadOptions) {
88 const { url, username, password } = options
89
90 const server = buildServer(url)
91 await assignToken(server, username, password)
92
93 await access(options.file, constants.F_OK)
94
95 console.log('Uploading %s video...', options.videoName)
96
97 const baseAttributes = await buildVideoAttributesFromCommander(server, options)
98
99 const attributes = {
100 ...baseAttributes,
101
102 fixture: options.file,
103 thumbnailfile: options.thumbnail,
104 previewfile: options.preview
105 }
106
107 try {
108 await server.videos.upload({ attributes })
109 console.log(`Video ${options.videoName} uploaded.`)
110 process.exit(0)
111 } catch (err) {
112 const message = err.message || ''
113 if (message.includes('413')) {
114 console.error('Aborted: user quota is exceeded or video file is too big for this PeerTube instance.')
115 } else {
116 console.error(inspect(err))
117 }
118
119 process.exit(-1)
120 }
121}
122
123async function buildVideoAttributesFromCommander (server: PeerTubeServer, options: UploadOptions, defaultAttributes: any = {}) {
124 const defaultBooleanAttributes = {
125 nsfw: false,
126 commentsEnabled: true,
127 downloadEnabled: true,
128 waitTranscoding: true
129 }
130
131 const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}
132
133 for (const key of Object.keys(defaultBooleanAttributes)) {
134 if (options[key] !== undefined) {
135 booleanAttributes[key] = options[key]
136 } else if (defaultAttributes[key] !== undefined) {
137 booleanAttributes[key] = defaultAttributes[key]
138 } else {
139 booleanAttributes[key] = defaultBooleanAttributes[key]
140 }
141 }
142
143 const videoAttributes = {
144 name: options.videoName || defaultAttributes.name,
145 category: options.category || defaultAttributes.category || undefined,
146 licence: options.licence || defaultAttributes.licence || undefined,
147 language: options.language || defaultAttributes.language || undefined,
148 privacy: options.privacy || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
149 support: options.support || defaultAttributes.support || undefined,
150 description: options.videoDescription || defaultAttributes.description || undefined,
151 tags: options.tags || defaultAttributes.tags || undefined
152 }
153
154 Object.assign(videoAttributes, booleanAttributes)
155
156 if (options.channelName) {
157 const videoChannel = await server.channels.get({ channelName: options.channelName })
158
159 Object.assign(videoAttributes, { channelId: videoChannel.id })
160
161 if (!videoAttributes.support && videoChannel.support) {
162 Object.assign(videoAttributes, { support: videoChannel.support })
163 }
164 }
165
166 return videoAttributes
167}