🎨 remove long relatives paths -> short module paths

This commit is contained in:
Flam3rboy
2021-09-12 21:09:29 +02:00
parent 7eea37d184
commit 60535f5159
79 changed files with 1633 additions and 261 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
"scripts": {
"test": "npm run build && jest --coverage ./tests",
"build": "npx tsc -b .",
"start": "npm run build && node dist/start.js"
"start": "npm run build && node -r ./scripts/tsconfig-paths-bootstrap.js dist/start.js"
},
"repository": {
"type": "git",
+10
View File
@@ -0,0 +1,10 @@
const tsConfigPaths = require("tsconfig-paths");
const path = require("path");
const cleanup = tsConfigPaths.register({
baseUrl: path.join(__dirname, ".."),
paths: {
"@fosscord/cdn": ["dist/index.js"],
"@fosscord/cdn/*": ["dist/*"],
},
});
+64 -50
View File
@@ -1,73 +1,87 @@
import { Router, Response, Request } from "express";
import { Config, Snowflake } from "@fosscord/util";
import { storage } from "../util/Storage";
import { storage } from "@fosscord/cdn/util/Storage";
import FileType from "file-type";
import { HTTPError } from "lambert-server";
import { multer } from "../util/multer";
import { multer } from "@fosscord/cdn/util/multer";
import imageSize from "image-size";
const router = Router();
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");
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 { 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.endpoint || "http://localhost:3003";
const endpoint = Config.get()?.cdn.endpoint || "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;
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);
}
);
const file = {
id,
content_type: mimetype,
filename: filename,
size,
url: `${endpoint}/${path}`,
width,
height,
};
router.get(
"/:channel_id/:id/:filename",
async (req: Request, res: Response) => {
const { channel_id, id, filename } = req.params;
return res.json(file);
});
const file = await storage.get(
`attachments/${channel_id}/${id}/${filename}`
);
if (!file) throw new HTTPError("File not found");
const type = await FileType.fromBuffer(file);
router.get("/:channel_id/:id/:filename", async (req: Request, res: Response) => {
const { channel_id, id, filename } = req.params;
res.set("Content-Type", type?.mime);
res.set("Cache-Control", "public, max-age=31536000");
const file = await storage.get(`attachments/${channel_id}/${id}/${filename}`);
if (!file) throw new HTTPError("File not found");
const type = await FileType.fromBuffer(file);
return res.send(file);
}
);
res.set("Content-Type", type?.mime);
res.set("Cache-Control", "public, max-age=31536000");
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");
return res.send(file);
});
const { channel_id, id, filename } = req.params;
const path = `attachments/${channel_id}/${id}/${filename}`;
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");
await storage.delete(path);
const { channel_id, id, filename } = req.params;
const path = `attachments/${channel_id}/${id}/${filename}`;
await storage.delete(path);
return res.send({ success: true });
});
return res.send({ success: true });
}
);
export default router;
+37 -23
View File
@@ -1,9 +1,9 @@
import { Router, Response, Request } from "express";
import { Config, Snowflake } from "@fosscord/util";
import { storage } from "../util/Storage";
import { storage } from "@fosscord/cdn/util/Storage";
import FileType from "file-type";
import { HTTPError } from "lambert-server";
import { multer } from "../util/multer";
import { multer } from "@fosscord/cdn/util/multer";
import crypto from "crypto";
// TODO: check premium and animated pfp are allowed in the config
@@ -12,36 +12,50 @@ import crypto from "crypto";
// 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("/: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;
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");
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 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.endpoint || "http://localhost:3003";
const path = `avatars/${user_id}/${hash}`;
const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
await storage.set(path, buffer);
await storage.set(path, buffer);
return res.json({
id: hash,
content_type: type.mime,
size,
url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
});
});
return res.json({
id: hash,
content_type: type.mime,
size,
url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
});
}
);
router.get("/:user_id/:hash", async (req: Request, res: Response) => {
var { user_id, hash } = req.params;
+3 -2
View File
@@ -2,7 +2,7 @@ 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 { storage } from "@fosscord/cdn/util/Storage";
import FileType from "file-type";
import { Config } from "@fosscord/util";
@@ -13,7 +13,8 @@ const DEFAULT_FETCH_OPTIONS: any = {
redirect: "follow",
follow: 1,
headers: {
"user-agent": "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)",
"user-agent":
"Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)",
},
size: 1024 * 1024 * 8,
compress: true,
+6 -1
View File
@@ -65,6 +65,11 @@
/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"baseUrl": ".",
"paths": {
"@fosscord/cdn/": ["src/index.ts"],
"@fosscord/cdn/*": ["src/*"]
}
}
}