207 lines
6.4 KiB
TypeScript
207 lines
6.4 KiB
TypeScript
import { Request, Response } from "express";
|
|
import CalendarService from "../service/club/calendarService";
|
|
import CalendarTypeService from "../service/configuration/calendarTypeService";
|
|
import { calendar } from "../entity/club/calendar";
|
|
import InternalException from "../exceptions/internalException";
|
|
import CalendarFactory from "../factory/admin/club/calendar";
|
|
import { CalendarHelper } from "../helpers/calendarHelper";
|
|
import SettingHelper from "../helpers/settingsHelper";
|
|
import sharp from "sharp";
|
|
import ico from "sharp-ico";
|
|
import { FileSystemHelper } from "../helpers/fileSystemHelper";
|
|
|
|
/**
|
|
* @description get all calendar items by types or nscdr
|
|
* @summary passphrase is passed as value pair like `type:passphrase`
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getCalendarItemsByTypes(req: Request, res: Response): Promise<any> {
|
|
let types = Array.isArray(req.query.types) ? req.query.types : [req.query.types];
|
|
let nscdr = req.query.nscdr == "true";
|
|
let output = (req.query.output as "ics" | "json") ?? "ics";
|
|
|
|
if (output != "ics" && output != "json") {
|
|
throw new InternalException("set output query value to `ics` or `json` (defaults to `ics`)");
|
|
}
|
|
|
|
types = types.filter((t) => t);
|
|
|
|
let items: Array<calendar> = [];
|
|
if (types.length != 0) {
|
|
let typeIds = await CalendarTypeService.getByTypes((types as Array<string>).map((t) => t.split(":")[0]));
|
|
typeIds = typeIds.filter(
|
|
(ti) =>
|
|
ti.passphrase == null ||
|
|
ti.passphrase == "" ||
|
|
ti.passphrase == (types as Array<string>).find((t) => t.includes(ti.type)).split(":")[1]
|
|
);
|
|
items = await CalendarService.getByTypes(
|
|
typeIds.map((t) => t.id),
|
|
nscdr
|
|
);
|
|
} else {
|
|
items = await CalendarService.getByTypeNSCDR();
|
|
}
|
|
|
|
if (output == "json") {
|
|
res.json(CalendarFactory.mapToBase(items));
|
|
} else {
|
|
let { error, value } = CalendarHelper.buildICS(items);
|
|
|
|
res.type("ics").send(value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description get configuration of UI
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getApplicationConfig(req: Request, res: Response): Promise<any> {
|
|
let config = {
|
|
"club.name": SettingHelper.getSetting("club.name"),
|
|
"club.imprint": SettingHelper.getSetting("club.imprint"),
|
|
"club.privacy": SettingHelper.getSetting("club.privacy"),
|
|
"club.website": SettingHelper.getSetting("club.website"),
|
|
"app.custom_login_message": SettingHelper.getSetting("app.custom_login_message"),
|
|
"app.show_link_to_calendar": SettingHelper.getSetting("app.show_link_to_calendar"),
|
|
};
|
|
|
|
res.json(config);
|
|
}
|
|
|
|
/**
|
|
* @description get application Manifest
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getApplicationManifest(req: Request, res: Response): Promise<any> {
|
|
const backendUrl = `${req.protocol}://${req.get("host")}`;
|
|
const frontenUrl = `${req.get("referer")}`;
|
|
|
|
const manifest = {
|
|
id: "ff_admin_webapp",
|
|
lang: "de",
|
|
name: SettingHelper.getSetting("club.name"),
|
|
short_name: SettingHelper.getSetting("club.name"),
|
|
theme_color: "#990b00",
|
|
display: "standalone",
|
|
orientation: "portrait-primary",
|
|
start_url: frontenUrl,
|
|
icons: [
|
|
{
|
|
src: `${backendUrl}/api/public/favicon.ico`,
|
|
sizes: "48x48",
|
|
type: "image/ico",
|
|
},
|
|
{
|
|
src: `${backendUrl}/api/public/icon.png?width=512&height=512`,
|
|
sizes: "512x512",
|
|
type: "image/png",
|
|
},
|
|
],
|
|
};
|
|
|
|
res.set({
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Content-Type": "application/manifest+json",
|
|
});
|
|
|
|
res.json(manifest);
|
|
}
|
|
|
|
/**
|
|
* @description get application Logo
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getApplicationLogo(req: Request, res: Response): Promise<any> {
|
|
let setLogo = SettingHelper.getSetting("club.logo");
|
|
|
|
res.set({
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Cross-Origin-Resource-Policy": "cross-origin",
|
|
"Cross-Origin-Embedder-Policy": "credentialless",
|
|
"Timing-Allow-Origin": "*",
|
|
});
|
|
|
|
if (setLogo != "" && FileSystemHelper.getFilesInDirectory("/app", ".png").includes("admin-icon.png")) {
|
|
res.sendFile(FileSystemHelper.formatPath("/app/admin-logo.png"));
|
|
} else {
|
|
res.sendFile(FileSystemHelper.readAssetFile("admin-logo.png", true));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description get application Favicon
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getApplicationFavicon(req: Request, res: Response): Promise<any> {
|
|
let icon = FileSystemHelper.readAssetFile("icon.png", true);
|
|
let setLogo = SettingHelper.getSetting("club.icon");
|
|
|
|
if (setLogo != "" && FileSystemHelper.getFilesInDirectory("/app", ".png").includes("admin-icon.png")) {
|
|
icon = FileSystemHelper.formatPath("/app/admin-icon.png");
|
|
}
|
|
|
|
let image = await sharp(icon)
|
|
.resize(48, 48, {
|
|
fit: "inside",
|
|
})
|
|
.png()
|
|
.toBuffer();
|
|
|
|
let buffer = ico.encode([image]);
|
|
|
|
res.set({
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Cross-Origin-Resource-Policy": "cross-origin",
|
|
"Cross-Origin-Embedder-Policy": "credentialless",
|
|
"Timing-Allow-Origin": "*",
|
|
"Content-Type": "image/x-icon",
|
|
});
|
|
|
|
res.send(buffer);
|
|
}
|
|
|
|
/**
|
|
* @description get application Icon
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getApplicationIcon(req: Request, res: Response): Promise<any> {
|
|
const width = parseInt((req.query.width as string) ?? "512");
|
|
const height = parseInt((req.query.height as string) ?? "512");
|
|
|
|
let icon = FileSystemHelper.readAssetFile("icon.png", true);
|
|
let setLogo = SettingHelper.getSetting("club.icon");
|
|
|
|
if (setLogo != "" && FileSystemHelper.getFilesInDirectory("/app", ".png").includes("admin-icon.png")) {
|
|
icon = FileSystemHelper.formatPath("/app/admin-icon.png");
|
|
}
|
|
|
|
let image = await sharp(icon)
|
|
.resize(width, height, {
|
|
fit: "inside",
|
|
})
|
|
.png()
|
|
.toBuffer();
|
|
|
|
res.set({
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Cross-Origin-Resource-Policy": "cross-origin",
|
|
"Cross-Origin-Embedder-Policy": "credentialless",
|
|
"Timing-Allow-Origin": "*",
|
|
"Content-Type": "image/png",
|
|
});
|
|
|
|
res.send(image);
|
|
}
|