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

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