1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

🎨 add handleFile()

This commit is contained in:
Flam3rboy 2021-08-07 19:15:48 +02:00
parent cbb62a8bb5
commit c396fac204
5 changed files with 28 additions and 48 deletions

14
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -17,7 +17,7 @@ import { HTTPError } from "lambert-server";
import { GuildUpdateSchema } from "../../../schema/Guild";
import { emitEvent } from "../../../util/Event";
import { check } from "../../../util/instanceOf";
import { uploadFile } from "../../../util/cdn";
import { handleFile } from "../../../util/cdn";
import "missing-native-js-functions";
const router = Router();
@ -43,31 +43,8 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_GUILD");
if (body.icon && body.icon.startsWith('data')) {
try {
const mimetype = body.icon.split(":")[1].split(";")[0];
const buffer = Buffer.from(body.icon.split(",")[1], "base64");
// @ts-ignore
const { id } = await uploadFile(`/icons/${guild_id}`, { buffer, mimetype, originalname: "icon" });
body.icon = id;
} catch (error) {
throw new HTTPError("Invalid icon");
}
}
if (body.banner && body.banner.startsWith('data')) {
try {
const mimetype = body.banner.split(":")[1].split(";")[0];
const buffer = Buffer.from(body.banner.split(",")[1], "base64");
// @ts-ignore
const { id } = await uploadFile(`/banners/${guild_id}`, { buffer, mimetype, originalname: "banner" });
body.banner = id;
} catch (error) {
throw new HTTPError("Invalid banner");
}
}
body.icon = await handleFile("icons", body.icon);
body.banner = await handleFile("banners", body.banner);
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
.populate({ path: "joined_at", match: { id: req.user_id } })

View File

@ -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(body.avatar as string);
const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: PublicUserProjection }).exec();
// TODO: dispatch user update event

View File

@ -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,17 @@ 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}/${guild_id}`, { buffer, mimetype, originalname: "banner" });
return id;
} catch (error) {
throw new HTTPError("Invalid " + path);
}
}