aboutsummaryrefslogtreecommitdiffhomepage
path: root/apps/peertube-cli/src/peertube-upload.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /apps/peertube-cli/src/peertube-upload.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
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}