ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

135 lines
4.1 KiB

  1. import { program } from 'commander'
  2. import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
  3. import { CONFIG } from '@server/initializers/config.js'
  4. import { initDatabaseModels } from '@server/initializers/database.js'
  5. import { JobQueue } from '@server/lib/job-queue/index.js'
  6. import { moveToExternalStorageState, moveToFileSystemState } from '@server/lib/video-state.js'
  7. import { VideoModel } from '@server/models/video/video.js'
  8. import { VideoState, FileStorage } from '@peertube/peertube-models'
  9. import { MStreamingPlaylist, MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
  10. program
  11. .description('Move videos to another storage.')
  12. .option('-o, --to-object-storage', 'Move videos in object storage')
  13. .option('-f, --to-file-system', 'Move videos to file system')
  14. .option('-v, --video [videoUUID]', 'Move a specific video')
  15. .option('-a, --all-videos', 'Migrate all videos')
  16. .parse(process.argv)
  17. const options = program.opts()
  18. if (!options['toObjectStorage'] && !options['toFileSystem']) {
  19. console.error('You need to choose where to send video files using --to-object-storage or --to-file-system.')
  20. process.exit(-1)
  21. }
  22. if (!options['video'] && !options['allVideos']) {
  23. console.error('You need to choose which videos to move.')
  24. process.exit(-1)
  25. }
  26. if (options['toObjectStorage'] && !CONFIG.OBJECT_STORAGE.ENABLED) {
  27. console.error('Object storage is not enabled on this instance.')
  28. process.exit(-1)
  29. }
  30. run()
  31. .then(() => process.exit(0))
  32. .catch(err => {
  33. console.error(err)
  34. process.exit(-1)
  35. })
  36. async function run () {
  37. await initDatabaseModels(true)
  38. JobQueue.Instance.init()
  39. let ids: number[] = []
  40. if (options['video']) {
  41. const video = await VideoModel.load(toCompleteUUID(options['video']))
  42. if (!video) {
  43. console.error('Unknown video ' + options['video'])
  44. process.exit(-1)
  45. }
  46. if (video.remote === true) {
  47. console.error('Cannot process a remote video')
  48. process.exit(-1)
  49. }
  50. if (video.isLive) {
  51. console.error('Cannot process live video')
  52. process.exit(-1)
  53. }
  54. if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE || video.state === VideoState.TO_MOVE_TO_FILE_SYSTEM) {
  55. console.error('This video is already being moved to external storage/file system')
  56. process.exit(-1)
  57. }
  58. ids.push(video.id)
  59. } else {
  60. ids = await VideoModel.listLocalIds()
  61. }
  62. for (const id of ids) {
  63. const videoFull = await VideoModel.loadFull(id)
  64. if (videoFull.isLive) continue
  65. if (options['toObjectStorage']) {
  66. await createMoveJobIfNeeded({
  67. video: videoFull,
  68. type: 'to object storage',
  69. canProcessVideo: (files, hls) => {
  70. return files.some(f => f.storage === FileStorage.FILE_SYSTEM) || hls?.storage === FileStorage.FILE_SYSTEM
  71. },
  72. handler: () => moveToExternalStorageState({ video: videoFull, isNewVideo: false, transaction: undefined })
  73. })
  74. continue
  75. }
  76. if (options['toFileSystem']) {
  77. await createMoveJobIfNeeded({
  78. video: videoFull,
  79. type: 'to file system',
  80. canProcessVideo: (files, hls) => {
  81. return files.some(f => f.storage === FileStorage.OBJECT_STORAGE) || hls?.storage === FileStorage.OBJECT_STORAGE
  82. },
  83. handler: () => moveToFileSystemState({ video: videoFull, isNewVideo: false, transaction: undefined })
  84. })
  85. }
  86. }
  87. }
  88. async function createMoveJobIfNeeded (options: {
  89. video: MVideoFullLight
  90. type: 'to object storage' | 'to file system'
  91. canProcessVideo: (files: MVideoFile[], hls: MStreamingPlaylist) => boolean
  92. handler: () => Promise<any>
  93. }) {
  94. const { video, type, canProcessVideo, handler } = options
  95. const files = video.VideoFiles || []
  96. const hls = video.getHLSPlaylist()
  97. if (canProcessVideo(files, hls)) {
  98. console.log(`Moving ${type} video ${video.name}`)
  99. const success = await handler()
  100. if (!success) {
  101. console.error(
  102. `Cannot create move ${type} for ${video.name}: job creation may have failed or there may be pending transcoding jobs for this video`
  103. )
  104. } else {
  105. console.log(`Created job ${type} for ${video.name}.`)
  106. }
  107. }
  108. }