"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveFilesIfPrivacyChanged = exports.isVideoInPublicDirectory = exports.isVideoInPrivateDirectory = exports.setVideoPrivacy = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const logger_1 = require("../helpers/logger");
const constants_1 = require("../initializers/constants");
const object_storage_1 = require("./object-storage");
function setVideoPrivacy(video, newPrivacy) {
    if (video.privacy === 3 && newPrivacy !== 3) {
        video.publishedAt = new Date();
    }
    video.privacy = newPrivacy;
}
exports.setVideoPrivacy = setVideoPrivacy;
function isVideoInPrivateDirectory(privacy) {
    return privacy === 3 || privacy === 4;
}
exports.isVideoInPrivateDirectory = isVideoInPrivateDirectory;
function isVideoInPublicDirectory(privacy) {
    return !isVideoInPrivateDirectory(privacy);
}
exports.isVideoInPublicDirectory = isVideoInPublicDirectory;
function moveFilesIfPrivacyChanged(video, oldPrivacy) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (isVideoInPublicDirectory(video.privacy) && isVideoInPrivateDirectory(oldPrivacy)) {
            yield moveFiles({ type: 'private-to-public', video });
            return true;
        }
        if (isVideoInPrivateDirectory(video.privacy) && isVideoInPublicDirectory(oldPrivacy)) {
            yield moveFiles({ type: 'public-to-private', video });
            return true;
        }
        return false;
    });
}
exports.moveFilesIfPrivacyChanged = moveFilesIfPrivacyChanged;
function moveFiles(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { type, video } = options;
        for (const file of video.VideoFiles) {
            if (file.storage === 0) {
                yield moveWebTorrentFileOnFS(type, video, file);
            }
            else {
                yield (0, object_storage_1.updateWebTorrentFileACL)(video, file);
            }
        }
        const hls = video.getHLSPlaylist();
        if (hls) {
            if (hls.storage === 0) {
                yield moveHLSFilesOnFS(type, video);
            }
            else {
                yield (0, object_storage_1.updateHLSFilesACL)(hls);
            }
        }
    });
}
function moveWebTorrentFileOnFS(type, video, file) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const directories = getWebTorrentDirectories(type);
        const source = (0, path_1.join)(directories.old, file.filename);
        const destination = (0, path_1.join)(directories.new, file.filename);
        try {
            logger_1.logger.info('Moving WebTorrent files of %s after privacy change (%s -> %s).', video.uuid, source, destination);
            yield (0, fs_extra_1.move)(source, destination);
        }
        catch (err) {
            logger_1.logger.error('Cannot move webtorrent file %s to %s after privacy change', source, destination, { err });
        }
    });
}
function getWebTorrentDirectories(moveType) {
    if (moveType === 'private-to-public') {
        return { old: constants_1.DIRECTORIES.VIDEOS.PRIVATE, new: constants_1.DIRECTORIES.VIDEOS.PUBLIC };
    }
    return { old: constants_1.DIRECTORIES.VIDEOS.PUBLIC, new: constants_1.DIRECTORIES.VIDEOS.PRIVATE };
}
function moveHLSFilesOnFS(type, video) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const directories = getHLSDirectories(type);
        const source = (0, path_1.join)(directories.old, video.uuid);
        const destination = (0, path_1.join)(directories.new, video.uuid);
        try {
            logger_1.logger.info('Moving HLS files of %s after privacy change (%s -> %s).', video.uuid, source, destination);
            yield (0, fs_extra_1.move)(source, destination);
        }
        catch (err) {
            logger_1.logger.error('Cannot move HLS file %s to %s after privacy change', source, destination, { err });
        }
    });
}
function getHLSDirectories(moveType) {
    if (moveType === 'private-to-public') {
        return { old: constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, new: constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC };
    }
    return { old: constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, new: constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE };
}
//# sourceMappingURL=video-privacy.js.map