"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filesRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const toInt_1 = (0, 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("../../../models/video/video-file");
const middlewares_1 = require("../../../middlewares");
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)(removeHLSPlaylist));
filesRouter.delete('/:id/webtorrent', middlewares_1.authenticate, (0, middlewares_1.ensureUserHasRight)(25), (0, middlewares_1.asyncMiddleware)(middlewares_1.videoFilesDeleteWebTorrentValidator), (0, middlewares_1.asyncMiddleware)(removeWebTorrentFiles));
function getVideoFileMetadata(req, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const videoFile = yield video_file_1.VideoFileModel.loadWithMetadata((0, toInt_1.default)(req.params.videoFileId));
        return res.json(videoFile.metadata);
    });
}
function removeHLSPlaylist(req, res) {
    return (0, 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));
        const hls = video.getHLSPlaylist();
        yield video.removeStreamingPlaylistFiles(hls);
        yield hls.destroy();
        video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id);
        yield (0, videos_1.federateVideoIfNeeded)(video, false, undefined);
        return res.sendStatus(204);
    });
}
function removeWebTorrentFiles(req, res) {
    return (0, 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));
        for (const file of video.VideoFiles) {
            yield video.removeWebTorrentFileAndTorrent(file);
            yield file.destroy();
        }
        video.VideoFiles = [];
        yield (0, videos_1.federateVideoIfNeeded)(video, false, undefined);
        return res.sendStatus(204);
    });
}
//# sourceMappingURL=files.js.map