peertube-upload.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. .catch(err => console.error(err))
  37. async function run (url: string, username: string, password: string) {
  38. const accessToken = await getAccessToken(url, username, password)
  39. await access(program['file'], constants.F_OK)
  40. console.log('Uploading %s video...', program['videoName'])
  41. const videoAttributes = await buildVideoAttributesFromCommander(url, program)
  42. Object.assign(videoAttributes, {
  43. fixture: program['file'],
  44. thumbnailfile: program['thumbnail'],
  45. previewfile: program['preview']
  46. })
  47. try {
  48. await uploadVideo(url, accessToken, videoAttributes)
  49. console.log(`Video ${program['videoName']} uploaded.`)
  50. process.exit(0)
  51. } catch (err) {
  52. console.error(require('util').inspect(err))
  53. process.exit(-1)
  54. }
  55. }
  56. // ----------------------------------------------------------------------------