1
0
Fork 0
mirror of https://github.com/alangrainger/immich-public-proxy.git synced 2025-01-16 12:56:45 +01:00
immich-public-proxy/dist/index.js
2024-10-30 14:43:26 +01:00

84 lines
No EOL
12 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.log = void 0;
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;
};
const log = (message) => console.log((0, dayjs_1.default)().format() + ' ' + message);
exports.log = log;
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)) {
(0, exports.log)('Invalid share key ' + req.params.key);
res.status(404).send();
}
else {
const sharedLink = yield immich_1.default.getShareByKey(req.params.key);
if (!sharedLink) {
(0, exports.log)('Unknown share key ' + req.params.key);
res.status(404).send();
}
else if (!sharedLink.assets.length) {
(0, exports.log)('No assets for key ' + req.params.key);
res.status(404).send();
}
else if (sharedLink.assets.length === 1) {
// This is an individual item (not a gallery)
(0, exports.log)('Serving link ' + req.params.key);
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
(0, exports.log)('Serving link ' + req.params.key);
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;
}
}
}
(0, exports.log)('No asset found for ' + req.path);
res.status(404).send();
}));
// Send a 404 for all other unmatched routes
app.get('*', (req, res) => {
(0, exports.log)('Invalid route ' + req.path);
res.status(404).send();
});
app.listen(3000, () => {
console.log((0, dayjs_1.default)().format() + ' Server started');
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztBQUFBLDhEQUE2QjtBQUM3Qiw4REFBNkI7QUFDN0IsOERBQTZCO0FBQzdCLDBEQUF5QjtBQUN6QixtQ0FBOEM7QUFHOUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDLE1BQU0sRUFBRSxDQUFBO0FBRTFCLE1BQU0sR0FBRyxHQUFHLElBQUEsaUJBQU8sR0FBRSxDQUFBO0FBQ3JCLEdBQUcsQ0FBQyxHQUFHLENBQUMsYUFBYSxFQUFFLEtBQUssQ0FBQyxDQUFBO0FBQzdCLEdBQUcsQ0FBQyxHQUFHLENBQUMsaUJBQU8sQ0FBQyxNQUFNLENBQUMsUUFBUSxDQUFDLENBQUMsQ0FBQTtBQU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