"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const express_1 = tslib_1.__importDefault(require("express")); const immich_1 = tslib_1.__importDefault(require("./immich")); const render_1 = tslib_1.__importDefault(require("./render")); const dayjs_1 = tslib_1.__importDefault(require("dayjs")); const types_1 = require("./types"); require('dotenv').config(); const app = (0, express_1.default)(); app.set('view engine', 'ejs'); app.use(express_1.default.static('public')); const getSize = (req) => { var _a; return ((_a = req === null || req === void 0 ? void 0 : req.query) === null || _a === void 0 ? void 0 : _a.size) === 'thumbnail' ? types_1.ImageSize.thumbnail : types_1.ImageSize.original; }; app.get('/share/:key', (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE); if (!immich_1.default.isKey(req.params.key)) { res.status(404).send(); } else { const sharedLink = yield immich_1.default.getShareByKey(req.params.key); if (!sharedLink || !sharedLink.assets.length) { res.status(404).send(); } else if (sharedLink.assets.length === 1) { // This is an individual item (not a gallery) const asset = sharedLink.assets[0]; if (asset.type === types_1.AssetType.image) { // For photos, output the image directly yield render_1.default.assetBuffer(res, sharedLink.assets[0], getSize(req)); } else if (asset.type === types_1.AssetType.video) { // For videos, show the video as a web player yield render_1.default.gallery(res, sharedLink, 1); } } else { // Multiple images - render as a gallery yield render_1.default.gallery(res, sharedLink); } } })); // Output the buffer data for an photo or video app.get('/:type(photo|video)/:key/:id', (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE); // Check for valid key and ID if (immich_1.default.isKey(req.params.key) && immich_1.default.isId(req.params.id)) { // Check if the key is a valid share link const sharedLink = yield immich_1.default.getShareByKey(req.params.key); if (sharedLink === null || sharedLink === void 0 ? void 0 : sharedLink.assets.length) { // Check that the requested asset exists in this share const asset = sharedLink.assets.find(x => x.id === req.params.id); if (asset) { asset.type = req.params.type === 'video' ? types_1.AssetType.video : types_1.AssetType.image; render_1.default.assetBuffer(res, asset, getSize(req)).then(); return; } } } res.status(404).send(); })); // Send a 404 for all other unmatched routes app.get('*', (_req, res) => { res.status(404).send(); }); app.listen(3000, () => { console.log((0, dayjs_1.default)().format() + ' Server started'); }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsOERBQTZCO0FBQzdCLDhEQUE2QjtBQUM3Qiw4REFBNkI7QUFDN0IsMERBQXlCO0FBQ3pCLG1DQUE4QztBQUc5QyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUMsTUFBTSxFQUFFLENBQUE7QUFFMUIsTUFBTSxHQUFHLEdBQUcsSUFBQSxpQkFBTyxHQUFFLENBQUE7QUFDckIsR0FBRyxDQUFDLEdBQUcsQ0FBQyxhQUFhLEVBQUUsS0FBSyxDQUFDLENBQUE7QUFDN0IsR0FBRyxDQUFDLEdBQUcsQ0FBQyxpQkFBTyxDQUFDLE1BQU0sQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFBO0FBRWpDLE1BQU0sT0FBTyxHQUFHLENBQUMsR0FBWSxFQUFFLEVBQUU7O0lBQy9CLE9BQU8sQ0FBQSxNQUFBLEdBQUcsYUFBSCxHQUFHLHVCQUFILEdBQUcsQ0FBRSxLQUFLLDBDQUFFLElBQUksTUFBSyxXQUFXLENBQUMsQ0FBQyxDQUFDLGlCQUFTLENBQUMsU0FBUyxDQUFDLENBQUMsQ0FBQyxpQkFBUyxDQUFDLFFBQVEsQ0FBQTtBQUNwRixDQUFDLENBQUE7QUFFRCxHQUFHLENBQUMsR0FBRyxDQUFDLGFBQWEsRUFBRSxDQUFPLEdBQUcsRUFBRSxHQUFHLEVBQUUsRUFBRTtJQUN4QyxHQUFHLENBQUMsR0FBRyxDQUFDLGVBQWUsRUFBRSxrQkFB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