"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 core_utils_1 = require("../../../../shared/core-utils");
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 ffprobe_utils_1 = require("../../../helpers/ffprobe-utils");
const logger_1 = require("../../../helpers/logger");
const video_2 = require("../../../models/video/video");
const video_file_1 = require("../../../models/video/video-file");
function processVideoFileImport(job) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const payload = job.data;
        logger_1.logger.info('Processing video file import in job %d.', job.id);
        const video = yield video_2.VideoModel.loadAndPopulateAccountAndServerAndTags(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 (0, video_1.addMoveToObjectStorageJob)(video);
        }
        else {
            yield (0, videos_1.federateVideoIfNeeded)(video, false);
        }
        return video;
    });
}
exports.processVideoFileImport = processVideoFileImport;
function updateVideoFile(video, inputFilePath) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const { resolution } = yield (0, ffprobe_utils_1.getVideoFileResolution)(inputFilePath);
        const { size } = yield (0, fs_extra_1.stat)(inputFilePath);
        const fps = yield (0, ffprobe_utils_1.getVideoFileFPS)(inputFilePath);
        const fileExt = (0, core_utils_1.getLowercaseExtension)(inputFilePath);
        const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution);
        if (currentVideoFile) {
            yield video.removeWebTorrentFileAndTorrent(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