2024-10-28 09:56:11 +01:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2024-10-30 14:43:26 +01:00
|
|
|
exports.log = void 0;
|
2024-10-28 09:56:11 +01: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 10:31:40 +01:00
|
|
|
const render_1 = tslib_1.__importDefault(require("./render"));
|
2024-10-28 09:56:11 +01:00
|
|
|
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
2024-10-29 10:31:40 +01:00
|
|
|
const types_1 = require("./types");
|
2024-10-28 09:56:11 +01:00
|
|
|
require('dotenv').config();
|
2024-10-29 10:31:40 +01:00
|
|
|
const app = (0, express_1.default)();
|
2024-10-30 17:59:14 +01:00
|
|
|
// Add the EJS view engine, to render the gallery page
|
2024-10-29 10:31:40 +01:00
|
|
|
app.set('view engine', 'ejs');
|
2024-10-30 17:59:14 +01:00
|
|
|
// Serve static assets from the /public folder
|
2024-10-29 10:31:40 +01:00
|
|
|
app.use(express_1.default.static('public'));
|
2024-10-30 15:40:42 +01:00
|
|
|
// An incoming request for a shared link
|
2024-10-29 10:31:40 +01:00
|
|
|
app.get('/share/:key', (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
2024-10-29 16:15:34 +01:00
|
|
|
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE);
|
2024-10-29 15:07:54 +01:00
|
|
|
if (!immich_1.default.isKey(req.params.key)) {
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('Invalid share key ' + req.params.key);
|
2024-10-28 09:56:11 +01:00
|
|
|
res.status(404).send();
|
|
|
|
}
|
|
|
|
else {
|
2024-10-29 15:07:54 +01:00
|
|
|
const sharedLink = yield immich_1.default.getShareByKey(req.params.key);
|
2024-10-30 14:28:29 +01:00
|
|
|
if (!sharedLink) {
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('Unknown share key ' + req.params.key);
|
2024-10-30 14:28:29 +01:00
|
|
|
res.status(404).send();
|
|
|
|
}
|
|
|
|
else if (!sharedLink.assets.length) {
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('No assets for key ' + req.params.key);
|
2024-10-29 10:31:40 +01:00
|
|
|
res.status(404).send();
|
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
else if (sharedLink.assets.length === 1) {
|
2024-10-28 09:56:11 +01:00
|
|
|
// This is an individual item (not a gallery)
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('Serving link ' + req.params.key);
|
2024-10-29 15:07:54 +01:00
|
|
|
const asset = sharedLink.assets[0];
|
2024-10-29 10:31:40 +01:00
|
|
|
if (asset.type === types_1.AssetType.image) {
|
2024-10-29 15:07:54 +01:00
|
|
|
// For photos, output the image directly
|
|
|
|
yield render_1.default.assetBuffer(res, sharedLink.assets[0], getSize(req));
|
2024-10-29 10:31:40 +01:00
|
|
|
}
|
|
|
|
else if (asset.type === types_1.AssetType.video) {
|
2024-10-29 15:07:54 +01:00
|
|
|
// For videos, show the video as a web player
|
|
|
|
yield render_1.default.gallery(res, sharedLink, 1);
|
2024-10-28 09:56:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2024-10-29 10:31:40 +01:00
|
|
|
// Multiple images - render as a gallery
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('Serving link ' + req.params.key);
|
2024-10-29 15:07:54 +01:00
|
|
|
yield render_1.default.gallery(res, sharedLink);
|
2024-10-28 09:56:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2024-10-30 15:40:42 +01:00
|
|
|
// Output the buffer data for a photo or video
|
2024-10-29 15:07:54 +01:00
|
|
|
app.get('/:type(photo|video)/:key/:id', (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
2024-10-29 16:15:34 +01:00
|
|
|
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE);
|
2024-10-29 15:07:54 +01: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 10:31:40 +01:00
|
|
|
}
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('No asset found for ' + req.path);
|
2024-10-29 15:07:54 +01:00
|
|
|
res.status(404).send();
|
|
|
|
}));
|
2024-10-30 15:40:42 +01:00
|
|
|
// Send a 404 for all other routes
|
2024-10-30 14:28:29 +01:00
|
|
|
app.get('*', (req, res) => {
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, exports.log)('Invalid route ' + req.path);
|
2024-10-29 15:07:54 +01:00
|
|
|
res.status(404).send();
|
2024-10-29 10:31:40 +01:00
|
|
|
});
|
2024-10-30 15:40:42 +01:00
|
|
|
/**
|
|
|
|
* Sanitise the data for an incoming query string `size` parameter
|
|
|
|
* e.g. https://example.com/share/abc...xyz?size=thumbnail
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Output a console.log message with timestamp
|
|
|
|
*/
|
|
|
|
const log = (message) => console.log((0, dayjs_1.default)().format() + ' ' + message);
|
|
|
|
exports.log = log;
|
2024-10-28 09:56:11 +01:00
|
|
|
app.listen(3000, () => {
|
|
|
|
console.log((0, dayjs_1.default)().format() + ' Server started');
|
|
|
|
});
|
2024-10-30 17:59:14 +01:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztBQUFBLDhEQUE2QjtBQUM3Qiw4REFBNkI7QUFDN0IsOERBQTZCO0FBQzdCLDBEQUF5QjtBQUN6QixtQ0FBOEM7QUFHOUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDLE1BQU0sRUFBRSxDQUFBO0FBRTFCLE1BQU0sR0FBRyxHQUFHLElBQUEsaUJBQU8sR0FBRSxDQUFBO0FBQ3JCLHNEQUFzRDtBQUN0RCxHQUFHLENBQUMsR0FBRyxDQUFDLGFBQWEsRUFBRSxLQUFLLENBQUMsQ0FBQTtBQUM3Qiw4Q0FBOEM7QUFDOUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxpQkFBTyxDQUFDLE1BQU0sQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFBO0FBRWpDLHdDQUF3QztBQUN4QyxHQUFHLENBQUMsR0FBRyxDQUFDLGFBQWEsRUFBRSxDQUFPLEdBQUcsRUFBRSxHQUFHLEVBQUUsRUFBRTtJQUN4QyxHQUFHLENBQUMsR0FBRyxDQUFDLGVBQWUsRUFBRSxrQkFBa0IsR0FBRyxPQUFPLENBQUMsR0FBRyxDQUFDLFNBQVMsQ0FBQyxDQUFBO0lBQ3BFLElBQUksQ0FBQyxnQkFBTSxDQUFDLEtBQUssQ0FBQyxHQUFHLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxFQUFFLENBQUM7UUFDbEMsS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