Refactor to mono-repo + upgrade packages
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
import { Config, Snowflake } from "@fosscord/util";
|
||||
import { storage } from "../util/Storage";
|
||||
import FileType from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { multer } from "../util/multer";
|
||||
import imageSize from "image-size";
|
||||
|
||||
const router = Router();
|
||||
|
||||
const SANITIZED_CONTENT_TYPE = [
|
||||
"text/html",
|
||||
"text/mhtml",
|
||||
"multipart/related",
|
||||
"application/xhtml+xml",
|
||||
];
|
||||
|
||||
router.post(
|
||||
"/:channel_id",
|
||||
multer.single("file"),
|
||||
async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature)
|
||||
throw new HTTPError("Invalid request signature");
|
||||
if (!req.file) throw new HTTPError("file missing");
|
||||
|
||||
const { buffer, mimetype, size, originalname, fieldname } = req.file;
|
||||
const { channel_id } = req.params;
|
||||
const filename = originalname
|
||||
.replaceAll(" ", "_")
|
||||
.replace(/[^a-zA-Z0-9._]+/g, "");
|
||||
const id = Snowflake.generate();
|
||||
const path = `attachments/${channel_id}/${id}/${filename}`;
|
||||
|
||||
const endpoint =
|
||||
Config.get()?.cdn.endpointPublic || "http://localhost:3003";
|
||||
|
||||
await storage.set(path, buffer);
|
||||
var width;
|
||||
var height;
|
||||
if (mimetype.includes("image")) {
|
||||
const dimensions = imageSize(buffer);
|
||||
if (dimensions) {
|
||||
width = dimensions.width;
|
||||
height = dimensions.height;
|
||||
}
|
||||
}
|
||||
|
||||
const file = {
|
||||
id,
|
||||
content_type: mimetype,
|
||||
filename: filename,
|
||||
size,
|
||||
url: `${endpoint}/${path}`,
|
||||
width,
|
||||
height,
|
||||
};
|
||||
|
||||
return res.json(file);
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/:channel_id/:id/:filename",
|
||||
async (req: Request, res: Response) => {
|
||||
const { channel_id, id, filename } = req.params;
|
||||
|
||||
const file = await storage.get(
|
||||
`attachments/${channel_id}/${id}/${filename}`
|
||||
);
|
||||
if (!file) throw new HTTPError("File not found");
|
||||
const type = await FileType.fromBuffer(file);
|
||||
let content_type = type?.mime || "application/octet-stream";
|
||||
|
||||
if (SANITIZED_CONTENT_TYPE.includes(content_type)) {
|
||||
content_type = "application/octet-stream";
|
||||
}
|
||||
|
||||
res.set("Content-Type", content_type);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
}
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/:channel_id/:id/:filename",
|
||||
async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature)
|
||||
throw new HTTPError("Invalid request signature");
|
||||
|
||||
const { channel_id, id, filename } = req.params;
|
||||
const path = `attachments/${channel_id}/${id}/${filename}`;
|
||||
|
||||
await storage.delete(path);
|
||||
|
||||
return res.send({ success: true });
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,104 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
import { Config, Snowflake } from "@fosscord/util";
|
||||
import { storage } from "../util/Storage";
|
||||
import FileType from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
// TODO: generate different image types of icon
|
||||
// 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 ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES];
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post(
|
||||
"/:user_id",
|
||||
multer.single("file"),
|
||||
async (req: Request, res: Response) => {
|
||||
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 { user_id } = req.params;
|
||||
|
||||
var 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 (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash
|
||||
|
||||
const path = `avatars/${user_id}/${hash}`;
|
||||
const endpoint =
|
||||
Config.get().cdn.endpointPublic || "http://localhost:3003";
|
||||
|
||||
await storage.set(path, buffer);
|
||||
|
||||
return res.json({
|
||||
id: hash,
|
||||
content_type: type.mime,
|
||||
size,
|
||||
url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
router.get("/:user_id", async (req: Request, res: Response) => {
|
||||
var { user_id } = req.params;
|
||||
user_id = user_id.split(".")[0]; // remove .file extension
|
||||
const path = `avatars/${user_id}`;
|
||||
|
||||
const file = await storage.get(path);
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
const type = await FileType.fromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
export const getAvatar = async (req: Request, res: Response) => {
|
||||
var { user_id, hash } = req.params;
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `avatars/${user_id}/${hash}`;
|
||||
|
||||
const file = await storage.get(path);
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
const type = await FileType.fromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
}
|
||||
|
||||
router.get("/:user_id/:hash", getAvatar);
|
||||
|
||||
router.delete("/:user_id/:id", async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature)
|
||||
throw new HTTPError("Invalid request signature");
|
||||
const { user_id, id } = req.params;
|
||||
const path = `avatars/${user_id}/${id}`;
|
||||
|
||||
await storage.delete(path);
|
||||
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,91 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
import fetch from "node-fetch";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { Snowflake } from "@fosscord/util";
|
||||
import { storage } from "../util/Storage";
|
||||
import FileType from "file-type";
|
||||
import { Config } from "@fosscord/util";
|
||||
import sharp from "sharp";
|
||||
|
||||
// TODO: somehow handle the deletion of images posted to the /external route
|
||||
|
||||
const router = Router();
|
||||
const DEFAULT_FETCH_OPTIONS: any = {
|
||||
redirect: "follow",
|
||||
follow: 1,
|
||||
headers: {
|
||||
"user-agent":
|
||||
"Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)",
|
||||
},
|
||||
size: 1024 * 1024 * 8,
|
||||
compress: true,
|
||||
method: "GET",
|
||||
};
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature)
|
||||
throw new HTTPError("Invalid request signature");
|
||||
|
||||
if (!req.body) throw new HTTPError("Invalid Body");
|
||||
|
||||
const { url } = req.body;
|
||||
if (!url || typeof url !== "string") throw new HTTPError("Invalid url");
|
||||
|
||||
const id = Snowflake.generate();
|
||||
|
||||
try {
|
||||
const response = await fetch(url, DEFAULT_FETCH_OPTIONS);
|
||||
const buffer = await response.buffer();
|
||||
|
||||
await storage.set(`/external/${id}`, buffer);
|
||||
|
||||
res.send({ id });
|
||||
} catch (error) {
|
||||
throw new HTTPError("Couldn't fetch website");
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:id", async (req: Request, res: Response) => {
|
||||
const { id } = req.params;
|
||||
|
||||
const file = await storage.get(`/external/${id}`);
|
||||
if (!file) throw new HTTPError("File not found");
|
||||
const result = await FileType.fromBuffer(file);
|
||||
|
||||
res.set("Content-Type", result?.mime);
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
// this method is gross lol don't care
|
||||
router.get("/resize/:url", async (req: Request, res: Response) => {
|
||||
const url = decodeURIComponent(req.params.url);
|
||||
const { width, height } = req.query;
|
||||
if (!width || !height) throw new HTTPError("Must provide width and height");
|
||||
|
||||
const { resizeHeightMax, resizeWidthMax } = Config.get().cdn;
|
||||
const w = Math.min(parseInt(width as string), resizeWidthMax ?? 100);
|
||||
const h = Math.min(parseInt(height as string), resizeHeightMax ?? 100);
|
||||
if (w < 1 || h < 1) throw new HTTPError("Width and height must be greater than 0");
|
||||
|
||||
let buffer, response;
|
||||
try {
|
||||
response = await fetch(url, DEFAULT_FETCH_OPTIONS);
|
||||
buffer = await response.buffer();
|
||||
}
|
||||
catch (e) {
|
||||
throw new HTTPError("Couldn't fetch website");
|
||||
}
|
||||
|
||||
const resizedBuffer = await sharp(buffer)
|
||||
.resize(parseInt(width as string), parseInt(height as string), {
|
||||
fit: "inside",
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
res.setHeader("Content-Disposition", "attachment");
|
||||
res.setHeader("Content-Type", response.headers.get("content-type") ?? "image/png");
|
||||
return res.end(resizedBuffer);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Router } from "express";
|
||||
import { getAvatar } from "./avatars";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// TODO: handle guild profiles
|
||||
router.get("/:guild_id/users/:user_id/avatars/:hash", getAvatar);
|
||||
router.get("/:guild_id/users/:user_id/banners/:hash", getAvatar);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", (req: Request, res: Response) => {
|
||||
res.send("pong");
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,101 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
import { Config, Snowflake } from "@fosscord/util";
|
||||
import { storage } from "../util/Storage";
|
||||
import FileType from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
|
||||
//Role icons ---> avatars.ts modified
|
||||
|
||||
// TODO: check user rights and perks and animated pfp are allowed in the policies
|
||||
// TODO: generate different sizes of icon
|
||||
// TODO: generate different image types of icon
|
||||
|
||||
const STATIC_MIME_TYPES = [
|
||||
"image/png",
|
||||
"image/jpeg",
|
||||
"image/webp",
|
||||
"image/svg+xml",
|
||||
"image/svg",
|
||||
];
|
||||
const ALLOWED_MIME_TYPES = [...STATIC_MIME_TYPES];
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post(
|
||||
"/:role_id",
|
||||
multer.single("file"),
|
||||
async (req: Request, res: Response) => {
|
||||
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 { role_id } = req.params;
|
||||
|
||||
var 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");
|
||||
|
||||
const path = `role-icons/${role_id}/${hash}.png`;
|
||||
const endpoint =
|
||||
Config.get().cdn.endpointPublic || "http://localhost:3003";
|
||||
|
||||
await storage.set(path, buffer);
|
||||
|
||||
return res.json({
|
||||
id: hash,
|
||||
content_type: type.mime,
|
||||
size,
|
||||
url: `${endpoint}${req.baseUrl}/${role_id}/${hash}`,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
router.get("/:role_id", async (req: Request, res: Response) => {
|
||||
var { role_id } = req.params;
|
||||
//role_id = role_id.split(".")[0]; // remove .file extension
|
||||
const path = `role-icons/${role_id}`;
|
||||
|
||||
const file = await storage.get(path);
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
const type = await FileType.fromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000, must-revalidate");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
router.get("/:role_id/:hash", async (req: Request, res: Response) => {
|
||||
var { role_id, hash } = req.params;
|
||||
//hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `role-icons/${role_id}/${hash}`;
|
||||
|
||||
const file = await storage.get(path);
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
const type = await FileType.fromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000, must-revalidate");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
router.delete("/:role_id/:id", async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature)
|
||||
throw new HTTPError("Invalid request signature");
|
||||
const { role_id, id } = req.params;
|
||||
const path = `role-icons/${role_id}/${id}`;
|
||||
|
||||
await storage.delete(path);
|
||||
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user