Integrated a logic for the content delivery server

This commit is contained in:
Mathias Wagner 2022-09-09 15:42:15 +02:00
parent ea0027cac3
commit 3a38ad2d36
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,9 +1,20 @@
import express, {Express} from 'express'; import express, {Express, Request, Response} from 'express';
import {getMediaObject, pipeMedia} from "../controller/media";
const app: Express = express(); const app: Express = express();
/** Logs something with a cdn prefix */ /** Logs something with a cdn prefix */
const log = (msg: string) => console.log(`[ContentDelivery] ${msg}`); const log = (msg: string) => console.log(`[ContentDelivery] ${msg}`);
app.get("*.*", async (req: Request, res: Response) => {
const asset = (req.originalUrl.substring(1) || "default.txt").split(".");
const found = await getMediaObject(asset[0]);
if (!found) return res.status(404).send("<h2>Asset not found</h2>");
if (found.assetEnding !== asset[1]) return res.status(404).send("<h2>Asset not found</h2>");
await pipeMedia(found.assetId, res);
});
/** Starts the cdn service */ /** Starts the cdn service */
export const startServer = (port: number = 8673) => app.listen(port, () => log(`Listening on port ${port}`)); export const startServer = (port: number = 8673) => app.listen(port, () => log(`Listening on port ${port}`));