"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.staticRouter = void 0;
const tslib_1 = require("tslib");
const cors_1 = tslib_1.__importDefault(require("cors"));
const express_1 = tslib_1.__importDefault(require("express"));
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const hls_1 = require("../lib/hls");
const middlewares_1 = require("../middlewares");
const config_1 = require("../initializers/config");
const constants_1 = require("../initializers/constants");
const m3u8_playlist_1 = require("./shared/m3u8-playlist");
const staticRouter = express_1.default.Router();
exports.staticRouter = staticRouter;
staticRouter.use((0, cors_1.default)());
const privateWebTorrentStaticMiddlewares = config_1.CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true
    ? [middlewares_1.optionalAuthenticate, (0, middlewares_1.asyncMiddleware)(middlewares_1.ensureCanAccessVideoPrivateWebTorrentFiles)]
    : [];
staticRouter.use(constants_1.STATIC_PATHS.PRIVATE_WEBSEED, ...privateWebTorrentStaticMiddlewares, express_1.default.static(constants_1.DIRECTORIES.VIDEOS.PRIVATE, { fallthrough: false }), middlewares_1.handleStaticError);
staticRouter.use(constants_1.STATIC_PATHS.WEBSEED, express_1.default.static(constants_1.DIRECTORIES.VIDEOS.PUBLIC, { fallthrough: false }), middlewares_1.handleStaticError);
staticRouter.use(constants_1.STATIC_PATHS.REDUNDANCY, express_1.default.static(config_1.CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }), middlewares_1.handleStaticError);
const privateHLSStaticMiddlewares = config_1.CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true
    ? [middlewares_1.optionalAuthenticate, (0, middlewares_1.asyncMiddleware)(middlewares_1.ensureCanAccessPrivateVideoHLSFiles)]
    : [];
staticRouter.use(constants_1.STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:playlistName.m3u8', ...privateHLSStaticMiddlewares, (0, middlewares_1.asyncMiddleware)(servePrivateM3U8));
staticRouter.use(constants_1.STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, ...privateHLSStaticMiddlewares, express_1.default.static(constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }), middlewares_1.handleStaticError);
staticRouter.use(constants_1.STATIC_PATHS.STREAMING_PLAYLISTS.HLS, express_1.default.static(constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, { fallthrough: false }), middlewares_1.handleStaticError);
const thumbnailsPhysicalPath = config_1.CONFIG.STORAGE.THUMBNAILS_DIR;
staticRouter.use(constants_1.STATIC_PATHS.THUMBNAILS, express_1.default.static(thumbnailsPhysicalPath, { maxAge: constants_1.STATIC_MAX_AGE.SERVER, fallthrough: false }), middlewares_1.handleStaticError);
function servePrivateM3U8(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const path = (0, path_1.join)(constants_1.DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, req.params.videoUUID, req.params.playlistName + '.m3u8');
        let playlistContent;
        try {
            playlistContent = yield (0, fs_extra_1.readFile)(path, 'utf-8');
        }
        catch (err) {
            if (err.message.includes('ENOENT')) {
                return res.fail({
                    status: 404,
                    message: 'File not found'
                });
            }
            throw err;
        }
        const transformedContent = (0, m3u8_playlist_1.doReinjectVideoFileToken)(req)
            ? (0, hls_1.injectQueryToPlaylistUrls)(playlistContent, (0, m3u8_playlist_1.buildReinjectVideoFileTokenQuery)(req))
            : playlistContent;
        return res.set('content-type', 'application/vnd.apple.mpegurl').send(transformedContent).end();
    });
}
//# sourceMappingURL=static.js.map