"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processVideoFileImport = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = require("fs-extra");
const webtorrent_1 = require("../../../helpers/webtorrent");
const config_1 = require("../../../initializers/config");
const videos_1 = require("../../activitypub/videos");
const paths_1 = require("../../paths");
const video_1 = require("../../video");
const video_path_manager_1 = require("../../video-path-manager");
const video_2 = require("../../../models/video/video");
const video_file_1 = require("../../../models/video/video-file");
const core_utils_1 = require("../../../../shared/core-utils");
const ffmpeg_1 = require("../../../helpers/ffmpeg");
const logger_1 = require("../../../helpers/logger");
const job_queue_1 = require("../job-queue");
function processVideoFileImport(job) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const payload = job.data;
        logger_1.logger.info('Processing video file import in job %s.', job.id);
        const video = yield video_2.VideoModel.loadFull(payload.videoUUID);
        if (!video) {
            logger_1.logger.info('Do not process job %d, video does not exist.', job.id);
            return undefined;
        }
        yield updateVideoFile(video, payload.filePath);
        if (config_1.CONFIG.OBJECT_STORAGE.ENABLED) {
            yield job_queue_1.JobQueue.Instance.createJob(yield (0, video_1.buildMoveToObjectStorageJob)({ video, previousVideoState: video.state }));
        }
        else {
            yield (0, videos_1.federateVideoIfNeeded)(video, false);
        }
        return video;
    });
}
exports.processVideoFileImport = processVideoFileImport;
function updateVideoFile(video, inputFilePath) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { resolution } = yield (0, ffmpeg_1.getVideoStreamDimensionsInfo)(inputFilePath);
        const { size } = yield (0, fs_extra_1.stat)(inputFilePath);
        const fps = yield (0, ffmpeg_1.getVideoStreamFPS)(inputFilePath);
        const fileExt = (0, core_utils_1.getLowercaseExtension)(inputFilePath);
        const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution);
        if (currentVideoFile) {
            yield video.removeWebTorrentFile(currentVideoFile);
            video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile);
            yield currentVideoFile.destroy();
        }
        const newVideoFile = new video_file_1.VideoFileModel({
            resolution,
            extname: fileExt,
            filename: (0, paths_1.generateWebTorrentVideoFilename)(resolution, fileExt),
            storage: 0,
            size,
            fps,
            videoId: video.id
        });
        const outputPath = video_path_manager_1.VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile);
        yield (0, fs_extra_1.copy)(inputFilePath, outputPath);
        video.VideoFiles.push(newVideoFile);
        yield (0, webtorrent_1.createTorrentAndSetInfoHash)(video, newVideoFile);
        yield newVideoFile.save();
    });
}
//# sourceMappingURL=video-file-import.js.map