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

Merge branch 'fosscord:master' into master

This commit is contained in:
Paul Munteanu 2021-05-29 21:46:47 +03:00
commit a4c5cd360d

View File

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