2021-06-25 10:48:27 -05:00
|
|
|
import { program } from 'commander'
|
2018-08-27 06:28:49 -05:00
|
|
|
import { access, constants } from 'fs-extra'
|
2017-09-07 10:58:09 -05:00
|
|
|
import { isAbsolute } from 'path'
|
2021-07-13 04:44:16 -05:00
|
|
|
import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
|
2017-09-07 10:58:09 -05:00
|
|
|
|
2019-06-13 06:53:28 -05:00
|
|
|
let command = program
|
2018-09-13 07:27:44 -05:00
|
|
|
.name('upload')
|
2019-06-13 06:53:28 -05:00
|
|
|
|
|
|
|
command = buildCommonVideoOptions(command)
|
|
|
|
|
|
|
|
command
|
2017-09-07 10:58:09 -05:00
|
|
|
.option('-u, --url <url>', 'Server url')
|
2018-02-12 05:48:58 -06:00
|
|
|
.option('-U, --username <username>', 'Username')
|
|
|
|
.option('-p, --password <token>', 'Password')
|
2018-02-15 07:46:26 -06:00
|
|
|
.option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
|
2018-02-15 11:40:24 -06:00
|
|
|
.option('-v, --preview <previewPath>', 'Preview path')
|
2017-09-07 10:58:09 -05:00
|
|
|
.option('-f, --file <file>', 'Video absolute file path')
|
|
|
|
.parse(process.argv)
|
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
const options = command.opts()
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
getServerCredentials(command)
|
|
|
|
.then(({ url, username, password }) => {
|
2021-02-03 02:33:05 -06:00
|
|
|
if (!options.videoName || !options.file) {
|
|
|
|
if (!options.videoName) console.error('--video-name is required.')
|
|
|
|
if (!options.file) console.error('--file is required.')
|
2018-09-13 07:27:44 -05:00
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-09-13 07:27:44 -05:00
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
if (isAbsolute(options.file) === false) {
|
2019-07-11 10:23:24 -05:00
|
|
|
console.error('File path should be absolute.')
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-09-13 07:27:44 -05:00
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
run(url, username, password).catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
})
|
2020-01-31 09:56:52 -06:00
|
|
|
.catch(err => console.error(err))
|
2017-09-07 10:58:09 -05:00
|
|
|
|
2019-04-25 06:55:28 -05:00
|
|
|
async function run (url: string, username: string, password: string) {
|
2021-07-13 04:44:16 -05:00
|
|
|
const server = buildServer(url)
|
|
|
|
await assignToken(server, username, password)
|
2017-09-07 10:58:09 -05:00
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
await access(options.file, constants.F_OK)
|
2018-02-12 05:48:58 -06:00
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
console.log('Uploading %s video...', options.videoName)
|
2017-09-07 10:58:09 -05:00
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
const baseAttributes = await buildVideoAttributesFromCommander(server, program)
|
|
|
|
|
|
|
|
const attributes = {
|
|
|
|
...baseAttributes,
|
2019-06-13 06:53:28 -05:00
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
fixture: options.file,
|
|
|
|
thumbnailfile: options.thumbnail,
|
|
|
|
previewfile: options.preview
|
2021-07-15 03:02:54 -05:00
|
|
|
}
|
2018-02-12 05:48:58 -06:00
|
|
|
|
2019-04-25 06:55:28 -05:00
|
|
|
try {
|
2021-07-16 02:04:35 -05:00
|
|
|
await server.videos.upload({ attributes })
|
2021-02-03 02:33:05 -06:00
|
|
|
console.log(`Video ${options.videoName} uploaded.`)
|
2019-04-25 06:55:28 -05:00
|
|
|
process.exit(0)
|
|
|
|
} catch (err) {
|
2022-01-18 02:19:59 -06:00
|
|
|
const message = err.message || ''
|
|
|
|
if (message.includes('413')) {
|
2022-01-19 03:50:34 -06:00
|
|
|
console.error('Aborted: user quota is exceeded or video file is too big for this PeerTube instance.')
|
2022-01-18 02:19:59 -06:00
|
|
|
} else {
|
|
|
|
console.error(require('util').inspect(err))
|
|
|
|
}
|
|
|
|
|
2019-04-25 06:55:28 -05:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
2018-02-12 05:48:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|