1 files changed, 30 insertions, 0 deletions
diff --git a/src/routes/assets/index.ts b/src/routes/assets/index.ts
new file mode 100644
index 00000000..d3683f43
--- /dev/null
+++ b/src/routes/assets/index.ts
@@ -0,0 +1,30 @@
+import { Router } from "express";
+import fetch from "node-fetch";
+
+const router = Router();
+const cache = new Map();
+const assetEndpoint = "https://discord.com/assets/";
+
+export async function getCache(key: string): Promise<Response> {
+ let cachedRessource = cache.get(key);
+
+ if (!cachedRessource) {
+ const res = await fetch(assetEndpoint + key);
+ // @ts-ignore
+ res.bufferResponse = await res.buffer();
+ cache.set(key, res);
+ cachedRessource = res;
+ }
+
+ return cachedRessource;
+}
+
+router.get("/:hash", async (req, res) => {
+ res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
+ const cache = await getCache(req.params.hash);
+ res.set("content-type", <string>cache.headers.get("content-type"));
+ // @ts-ignore
+ res.send(cache.bufferResponse);
+});
+
+export default router;
|