"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processManageVideoTorrent = void 0;
const tslib_1 = require("tslib");
const video_1 = require("../../../helpers/video");
const webtorrent_1 = require("../../../helpers/webtorrent");
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 video_streaming_playlist_1 = require("../../../models/video/video-streaming-playlist");
const logger_1 = require("../../../helpers/logger");
function processManageVideoTorrent(job) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const payload = job.data;
        logger_1.logger.info('Processing torrent in job %s.', job.id);
        if (payload.action === 'create')
            return doCreateAction(payload);
        if (payload.action === 'update-metadata')
            return doUpdateMetadataAction(payload);
    });
}
exports.processManageVideoTorrent = processManageVideoTorrent;
function doCreateAction(payload) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const [video, file] = yield Promise.all([
            loadVideoOrLog(payload.videoId),
            loadFileOrLog(payload.videoFileId)
        ]);
        if (!video || !file)
            return;
        const fileMutexReleaser = yield video_path_manager_1.VideoPathManager.Instance.lockFiles(video.uuid);
        try {
            yield (0, webtorrent_1.createTorrentAndSetInfoHash)(video, file);
            const refreshedFile = yield video_file_1.VideoFileModel.loadWithVideo(file.id);
            if (!refreshedFile)
                return file.removeTorrent();
            refreshedFile.infoHash = file.infoHash;
            refreshedFile.torrentFilename = file.torrentFilename;
            yield refreshedFile.save();
        }
        finally {
            fileMutexReleaser();
        }
    });
}
function doUpdateMetadataAction(payload) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const [video, streamingPlaylist, file] = yield Promise.all([
            loadVideoOrLog(payload.videoId),
            loadStreamingPlaylistOrLog(payload.streamingPlaylistId),
            loadFileOrLog(payload.videoFileId)
        ]);
        if ((!video && !streamingPlaylist) || !file)
            return;
        const extractedVideo = (0, video_1.extractVideo)(video || streamingPlaylist);
        const fileMutexReleaser = yield video_path_manager_1.VideoPathManager.Instance.lockFiles(extractedVideo.uuid);
        try {
            yield (0, webtorrent_1.updateTorrentMetadata)(video || streamingPlaylist, file);
            yield file.save();
        }
        finally {
            fileMutexReleaser();
        }
    });
}
function loadVideoOrLog(videoId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!videoId)
            return undefined;
        const video = yield video_2.VideoModel.load(videoId);
        if (!video) {
            logger_1.logger.debug('Do not process torrent for video %d: does not exist anymore.', videoId);
        }
        return video;
    });
}
function loadStreamingPlaylistOrLog(streamingPlaylistId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!streamingPlaylistId)
            return undefined;
        const streamingPlaylist = yield video_streaming_playlist_1.VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId);
        if (!streamingPlaylist) {
            logger_1.logger.debug('Do not process torrent for streaming playlist %d: does not exist anymore.', streamingPlaylistId);
        }
        return streamingPlaylist;
    });
}
function loadFileOrLog(videoFileId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!videoFileId)
            return undefined;
        const file = yield video_file_1.VideoFileModel.load(videoFileId);
        if (!file) {
            logger_1.logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId);
        }
        return file;
    });
}
//# sourceMappingURL=manage-video-torrent.js.map