"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const commander_1 = require("commander");
const config_1 = require("../server/initializers/config");
const database_1 = require("../server/initializers/database");
const job_queue_1 = require("../server/lib/job-queue");
const video_state_1 = require("../server/lib/video-state");
const video_1 = require("../server/models/video/video");
commander_1.program
    .description('Move videos to another storage.')
    .option('-o, --to-object-storage', 'Move videos in object storage')
    .option('-v, --video [videoUUID]', 'Move a specific video')
    .option('-a, --all-videos', 'Migrate all videos')
    .parse(process.argv);
const options = commander_1.program.opts();
if (!options['toObjectStorage']) {
    console.error('You need to choose where to send video files.');
    process.exit(-1);
}
if (!options['video'] && !options['allVideos']) {
    console.error('You need to choose which videos to move.');
    process.exit(-1);
}
if (options['toObjectStorage'] && !config_1.CONFIG.OBJECT_STORAGE.ENABLED) {
    console.error('Object storage is not enabled on this instance.');
    process.exit(-1);
}
run()
    .then(() => process.exit(0))
    .catch(err => console.error(err));
function run() {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        yield (0, database_1.initDatabaseModels)(true);
        job_queue_1.JobQueue.Instance.init();
        let ids = [];
        if (options['video']) {
            const video = yield video_1.VideoModel.load(options['video']);
            if (!video) {
                console.error('Unknown video ' + options['video']);
                process.exit(-1);
            }
            if (video.remote === true) {
                console.error('Cannot process a remote video');
                process.exit(-1);
            }
            if (video.isLive) {
                console.error('Cannot process live video');
                process.exit(-1);
            }
            if (video.state === 6) {
                console.error('This video is already being moved to external storage');
                process.exit(-1);
            }
            ids.push(video.id);
        }
        else {
            ids = yield video_1.VideoModel.listLocalIds();
        }
        for (const id of ids) {
            const videoFull = yield video_1.VideoModel.loadFull(id);
            const files = videoFull.VideoFiles || [];
            const hls = videoFull.getHLSPlaylist();
            if (files.some(f => f.storage === 0) || (hls === null || hls === void 0 ? void 0 : hls.storage) === 0) {
                console.log('Processing video %s.', videoFull.name);
                const success = yield (0, video_state_1.moveToExternalStorageState)({ video: videoFull, isNewVideo: false, transaction: undefined });
                if (!success) {
                    console.error('Cannot create move job for %s: job creation may have failed or there may be pending transcoding jobs for this video', videoFull.name);
                }
            }
            console.log(`Created move-to-object-storage job for ${videoFull.name}.`);
        }
    });
}
//# sourceMappingURL=create-move-video-storage-job.js.map