1 import { program } from 'commander'
2 import { CONFIG } from '@server/initializers/config'
3 import { initDatabaseModels } from '@server/initializers/database'
4 import { JobQueue } from '@server/lib/job-queue'
5 import { moveToExternalStorageState } from '@server/lib/video-state'
6 import { VideoModel } from '@server/models/video/video'
7 import { VideoState, VideoStorage } from '@shared/models'
10 .description('Move videos to another storage.')
11 .option('-o, --to-object-storage', 'Move videos in object storage')
12 .option('-v, --video [videoUUID]', 'Move a specific video')
13 .option('-a, --all-videos', 'Migrate all videos')
16 const options = program.opts()
18 if (!options['toObjectStorage']) {
19 console.error('You need to choose where to send video files.')
23 if (!options['video'] && !options['allVideos']) {
24 console.error('You need to choose which videos to move.')
28 if (options['toObjectStorage'] && !CONFIG.OBJECT_STORAGE.ENABLED) {
29 console.error('Object storage is not enabled on this instance.')
34 .then(() => process.exit(0))
35 .catch(err => console.error(err))
37 async function run () {
38 await initDatabaseModels(true)
40 JobQueue.Instance.init(true)
42 let ids: number[] = []
44 if (options['video']) {
45 const video = await VideoModel.load(options['video'])
48 console.error('Unknown video ' + options['video'])
52 if (video.remote === true) {
53 console.error('Cannot process a remote video')
58 console.error('Cannot process live video')
62 if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
63 console.error('This video is already being moved to external storage')
69 ids = await VideoModel.listLocalIds()
72 for (const id of ids) {
73 const videoFull = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
75 const files = videoFull.VideoFiles || []
76 const hls = videoFull.getHLSPlaylist()
78 if (files.some(f => f.storage === VideoStorage.FILE_SYSTEM) || hls?.storage === VideoStorage.FILE_SYSTEM) {
79 console.log('Processing video %s.', videoFull.name)
81 const success = await moveToExternalStorageState({ video: videoFull, isNewVideo: false, transaction: undefined })
85 'Cannot create move job for %s: job creation may have failed or there may be pending transcoding jobs for this video',
91 console.log(`Created move-to-object-storage job for ${videoFull.name}.`)