"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildConcatenatedName = exports.cleanupTMPLiveFiles = exports.cleanupUnsavedNormalLive = exports.cleanupAndDestroyPermanentLive = 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 object_storage_1 = require("../object-storage");
const paths_1 = require("../paths");
function buildConcatenatedName(segmentOrPlaylistPath) {
    const num = (0, path_1.basename)(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/);
    return 'concat-' + num[1] + '.ts';
}
exports.buildConcatenatedName = buildConcatenatedName;
function cleanupAndDestroyPermanentLive(video, streamingPlaylist) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        yield cleanupTMPLiveFiles(video, streamingPlaylist);
        yield streamingPlaylist.destroy();
    });
}
exports.cleanupAndDestroyPermanentLive = cleanupAndDestroyPermanentLive;
function cleanupUnsavedNormalLive(video, streamingPlaylist) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const hlsDirectory = (0, paths_1.getLiveDirectory)(video);
        if (streamingPlaylist.storage === 1) {
            yield (0, object_storage_1.removeHLSObjectStorage)(streamingPlaylist.withVideo(video));
        }
        yield (0, fs_extra_1.remove)(hlsDirectory);
        yield streamingPlaylist.destroy();
    });
}
exports.cleanupUnsavedNormalLive = cleanupUnsavedNormalLive;
function cleanupTMPLiveFiles(video, streamingPlaylist) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        yield cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video));
        yield cleanupTMPLiveFilesFromFilesystem(video);
    });
}
exports.cleanupTMPLiveFiles = cleanupTMPLiveFiles;
function isTMPLiveFile(name) {
    return name.endsWith('.ts') ||
        name.endsWith('.m3u8') ||
        name.endsWith('.json') ||
        name.endsWith('.mpd') ||
        name.endsWith('.m4s') ||
        name.endsWith('.tmp');
}
function cleanupTMPLiveFilesFromFilesystem(video) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const hlsDirectory = (0, paths_1.getLiveDirectory)(video);
        if (!(yield (0, fs_extra_1.pathExists)(hlsDirectory)))
            return;
        logger_1.logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory);
        const files = yield (0, fs_extra_1.readdir)(hlsDirectory);
        for (const filename of files) {
            if (isTMPLiveFile(filename)) {
                const p = (0, path_1.join)(hlsDirectory, filename);
                (0, fs_extra_1.remove)(p)
                    .catch(err => logger_1.logger.error('Cannot remove %s.', p, { err }));
            }
        }
    });
}
function cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (streamingPlaylist.storage !== 1)
            return;
        logger_1.logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid);
        const keys = yield (0, object_storage_1.listHLSFileKeysOf)(streamingPlaylist);
        for (const key of keys) {
            if (isTMPLiveFile(key)) {
                yield (0, object_storage_1.removeHLSFileObjectStorageByFullKey)(key);
            }
        }
    });
}
//# sourceMappingURL=live-utils.js.map