avatars + attachments

This commit is contained in:
Flam3rboy 2021-05-30 01:44:46 +02:00
parent 3b03266785
commit e87bebc3a3
7 changed files with 81 additions and 13 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ node_modules/
.DS_Store .DS_Store
.env .env
dist/ dist/
files/

53
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@fosscord/server-util": "^1.3.8", "@fosscord/server-util": "^1.3.10",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"btoa": "^1.2.1", "btoa": "^1.2.1",
"cheerio": "^1.0.0-rc.5", "cheerio": "^1.0.0-rc.5",
@ -17,6 +17,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"express-async-errors": "^3.1.1", "express-async-errors": "^3.1.1",
"file-type": "^16.5.0", "file-type": "^16.5.0",
"image-size": "^1.0.0",
"lambert-db": "^1.2.3", "lambert-db": "^1.2.3",
"lambert-server": "^1.2.1", "lambert-server": "^1.2.1",
"missing-native-js-functions": "^1.0.8", "missing-native-js-functions": "^1.0.8",
@ -35,9 +36,9 @@
} }
}, },
"node_modules/@fosscord/server-util": { "node_modules/@fosscord/server-util": {
"version": "1.3.8", "version": "1.3.10",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.8.tgz", "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.10.tgz",
"integrity": "sha512-bqCoCcuXRCDvloWcmQDSGVEAeHTgme4idBquL93Q/AxVe0l8J2hv+qm6bJ9mtK+TYPJhUlzku4H+jnMbH9msGg==", "integrity": "sha512-pu+XAoerl/WLFxoNxT1NV7Nj0QT+QigK5ghr1VCXkN5N/pUAJUyC72fJPYk+5Ug0CbJkPb0XNsRVJpuz8k0R2g==",
"dependencies": { "dependencies": {
"@types/jsonwebtoken": "^8.5.0", "@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-autopopulate": "^0.10.1",
@ -849,6 +850,20 @@
} }
] ]
}, },
"node_modules/image-size": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.0.tgz",
"integrity": "sha512-JLJ6OwBfO1KcA+TvJT+v8gbE6iWbj24LyDNFgFEN0lzegn6cC6a/p3NIDaepMsJjQjlUWqIC7wJv8lBFxPNjcw==",
"dependencies": {
"queue": "6.0.2"
},
"bin": {
"image-size": "bin/image-size.js"
},
"engines": {
"node": ">=12.0.0"
}
},
"node_modules/inherits": { "node_modules/inherits": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@ -1329,6 +1344,14 @@
"node": ">=0.6" "node": ">=0.6"
} }
}, },
"node_modules/queue": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz",
"integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==",
"dependencies": {
"inherits": "~2.0.3"
}
},
"node_modules/range-parser": { "node_modules/range-parser": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -1658,9 +1681,9 @@
}, },
"dependencies": { "dependencies": {
"@fosscord/server-util": { "@fosscord/server-util": {
"version": "1.3.8", "version": "1.3.10",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.8.tgz", "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.10.tgz",
"integrity": "sha512-bqCoCcuXRCDvloWcmQDSGVEAeHTgme4idBquL93Q/AxVe0l8J2hv+qm6bJ9mtK+TYPJhUlzku4H+jnMbH9msGg==", "integrity": "sha512-pu+XAoerl/WLFxoNxT1NV7Nj0QT+QigK5ghr1VCXkN5N/pUAJUyC72fJPYk+5Ug0CbJkPb0XNsRVJpuz8k0R2g==",
"requires": { "requires": {
"@types/jsonwebtoken": "^8.5.0", "@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-autopopulate": "^0.10.1",
@ -2353,6 +2376,14 @@
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA=="
}, },
"image-size": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.0.tgz",
"integrity": "sha512-JLJ6OwBfO1KcA+TvJT+v8gbE6iWbj24LyDNFgFEN0lzegn6cC6a/p3NIDaepMsJjQjlUWqIC7wJv8lBFxPNjcw==",
"requires": {
"queue": "6.0.2"
}
},
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@ -2733,6 +2764,14 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
}, },
"queue": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz",
"integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==",
"requires": {
"inherits": "~2.0.3"
}
},
"range-parser": { "range-parser": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",

View File

@ -20,7 +20,7 @@
}, },
"homepage": "https://github.com/discord-open-source/discord-cdn#readme", "homepage": "https://github.com/discord-open-source/discord-cdn#readme",
"dependencies": { "dependencies": {
"@fosscord/server-util": "^1.3.8", "@fosscord/server-util": "^1.3.10",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"btoa": "^1.2.1", "btoa": "^1.2.1",
"cheerio": "^1.0.0-rc.5", "cheerio": "^1.0.0-rc.5",
@ -28,6 +28,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"express-async-errors": "^3.1.1", "express-async-errors": "^3.1.1",
"file-type": "^16.5.0", "file-type": "^16.5.0",
"image-size": "^1.0.0",
"lambert-db": "^1.2.3", "lambert-db": "^1.2.3",
"lambert-server": "^1.2.1", "lambert-server": "^1.2.1",
"missing-native-js-functions": "^1.0.8", "missing-native-js-functions": "^1.0.8",

View File

@ -31,8 +31,8 @@ export class CDNServer extends Server {
export const multer = multerConfig({ export const multer = multerConfig({
storage: multerConfig.memoryStorage(), storage: multerConfig.memoryStorage(),
limits: { limits: {
fields: 0, fields: 10,
files: 1, files: 10,
fileSize: 1024 * 1024 * 100, // 100 mb fileSize: 1024 * 1024 * 100, // 100 mb
}, },
}); });

View File

@ -4,10 +4,14 @@ import { storage } from "../util/Storage";
import FileType from "file-type"; import FileType from "file-type";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { multer } from "../Server"; import { multer } from "../Server";
import imageSize from "image-size";
const router = Router(); const router = Router();
router.post("/:channel_id", multer.single("file"), async (req, res) => { router.post("/:channel_id", multer.single("file"), async (req, res) => {
if (req.headers.signature !== Config.get().security.requestSignature)
throw new HTTPError("Invalid request signature");
const { buffer, mimetype, size, originalname, fieldname } = req.file; const { buffer, mimetype, size, originalname, fieldname } = req.file;
const { channel_id } = req.params; const { channel_id } = req.params;
const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, ""); const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, "");
@ -17,6 +21,15 @@ router.post("/:channel_id", multer.single("file"), async (req, res) => {
const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
await storage.set(path, buffer); 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 = { const file = {
id, id,
@ -24,6 +37,8 @@ router.post("/:channel_id", multer.single("file"), async (req, res) => {
filename: filename, filename: filename,
size, size,
url: `${endpoint}/${path}`, url: `${endpoint}/${path}`,
width,
height,
}; };
return res.json(file); return res.json(file);
@ -42,6 +57,9 @@ router.get("/:channel_id/:id/:filename", async (req, res) => {
}); });
router.delete("/:channel_id/:id/:filename", async (req, res) => { router.delete("/:channel_id/:id/:filename", async (req, res) => {
if (req.headers.signature !== Config.get().security.requestSignature)
throw new HTTPError("Invalid request signature");
const { channel_id, id, filename } = req.params; const { channel_id, id, filename } = req.params;
const path = `attachments/${channel_id}/${id}/${filename}`; const path = `attachments/${channel_id}/${id}/${filename}`;

View File

@ -4,6 +4,7 @@ import { storage } from "../util/Storage";
import FileType from "file-type"; import FileType from "file-type";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { multer } from "../Server"; import { multer } from "../Server";
import crypto from "crypto";
// TODO: check premium and animated pfp are allowed in the config // TODO: check premium and animated pfp are allowed in the config
// TODO: generate different sizes of avatar // TODO: generate different sizes of avatar
@ -18,10 +19,13 @@ const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES];
const router = Router(); const router = Router();
router.post("/:user_id", multer.single("file"), async (req, res) => { router.post("/:user_id", multer.single("file"), async (req, res) => {
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 { buffer, mimetype, size, originalname, fieldname } = req.file;
const { user_id } = req.params; const { user_id } = req.params;
const id = Snowflake.generate(); const id = crypto.createHash("md5").update(Snowflake.generate()).digest("hex");
const type = await FileType.fromBuffer(buffer); 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");
@ -39,7 +43,8 @@ router.post("/:user_id", multer.single("file"), async (req, res) => {
}); });
router.get("/:user_id/:id", async (req, res) => { router.get("/:user_id/:id", async (req, res) => {
const { user_id, id } = req.params; var { user_id, id } = req.params;
id = id.split(".")[0];
const path = `avatars/${user_id}/${id}`; const path = `avatars/${user_id}/${id}`;
const file = await storage.get(path); const file = await storage.get(path);
@ -52,6 +57,8 @@ router.get("/:user_id/:id", async (req, res) => {
}); });
router.delete("/:user_id/:id", async (req, res) => { router.delete("/:user_id/:id", async (req, res) => {
if (req.headers.signature !== Config.get().security.requestSignature)
throw new HTTPError("Invalid request signature");
const { user_id, id } = req.params; const { user_id, id } = req.params;
const path = `avatars/${user_id}/${id}`; const path = `avatars/${user_id}/${id}`;

View File

@ -30,6 +30,8 @@ const DEFAULT_FETCH_OPTIONS: any = {
}; };
router.post("/", bodyParser.json(), async (req, res) => { router.post("/", bodyParser.json(), async (req, res) => {
if (req.headers.signature !== Config.get().security.requestSignature)
throw new HTTPError("Invalid request signature");
if (!req.body) throw new HTTPError("Invalid Body"); if (!req.body) throw new HTTPError("Invalid Body");
const { url } = req.body; const { url } = req.body;
if (!url || typeof url !== "string") throw new HTTPError("Invalid url"); if (!url || typeof url !== "string") throw new HTTPError("Invalid url");