This commit is contained in:
Madeline
2023-01-05 17:16:55 +11:00
parent a665a39523
commit df449169bd
124 changed files with 3639 additions and 1697 deletions
+9 -3
View File
@@ -56,7 +56,7 @@ export class CDNServer extends Server {
this.app.use("/splashes/", avatarsRoute);
this.log("verbose", "[Server] Route /splashes registered");
this.app.use("/discovery-splashes/", avatarsRoute);
this.log("verbose", "[Server] Route /discovery-splashes registered");
@@ -75,10 +75,16 @@ export class CDNServer extends Server {
this.app.use("/channel-icons/", avatarsRoute);
this.log("verbose", "[Server] Route /channel-icons registered");
this.app.use("/guilds/:guild_id/users/:user_id/avatars", guildProfilesRoute);
this.app.use(
"/guilds/:guild_id/users/:user_id/avatars",
guildProfilesRoute,
);
this.log("verbose", "[Server] Route /guilds/avatars registered");
this.app.use("/guilds/:guild_id/users/:user_id/banners", guildProfilesRoute);
this.app.use(
"/guilds/:guild_id/users/:user_id/banners",
guildProfilesRoute,
);
this.log("verbose", "[Server] Route /guilds/banners registered");
return super.start();
+18 -6
View File
@@ -12,21 +12,32 @@ import { storage } from "../util/Storage";
// TODO: delete old icons
const ANIMATED_MIME_TYPES = ["image/apng", "image/gif", "image/gifv"];
const STATIC_MIME_TYPES = ["image/png", "image/jpeg", "image/webp", "image/svg+xml", "image/svg"];
const STATIC_MIME_TYPES = [
"image/png",
"image/jpeg",
"image/webp",
"image/svg+xml",
"image/svg",
];
const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES];
const router = Router();
router.post("/", multer.single("file"), async (req: Request, res: Response) => {
if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature");
if (req.headers.signature !== Config.get().security.requestSignature)
throw new HTTPError("Invalid request signature");
if (!req.file) throw new HTTPError("Missing file");
const { buffer, mimetype, size, originalname, fieldname } = req.file;
const { guild_id, user_id } = req.params;
let hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex");
let hash = crypto
.createHash("md5")
.update(Snowflake.generate())
.digest("hex");
const type = await FileType.fromBuffer(buffer);
if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type");
if (!type || !ALLOWED_MIME_TYPES.includes(type.mime))
throw new HTTPError("Invalid file type");
if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash
const path = `guilds/${guild_id}/users/${user_id}/avatars/${hash}`;
@@ -38,7 +49,7 @@ router.post("/", multer.single("file"), async (req: Request, res: Response) => {
id: hash,
content_type: type.mime,
size,
url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`
url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
});
});
@@ -73,7 +84,8 @@ router.get("/:hash", async (req: Request, res: Response) => {
});
router.delete("/:id", async (req: Request, res: Response) => {
if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature");
if (req.headers.signature !== Config.get().security.requestSignature)
throw new HTTPError("Invalid request signature");
const { guild_id, user_id, id } = req.params;
const path = `guilds/${guild_id}/users/${user_id}/avatars/${id}`;
+2 -1
View File
@@ -35,7 +35,8 @@ export class FileStorage implements Storage {
async set(path: string, value: any) {
path = getPath(path);
if (!fs.existsSync(dirname(path))) fs.mkdirSync(dirname(path), { recursive: true });
if (!fs.existsSync(dirname(path)))
fs.mkdirSync(dirname(path), { recursive: true });
value = Readable.from(value);
const cleaned_file = fs.createWriteStream(path);