"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHLSFileReadStream = exports.getWebTorrentFileReadStream = exports.makeHLSFileAvailable = exports.makeWebTorrentFileAvailable = exports.removeWebTorrentObjectStorage = exports.removeHLSFileObjectStorageByFullKey = exports.removeHLSFileObjectStorageByPath = exports.removeHLSFileObjectStorageByFilename = exports.removeHLSObjectStorage = exports.updateHLSFilesACL = exports.updateWebTorrentFileACL = exports.storeHLSFileFromPath = exports.storeHLSFileFromFilename = exports.storeWebTorrentFile = exports.listHLSFileKeysOf = void 0;
const tslib_1 = require("tslib");
const path_1 = require("path");
const logger_1 = require("../../helpers/logger");
const config_1 = require("../../initializers/config");
const paths_1 = require("../paths");
const video_path_manager_1 = require("../video-path-manager");
const keys_1 = require("./keys");
const shared_1 = require("./shared");
function listHLSFileKeysOf(playlist) {
    return (0, shared_1.listKeysOfPrefix)((0, keys_1.generateHLSObjectBaseStorageKey)(playlist), config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS);
}
exports.listHLSFileKeysOf = listHLSFileKeysOf;
function storeHLSFileFromFilename(playlist, filename) {
    return (0, shared_1.storeObject)({
        inputPath: (0, path_1.join)((0, paths_1.getHLSDirectory)(playlist.Video), filename),
        objectStorageKey: (0, keys_1.generateHLSObjectStorageKey)(playlist, filename),
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
        isPrivate: playlist.Video.hasPrivateStaticPath()
    });
}
exports.storeHLSFileFromFilename = storeHLSFileFromFilename;
function storeHLSFileFromPath(playlist, path) {
    return (0, shared_1.storeObject)({
        inputPath: path,
        objectStorageKey: (0, keys_1.generateHLSObjectStorageKey)(playlist, (0, path_1.basename)(path)),
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
        isPrivate: playlist.Video.hasPrivateStaticPath()
    });
}
exports.storeHLSFileFromPath = storeHLSFileFromPath;
function storeWebTorrentFile(video, file) {
    return (0, shared_1.storeObject)({
        inputPath: video_path_manager_1.VideoPathManager.Instance.getFSVideoFileOutputPath(video, file),
        objectStorageKey: (0, keys_1.generateWebTorrentObjectStorageKey)(file.filename),
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.VIDEOS,
        isPrivate: video.hasPrivateStaticPath()
    });
}
exports.storeWebTorrentFile = storeWebTorrentFile;
function updateWebTorrentFileACL(video, file) {
    return (0, shared_1.updateObjectACL)({
        objectStorageKey: (0, keys_1.generateWebTorrentObjectStorageKey)(file.filename),
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.VIDEOS,
        isPrivate: video.hasPrivateStaticPath()
    });
}
exports.updateWebTorrentFileACL = updateWebTorrentFileACL;
function updateHLSFilesACL(playlist) {
    return (0, shared_1.updatePrefixACL)({
        prefix: (0, keys_1.generateHLSObjectBaseStorageKey)(playlist),
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
        isPrivate: playlist.Video.hasPrivateStaticPath()
    });
}
exports.updateHLSFilesACL = updateHLSFilesACL;
function removeHLSObjectStorage(playlist) {
    return (0, shared_1.removePrefix)((0, keys_1.generateHLSObjectBaseStorageKey)(playlist), config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS);
}
exports.removeHLSObjectStorage = removeHLSObjectStorage;
function removeHLSFileObjectStorageByFilename(playlist, filename) {
    return (0, shared_1.removeObject)((0, keys_1.generateHLSObjectStorageKey)(playlist, filename), config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS);
}
exports.removeHLSFileObjectStorageByFilename = removeHLSFileObjectStorageByFilename;
function removeHLSFileObjectStorageByPath(playlist, path) {
    return (0, shared_1.removeObject)((0, keys_1.generateHLSObjectStorageKey)(playlist, (0, path_1.basename)(path)), config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS);
}
exports.removeHLSFileObjectStorageByPath = removeHLSFileObjectStorageByPath;
function removeHLSFileObjectStorageByFullKey(key) {
    return (0, shared_1.removeObjectByFullKey)(key, config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS);
}
exports.removeHLSFileObjectStorageByFullKey = removeHLSFileObjectStorageByFullKey;
function removeWebTorrentObjectStorage(videoFile) {
    return (0, shared_1.removeObject)((0, keys_1.generateWebTorrentObjectStorageKey)(videoFile.filename), config_1.CONFIG.OBJECT_STORAGE.VIDEOS);
}
exports.removeWebTorrentObjectStorage = removeWebTorrentObjectStorage;
function makeHLSFileAvailable(playlist, filename, destination) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const key = (0, keys_1.generateHLSObjectStorageKey)(playlist, filename);
        logger_1.logger.info('Fetching HLS file %s from object storage to %s.', key, destination, (0, shared_1.lTags)());
        yield (0, shared_1.makeAvailable)({
            key,
            destination,
            bucketInfo: config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
        });
        return destination;
    });
}
exports.makeHLSFileAvailable = makeHLSFileAvailable;
function makeWebTorrentFileAvailable(filename, destination) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const key = (0, keys_1.generateWebTorrentObjectStorageKey)(filename);
        logger_1.logger.info('Fetching WebTorrent file %s from object storage to %s.', key, destination, (0, shared_1.lTags)());
        yield (0, shared_1.makeAvailable)({
            key,
            destination,
            bucketInfo: config_1.CONFIG.OBJECT_STORAGE.VIDEOS
        });
        return destination;
    });
}
exports.makeWebTorrentFileAvailable = makeWebTorrentFileAvailable;
function getWebTorrentFileReadStream(options) {
    const { filename, rangeHeader } = options;
    const key = (0, keys_1.generateWebTorrentObjectStorageKey)(filename);
    return (0, shared_1.createObjectReadStream)({
        key,
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.VIDEOS,
        rangeHeader
    });
}
exports.getWebTorrentFileReadStream = getWebTorrentFileReadStream;
function getHLSFileReadStream(options) {
    const { playlist, filename, rangeHeader } = options;
    const key = (0, keys_1.generateHLSObjectStorageKey)(playlist, filename);
    return (0, shared_1.createObjectReadStream)({
        key,
        bucketInfo: config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
        rangeHeader
    });
}
exports.getHLSFileReadStream = getHLSFileReadStream;
//# sourceMappingURL=videos.js.map