create-import-video-file-job.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { program } from 'commander'
  2. import { resolve } from 'path'
  3. import { isUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
  4. import { initDatabaseModels } from '../server/initializers/database'
  5. import { JobQueue } from '../server/lib/job-queue'
  6. import { VideoModel } from '../server/models/video/video'
  7. program
  8. .option('-v, --video [videoUUID]', 'Video UUID')
  9. .option('-i, --import [videoFile]', 'Video file')
  10. .description('Import a video file to replace an already uploaded file or to add a new resolution')
  11. .parse(process.argv)
  12. const options = program.opts()
  13. if (options.video === undefined || options.import === undefined) {
  14. console.error('All parameters are mandatory.')
  15. process.exit(-1)
  16. }
  17. run()
  18. .then(() => process.exit(0))
  19. .catch(err => {
  20. console.error(err)
  21. process.exit(-1)
  22. })
  23. async function run () {
  24. await initDatabaseModels(true)
  25. const uuid = toCompleteUUID(options.video)
  26. if (isUUIDValid(uuid) === false) {
  27. console.error('%s is not a valid video UUID.', options.video)
  28. return
  29. }
  30. const video = await VideoModel.load(uuid)
  31. if (!video) throw new Error('Video not found.')
  32. if (video.isOwned() === false) throw new Error('Cannot import files of a non owned video.')
  33. const dataInput = {
  34. videoUUID: video.uuid,
  35. filePath: resolve(options.import)
  36. }
  37. JobQueue.Instance.init()
  38. await JobQueue.Instance.createJob({ type: 'video-file-import', payload: dataInput })
  39. console.log('Import job for video %s created.', video.uuid)
  40. }