"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceByBaseUrl = exports.getWebTorrentPrivateFileUrl = exports.getHLSPrivateFileUrl = exports.getHLSPublicFileUrl = exports.getWebTorrentPublicFileUrl = exports.getInternalUrl = void 0;
const config_1 = require("../../initializers/config");
const constants_1 = require("../../initializers/constants");
const shared_1 = require("./shared");
function getInternalUrl(config, keyWithoutPrefix) {
    return getBaseUrl(config) + (0, shared_1.buildKey)(keyWithoutPrefix, config);
}
exports.getInternalUrl = getInternalUrl;
function getWebTorrentPublicFileUrl(fileUrl) {
    const baseUrl = config_1.CONFIG.OBJECT_STORAGE.VIDEOS.BASE_URL;
    if (!baseUrl)
        return fileUrl;
    return replaceByBaseUrl(fileUrl, baseUrl);
}
exports.getWebTorrentPublicFileUrl = getWebTorrentPublicFileUrl;
function getHLSPublicFileUrl(fileUrl) {
    const baseUrl = config_1.CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BASE_URL;
    if (!baseUrl)
        return fileUrl;
    return replaceByBaseUrl(fileUrl, baseUrl);
}
exports.getHLSPublicFileUrl = getHLSPublicFileUrl;
function getHLSPrivateFileUrl(video, filename) {
    return constants_1.WEBSERVER.URL + constants_1.OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + video.uuid + `/${filename}`;
}
exports.getHLSPrivateFileUrl = getHLSPrivateFileUrl;
function getWebTorrentPrivateFileUrl(filename) {
    return constants_1.WEBSERVER.URL + constants_1.OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + filename;
}
exports.getWebTorrentPrivateFileUrl = getWebTorrentPrivateFileUrl;
function getBaseUrl(bucketInfo, baseUrl) {
    if (baseUrl)
        return baseUrl;
    return `${(0, shared_1.getEndpointParsed)().protocol}//${bucketInfo.BUCKET_NAME}.${(0, shared_1.getEndpointParsed)().host}/`;
}
const regex = new RegExp('https?://[^/]+');
function replaceByBaseUrl(fileUrl, baseUrl) {
    return fileUrl.replace(regex, baseUrl);
}
exports.replaceByBaseUrl = replaceByBaseUrl;
//# sourceMappingURL=urls.js.map