2
1

peertube-upload.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import * as program from 'commander'
  2. import { access, constants } from 'fs-extra'
  3. import { isAbsolute } from 'path'
  4. import { getClient, login } from '../../shared/extra-utils'
  5. import { uploadVideo } from '../../shared/extra-utils/'
  6. import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
  7. let command = program
  8. .name('upload')
  9. command = buildCommonVideoOptions(command)
  10. command
  11. .option('-u, --url <url>', 'Server url')
  12. .option('-U, --username <username>', 'Username')
  13. .option('-p, --password <token>', 'Password')
  14. .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
  15. .option('-v, --preview <previewPath>', 'Preview path')
  16. .option('-f, --file <file>', 'Video absolute file path')
  17. .parse(process.argv)
  18. Promise.all([ getSettings(), getNetrc() ])
  19. .then(([ settings, netrc ]) => {
  20. const { url, username, password } = getRemoteObjectOrDie(program, settings, netrc)
  21. if (!program[ 'videoName' ] || !program[ 'file' ]) {
  22. if (!program[ 'videoName' ]) console.error('--video-name is required.')
  23. if (!program[ 'file' ]) console.error('--file is required.')
  24. process.exit(-1)
  25. }
  26. if (isAbsolute(program[ 'file' ]) === false) {
  27. console.error('File path should be absolute.')
  28. process.exit(-1)
  29. }
  30. run(url, username, password).catch(err => {
  31. console.error(err)
  32. process.exit(-1)
  33. })
  34. })
  35. async function run (url: string, username: string, password: string) {
  36. const resClient = await getClient(url)
  37. const client = {
  38. id: resClient.body.client_id,
  39. secret: resClient.body.client_secret
  40. }
  41. const user = { username, password }
  42. let accessToken: string
  43. try {
  44. const res = await login(url, client, user)
  45. accessToken = res.body.access_token
  46. } catch (err) {
  47. throw new Error('Cannot authenticate. Please check your username/password.')
  48. }
  49. await access(program[ 'file' ], constants.F_OK)
  50. console.log('Uploading %s video...', program[ 'videoName' ])
  51. const defaultAttributes = {
  52. tags: command[ 'tags' ],
  53. description: command[ 'videoDescription' ]
  54. }
  55. const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes)
  56. Object.assign(videoAttributes, {
  57. fixture: program[ 'file' ],
  58. thumbnailfile: program[ 'thumbnail' ],
  59. previewfile: program[ 'preview' ]
  60. })
  61. try {
  62. await uploadVideo(url, accessToken, videoAttributes)
  63. console.log(`Video ${program[ 'videoName' ]} uploaded.`)
  64. process.exit(0)
  65. } catch (err) {
  66. console.error(require('util').inspect(err))
  67. process.exit(-1)
  68. }
  69. }
  70. // ----------------------------------------------------------------------------