1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 13:14:06 +01:00

Merge branch 'master' of github.com:luth31/fosscord-api

This commit is contained in:
Paul Munteanu 2021-05-29 22:27:09 +03:00
commit 90f2536e41
No known key found for this signature in database
GPG Key ID: 4C0797E4861E8917

View File

@ -10,7 +10,7 @@ import i18nextBackend from "i18next-node-fs-backend";
import { ErrorHandler } from "./middlewares/ErrorHandler"; import { ErrorHandler } from "./middlewares/ErrorHandler";
import { BodyParser } from "./middlewares/BodyParser"; import { BodyParser } from "./middlewares/BodyParser";
import express, { Router } from "express"; import express, { Router } from "express";
import fetch from "node-fetch"; import fetch, { Response } from "node-fetch";
import mongoose from "mongoose"; import mongoose from "mongoose";
import path from "path"; import path from "path";
@ -28,6 +28,14 @@ declare global {
} }
} }
const assetCache = new Map<
string,
{
response: Response;
buffer: Buffer;
}
>();
export class FosscordServer extends Server { export class FosscordServer extends Server {
public options: FosscordServerOptions; public options: FosscordServerOptions;
@ -95,13 +103,21 @@ export class FosscordServer extends Server {
this.app.get("/assets/:file", async (req, res) => { this.app.get("/assets/:file", async (req, res) => {
delete req.headers.host; delete req.headers.host;
const response = await fetch(`https://discord.com/assets/${req.params.file}`, { var response: Response;
var buffer: Buffer;
const cache = assetCache.get(req.params.file);
if (!cache) {
response = await fetch(`https://discord.com/assets/${req.params.file}`, {
// @ts-ignore // @ts-ignore
headers: { headers: {
...req.headers ...req.headers
} }
}); });
const buffer = await response.buffer(); buffer = await response.buffer();
} else {
response = cache.response;
buffer = cache.buffer;
}
response.headers.forEach((value, name) => { response.headers.forEach((value, name) => {
if ( if (
@ -120,6 +136,7 @@ export class FosscordServer extends Server {
} }
res.set(name, value); res.set(name, value);
}); });
assetCache.set(req.params.file, { buffer, response });
return res.send(buffer); return res.send(buffer);
}); });