From 3a38ad2d3639792f43ee1493f685b5d900ca9209 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Fri, 9 Sep 2022 15:42:15 +0200 Subject: [PATCH] Integrated a logic for the content delivery server --- server/contentDelivery.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server/contentDelivery.ts b/server/contentDelivery.ts index 2aed7ba..f3c9938 100644 --- a/server/contentDelivery.ts +++ b/server/contentDelivery.ts @@ -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(); /** Logs something with a cdn prefix */ 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("

Asset not found

"); + if (found.assetEnding !== asset[1]) return res.status(404).send("

Asset not found

"); + + await pipeMedia(found.assetId, res); +}); + /** Starts the cdn service */ export const startServer = (port: number = 8673) => app.listen(port, () => log(`Listening on port ${port}`)); \ No newline at end of file