peertube-upload.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { registerTSPaths } from '../helpers/register-ts-paths'
  2. registerTSPaths()
  3. import * as program from 'commander'
  4. import { access, constants } from 'fs-extra'
  5. import { isAbsolute } from 'path'
  6. import { getAccessToken } from '../../shared/extra-utils'
  7. import { uploadVideo } from '../../shared/extra-utils/'
  8. import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
  9. let command = program
  10. .name('upload')
  11. command = buildCommonVideoOptions(command)
  12. command
  13. .option('-u, --url <url>', 'Server url')
  14. .option('-U, --username <username>', 'Username')
  15. .option('-p, --password <token>', 'Password')
  16. .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
  17. .option('-v, --preview <previewPath>', 'Preview path')
  18. .option('-f, --file <file>', 'Video absolute file path')
  19. .parse(process.argv)
  20. getServerCredentials(command)
  21. .then(({ url, username, password }) => {
  22. if (!program[ 'videoName' ] || !program[ 'file' ]) {
  23. if (!program[ 'videoName' ]) console.error('--video-name is required.')
  24. if (!program[ 'file' ]) console.error('--file is required.')
  25. process.exit(-1)
  26. }
  27. if (isAbsolute(program[ 'file' ]) === false) {
  28. console.error('File path should be absolute.')
  29. process.exit(-1)
  30. }
  31. run(url, username, password).catch(err => {
  32. console.error(err)
  33. process.exit(-1)
  34. })
  35. })
  36. async function run (url: string, username: string, password: string) {
  37. const accessToken = await getAccessToken(url, username, password)
  38. await access(program[ 'file' ], constants.F_OK)
  39. console.log('Uploading %s video...', program[ 'videoName' ])
  40. const videoAttributes = await buildVideoAttributesFromCommander(url, program)
  41. Object.assign(videoAttributes, {
  42. fixture: program[ 'file' ],
  43. thumbnailfile: program[ 'thumbnail' ],
  44. previewfile: program[ 'preview' ]
  45. })
  46. try {
  47. await uploadVideo(url, accessToken, videoAttributes)
  48. console.log(`Video ${program[ 'videoName' ]} uploaded.`)
  49. process.exit(0)
  50. } catch (err) {
  51. console.error(require('util').inspect(err))
  52. process.exit(-1)
  53. }
  54. }
  55. // ----------------------------------------------------------------------------