Merge pull request #188 from BanTheNons/guild-icons-banners
Implemented guild icons and banners
This commit is contained in:
		
						commit
						31df0bfe7b
					
				
							
								
								
									
										14
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										14
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -9,7 +9,7 @@ | ||||
| 			"version": "1.0.0", | ||||
| 			"license": "ISC", | ||||
| 			"dependencies": { | ||||
| 				"@fosscord/server-util": "^1.3.38", | ||||
| 				"@fosscord/server-util": "^1.3.39", | ||||
| 				"@types/jest": "^26.0.22", | ||||
| 				"@types/json-schema": "^7.0.7", | ||||
| 				"ajv": "^8.4.0", | ||||
| @ -678,9 +678,9 @@ | ||||
| 			} | ||||
| 		}, | ||||
| 		"node_modules/@fosscord/server-util": { | ||||
| 			"version": "1.3.38", | ||||
| 			"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.38.tgz", | ||||
| 			"integrity": "sha512-TYbs9/Nwx29bWjQwimW/yChHIg/qRoRdgjvTVBKHamEfvZ9XtOnAyvodS1dTys7vJvQJgMzFeZth24NuHm+YFQ==", | ||||
| 			"version": "1.3.39", | ||||
| 			"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.39.tgz", | ||||
| 			"integrity": "sha512-skDV2SMBsKMy0Ngz73aXHi9Nkhx6V6FH9pi9TuiJam976g2Dv/QWaBgfPgPNYu6euta986PTFrOqi8R8D2WYWw==", | ||||
| 			"dependencies": { | ||||
| 				"@types/jsonwebtoken": "^8.5.0", | ||||
| 				"@types/mongoose-autopopulate": "^0.10.1", | ||||
| @ -11457,9 +11457,9 @@ | ||||
| 			} | ||||
| 		}, | ||||
| 		"@fosscord/server-util": { | ||||
| 			"version": "1.3.38", | ||||
| 			"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.38.tgz", | ||||
| 			"integrity": "sha512-TYbs9/Nwx29bWjQwimW/yChHIg/qRoRdgjvTVBKHamEfvZ9XtOnAyvodS1dTys7vJvQJgMzFeZth24NuHm+YFQ==", | ||||
| 			"version": "1.3.39", | ||||
| 			"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.39.tgz", | ||||
| 			"integrity": "sha512-skDV2SMBsKMy0Ngz73aXHi9Nkhx6V6FH9pi9TuiJam976g2Dv/QWaBgfPgPNYu6euta986PTFrOqi8R8D2WYWw==", | ||||
| 			"requires": { | ||||
| 				"@types/jsonwebtoken": "^8.5.0", | ||||
| 				"@types/mongoose-autopopulate": "^0.10.1", | ||||
|  | ||||
| @ -33,7 +33,7 @@ | ||||
| 	}, | ||||
| 	"homepage": "https://github.com/fosscord/fosscord-api#readme", | ||||
| 	"dependencies": { | ||||
| 		"@fosscord/server-util": "^1.3.38", | ||||
| 		"@fosscord/server-util": "^1.3.39", | ||||
| 		"@types/jest": "^26.0.22", | ||||
| 		"@types/json-schema": "^7.0.7", | ||||
| 		"ajv": "^8.4.0", | ||||
|  | ||||
| @ -17,6 +17,7 @@ import { HTTPError } from "lambert-server"; | ||||
| import { GuildUpdateSchema } from "../../../schema/Guild"; | ||||
| import { emitEvent } from "../../../util/Event"; | ||||
| import { check } from "../../../util/instanceOf"; | ||||
| import { handleFile } from "../../../util/cdn"; | ||||
| import "missing-native-js-functions"; | ||||
| 
 | ||||
| const router = Router(); | ||||
| @ -42,6 +43,9 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) | ||||
| 	const perms = await getPermission(req.user_id, guild_id); | ||||
| 	perms.hasThrow("MANAGE_GUILD"); | ||||
| 
 | ||||
| 	body.icon = await handleFile(`/icons/${guild_id}`, body.icon); | ||||
| 	body.banner = await handleFile(`/banners/${guild_id}`, body.banner); | ||||
| 
 | ||||
| 	const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body) | ||||
| 		.populate({ path: "joined_at", match: { id: req.user_id } }) | ||||
| 		.exec(); | ||||
| @ -50,7 +54,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) | ||||
| 
 | ||||
| 	emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent); | ||||
| 
 | ||||
| 	return res.send(data); | ||||
| 	return res.json(data); | ||||
| }); | ||||
| 
 | ||||
| export default router; | ||||
|  | ||||
| @ -4,7 +4,7 @@ import { HTTPError } from "lambert-server"; | ||||
| import { getPublicUser } from "../../../util/User"; | ||||
| import { UserModifySchema } from "../../../schema/User"; | ||||
| import { check } from "../../../util/instanceOf"; | ||||
| import { uploadFile } from "../../../util/cdn"; | ||||
| import { handleFile } from "../../../util/cdn"; | ||||
| 
 | ||||
| const router: Router = Router(); | ||||
| 
 | ||||
| @ -14,19 +14,7 @@ router.get("/", async (req: Request, res: Response) => { | ||||
| 
 | ||||
| router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { | ||||
| 	const body = req.body as UserModifySchema; | ||||
| 
 | ||||
| 	if (body.avatar) { | ||||
| 		try { | ||||
| 			const mimetype = body.avatar.split(":")[1].split(";")[0]; | ||||
| 			const buffer = Buffer.from(body.avatar.split(",")[1], "base64"); | ||||
| 
 | ||||
| 			// @ts-ignore
 | ||||
| 			const { id } = await uploadFile(`/avatars/${req.user_id}`, { buffer, mimetype, originalname: "avatar" }); | ||||
| 			body.avatar = id; | ||||
| 		} catch (error) { | ||||
| 			throw new HTTPError("Invalid avatar"); | ||||
| 		} | ||||
| 	} | ||||
| 	body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string); | ||||
| 
 | ||||
| 	const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: PublicUserProjection }).exec(); | ||||
| 	// TODO: dispatch user update event
 | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| import { Config } from "@fosscord/server-util"; | ||||
| import FormData from "form-data"; | ||||
| import { HTTPError } from "lambert-server"; | ||||
| import fetch from "node-fetch"; | ||||
| 
 | ||||
| export async function uploadFile(path: string, file: Express.Multer.File) { | ||||
| @ -22,3 +23,18 @@ export async function uploadFile(path: string, file: Express.Multer.File) { | ||||
| 	if (response.status !== 200) throw result; | ||||
| 	return result; | ||||
| } | ||||
| 
 | ||||
| export async function handleFile(path: string, body?: string): Promise<string | undefined> { | ||||
| 	if (!body || !body.startsWith("data:")) return body; | ||||
| 	try { | ||||
| 		const mimetype = body.split(":")[1].split(";")[0]; | ||||
| 		const buffer = Buffer.from(body.split(",")[1], "base64"); | ||||
| 
 | ||||
| 		// @ts-ignore
 | ||||
| 		const { id } = await uploadFile(path, { buffer, mimetype, originalname: "banner" }); | ||||
| 		return id; | ||||
| 	} catch (error) { | ||||
| 		console.error(error); | ||||
| 		throw new HTTPError("Invalid icon"); | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Flam3rboy
						Flam3rboy