"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filesRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const toInt_1 = tslib_1.__importDefault(require("validator/lib/toInt"));
const logger_1 = require("../../../helpers/logger");
const videos_1 = require("../../../lib/activitypub/videos");
const video_file_1 = require("../../../lib/video-file");
const video_file_2 = require("../../../models/video/video-file");
const middlewares_1 = require("../../../middlewares");
const hls_1 = require("../../../lib/hls");
const lTags = (0, logger_1.loggerTagsFactory)('api', 'video');
const filesRouter = express_1.default.Router();
exports.filesRouter = filesRouter;
filesRouter.get('/:id/metadata/:videoFileId', (0, middlewares_1.asyncMiddleware)(middlewares_1.videosGetValidator), (0, middlewares_1.asyncMiddleware)(middlewares_1.videoFileMetadataGetValidator), (0, middlewares_1.asyncMiddleware)(getVideoFileMetadata));
filesRouter.delete('/:id/hls', middlewares_1.authenticate, (0, middlewares_1.ensureUserHasRight)(25), (0, middlewares_1.asyncMiddleware)(middlewares_1.videoFilesDeleteHLSValidator), (0, middlewares_1.asyncMiddleware)(removeHLSPlaylistController));
filesRouter.delete('/:id/hls/:videoFileId', middlewares_1.authenticate, (0, middlewares_1.ensureUserHasRight)(25), (0, middlewares_1.asyncMiddleware)(middlewares_1.videoFilesDeleteHLSFileValidator), (0, middlewares_1.asyncMiddleware)(removeHLSFileController));
filesRouter.delete('/:id/webtorrent', middlewares_1.authenticate, (0, middlewares_1.ensureUserHasRight)(25), (0, middlewares_1.asyncMiddleware)(middlewares_1.videoFilesDeleteWebTorrentValidator), (0, middlewares_1.asyncMiddleware)(removeAllWebTorrentFilesController));
filesRouter.delete('/:id/webtorrent/:videoFileId', middlewares_1.authenticate, (0, middlewares_1.ensureUserHasRight)(25), (0, middlewares_1.asyncMiddleware)(middlewares_1.videoFilesDeleteWebTorrentFileValidator), (0, middlewares_1.asyncMiddleware)(removeWebTorrentFileController));
function getVideoFileMetadata(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const videoFile = yield video_file_2.VideoFileModel.loadWithMetadata((0, toInt_1.default)(req.params.videoFileId));
        return res.json(videoFile.metadata);
    });
}
function removeHLSPlaylistController(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        logger_1.logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid));
        yield (0, video_file_1.removeHLSPlaylist)(video);
        yield (0, videos_1.federateVideoIfNeeded)(video, false, undefined);
        return res.sendStatus(204);
    });
}
function removeHLSFileController(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        const videoFileId = +req.params.videoFileId;
        logger_1.logger.info('Deleting HLS file %d of %s.', videoFileId, video.url, lTags(video.uuid));
        const playlist = yield (0, video_file_1.removeHLSFile)(video, videoFileId);
        if (playlist)
            yield (0, hls_1.updatePlaylistAfterFileChange)(video, playlist);
        yield (0, videos_1.federateVideoIfNeeded)(video, false, undefined);
        return res.sendStatus(204);
    });
}
function removeAllWebTorrentFilesController(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        logger_1.logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid));
        yield (0, video_file_1.removeAllWebTorrentFiles)(video);
        yield (0, videos_1.federateVideoIfNeeded)(video, false, undefined);
        return res.sendStatus(204);
    });
}
function removeWebTorrentFileController(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        const videoFileId = +req.params.videoFileId;
        logger_1.logger.info('Deleting WebTorrent file %d of %s.', videoFileId, video.url, lTags(video.uuid));
        yield (0, video_file_1.removeWebTorrentFile)(video, videoFileId);
        yield (0, videos_1.federateVideoIfNeeded)(video, false, undefined);
        return res.sendStatus(204);
    });
}
//# sourceMappingURL=files.js.map