"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVideoCaption = exports.getPreview = exports.lazyStaticRouter = void 0;
const tslib_1 = require("tslib");
const cors_1 = tslib_1.__importDefault(require("cors"));
const express_1 = tslib_1.__importDefault(require("express"));
const videos_torrent_cache_1 = require("../lib/files-cache/videos-torrent-cache");
const logger_1 = require("../helpers/logger");
const constants_1 = require("../initializers/constants");
const files_cache_1 = require("../lib/files-cache");
const local_actor_1 = require("../lib/local-actor");
const middlewares_1 = require("../middlewares");
const actor_image_1 = require("../models/actor/actor-image");
const lazyStaticRouter = express_1.default.Router();
exports.lazyStaticRouter = lazyStaticRouter;
lazyStaticRouter.use((0, cors_1.default)());
lazyStaticRouter.use(constants_1.LAZY_STATIC_PATHS.AVATARS + ':filename', (0, middlewares_1.asyncMiddleware)(getActorImage), middlewares_1.handleStaticError);
lazyStaticRouter.use(constants_1.LAZY_STATIC_PATHS.BANNERS + ':filename', (0, middlewares_1.asyncMiddleware)(getActorImage), middlewares_1.handleStaticError);
lazyStaticRouter.use(constants_1.LAZY_STATIC_PATHS.PREVIEWS + ':filename', (0, middlewares_1.asyncMiddleware)(getPreview), middlewares_1.handleStaticError);
lazyStaticRouter.use(constants_1.LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename', (0, middlewares_1.asyncMiddleware)(getVideoCaption), middlewares_1.handleStaticError);
lazyStaticRouter.use(constants_1.LAZY_STATIC_PATHS.TORRENTS + ':filename', (0, middlewares_1.asyncMiddleware)(getTorrent), middlewares_1.handleStaticError);
function getActorImage(req, res, next) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const filename = req.params.filename;
        if (local_actor_1.actorImagePathUnsafeCache.has(filename)) {
            return res.sendFile(local_actor_1.actorImagePathUnsafeCache.get(filename), { maxAge: constants_1.STATIC_MAX_AGE.SERVER });
        }
        const image = yield actor_image_1.ActorImageModel.loadByName(filename);
        if (!image)
            return res.status(404).end();
        if (image.onDisk === false) {
            if (!image.fileUrl)
                return res.status(404).end();
            logger_1.logger.info('Lazy serve remote actor image %s.', image.fileUrl);
            try {
                yield (0, local_actor_1.downloadActorImageFromWorker)({
                    filename: image.filename,
                    fileUrl: image.fileUrl,
                    size: getActorImageSize(image),
                    type: image.type
                });
            }
            catch (err) {
                logger_1.logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err });
                return res.status(404).end();
            }
            image.onDisk = true;
            image.save()
                .catch(err => logger_1.logger.error('Cannot save new actor image disk state.', { err }));
        }
        const path = image.getPath();
        local_actor_1.actorImagePathUnsafeCache.set(filename, path);
        return res.sendFile(path, { maxAge: constants_1.STATIC_MAX_AGE.LAZY_SERVER }, (err) => {
            if (!err)
                return;
            if (err.status === 404 && !image.isOwned()) {
                logger_1.logger.error('Cannot lazy serve actor image %s.', filename, { err });
                local_actor_1.actorImagePathUnsafeCache.delete(filename);
                image.onDisk = false;
                image.save()
                    .catch(err => logger_1.logger.error('Cannot save new actor image disk state.', { err }));
            }
            return next(err);
        });
    });
}
function getActorImageSize(image) {
    if (image.width && image.height) {
        return {
            height: image.height,
            width: image.width
        };
    }
    return constants_1.ACTOR_IMAGES_SIZE[image.type][0];
}
function getPreview(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const result = yield files_cache_1.VideosPreviewCache.Instance.getFilePath(req.params.filename);
        if (!result)
            return res.status(404).end();
        return res.sendFile(result.path, { maxAge: constants_1.STATIC_MAX_AGE.LAZY_SERVER });
    });
}
exports.getPreview = getPreview;
function getVideoCaption(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const result = yield files_cache_1.VideosCaptionCache.Instance.getFilePath(req.params.filename);
        if (!result)
            return res.status(404).end();
        return res.sendFile(result.path, { maxAge: constants_1.STATIC_MAX_AGE.LAZY_SERVER });
    });
}
exports.getVideoCaption = getVideoCaption;
function getTorrent(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const result = yield videos_torrent_cache_1.VideosTorrentCache.Instance.getFilePath(req.params.filename);
        if (!result)
            return res.status(404).end();
        return res.sendFile(result.path, { maxAge: constants_1.STATIC_MAX_AGE.SERVER });
    });
}
//# sourceMappingURL=lazy-static.js.map