immich-public-proxy/dist/index.js

84 lines
12 KiB
JavaScript
Raw Normal View History

2024-10-28 04:56:11 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2024-10-30 09:43:26 -04:00
exports.log = void 0;
2024-10-28 04:56:11 -04:00
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const immich_1 = tslib_1.__importDefault(require("./immich"));
2024-10-29 05:31:40 -04:00
const render_1 = tslib_1.__importDefault(require("./render"));
2024-10-28 04:56:11 -04:00
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
2024-10-29 05:31:40 -04:00
const types_1 = require("./types");
2024-10-28 04:56:11 -04:00
require('dotenv').config();
2024-10-29 05:31:40 -04:00
const app = (0, express_1.default)();
app.set('view engine', 'ejs');
app.use(express_1.default.static('public'));
const getSize = (req) => {
2024-10-28 04:56:11 -04:00
var _a;
2024-10-29 05:31:40 -04:00
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;
};
2024-10-30 09:28:29 -04:00
const log = (message) => console.log((0, dayjs_1.default)().format() + ' ' + message);
2024-10-30 09:43:26 -04:00
exports.log = log;
2024-10-29 05:31:40 -04:00
app.get('/share/:key', (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
2024-10-29 11:15:34 -04:00
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE);
2024-10-29 10:07:54 -04:00
if (!immich_1.default.isKey(req.params.key)) {
2024-10-30 09:43:26 -04:00
(0, exports.log)('Invalid share key ' + req.params.key);
2024-10-28 04:56:11 -04:00
res.status(404).send();
}
else {
2024-10-29 10:07:54 -04:00
const sharedLink = yield immich_1.default.getShareByKey(req.params.key);
2024-10-30 09:28:29 -04:00
if (!sharedLink) {
2024-10-30 09:43:26 -04:00
(0, exports.log)('Unknown share key ' + req.params.key);
2024-10-30 09:28:29 -04:00
res.status(404).send();
}
else if (!sharedLink.assets.length) {
2024-10-30 09:43:26 -04:00
(0, exports.log)('No assets for key ' + req.params.key);
2024-10-29 05:31:40 -04:00
res.status(404).send();
}
2024-10-29 10:07:54 -04:00
else if (sharedLink.assets.length === 1) {
2024-10-28 04:56:11 -04:00
// This is an individual item (not a gallery)
2024-10-30 09:43:26 -04:00
(0, exports.log)('Serving link ' + req.params.key);
2024-10-29 10:07:54 -04:00
const asset = sharedLink.assets[0];
2024-10-29 05:31:40 -04:00
if (asset.type === types_1.AssetType.image) {
2024-10-29 10:07:54 -04:00
// For photos, output the image directly
yield render_1.default.assetBuffer(res, sharedLink.assets[0], getSize(req));
2024-10-29 05:31:40 -04:00
}
else if (asset.type === types_1.AssetType.video) {
2024-10-29 10:07:54 -04:00
// For videos, show the video as a web player
yield render_1.default.gallery(res, sharedLink, 1);
2024-10-28 04:56:11 -04:00
}
}
else {
2024-10-29 05:31:40 -04:00
// Multiple images - render as a gallery
2024-10-30 09:43:26 -04:00
(0, exports.log)('Serving link ' + req.params.key);
2024-10-29 10:07:54 -04:00
yield render_1.default.gallery(res, sharedLink);
2024-10-28 04:56:11 -04:00
}
}
}));
2024-10-29 05:31:40 -04:00
// Output the buffer data for an photo or video
2024-10-29 10:07:54 -04:00
app.get('/:type(photo|video)/:key/:id', (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
2024-10-29 11:15:34 -04:00
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE);
2024-10-29 10:07:54 -04:00
// 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;
}
}
2024-10-29 05:31:40 -04:00
}
2024-10-30 09:43:26 -04:00
(0, exports.log)('No asset found for ' + req.path);
2024-10-29 10:07:54 -04:00
res.status(404).send();
}));
// Send a 404 for all other unmatched routes
2024-10-30 09:28:29 -04:00
app.get('*', (req, res) => {
2024-10-30 09:43:26 -04:00
(0, exports.log)('Invalid route ' + req.path);
2024-10-29 10:07:54 -04:00
res.status(404).send();
2024-10-29 05:31:40 -04:00
});
2024-10-28 04:56:11 -04:00
app.listen(3000, () => {
console.log((0, dayjs_1.default)().format() + ' Server started');
});
2024-10-30 09:43:26 -04:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztBQUFBLDhEQUE2QjtBQUM3Qiw4REFBNkI7QUFDN0IsOERBQTZCO0FBQzdCLDBEQUF5QjtBQUN6QixtQ0FBOEM7QUFHOUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDLE1BQU0sRUFBRSxDQUFBO0FBRTFCLE1BQU0sR0FBRyxHQUFHLElBQUEsaUJBQU8sR0FBRSxDQUFBO0FBQ3JCLEdBQUcsQ0FBQyxHQUFHLENBQUMsYUFBYSxFQUFFLEtBQUssQ0FBQyxDQUFBO0FBQzdCLEdBQUcsQ0FBQyxHQUFHLENBQUMsaUJBQU8sQ0FBQyxNQUFNLENBQUMsUUFBUSxDQUFDLENBQUMsQ0FBQTtBQUVqQyxNQUFNLE9BQU8sR0FBRyxDQUFDLEdBQVksRUFBRSxFQUFFOztJQUMvQixPQUFPLENBQUEsTUFBQSxHQUFHLGFBQUgsR0FBRyx1QkFBSCxHQUFHLENBQUUsS0FBSywwQ0FBRSxJQUFJLE1BQUssV0FBVyxDQUFDLENBQUMsQ0FBQyxpQkFBUyxDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUMsa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