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