🎨 add handleFile()
This commit is contained in:
parent
cbb62a8bb5
commit
c396fac204
14
package-lock.json
generated
14
package-lock.json
generated
@ -9,7 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fosscord/server-util": "^1.3.38",
|
"@fosscord/server-util": "^1.3.39",
|
||||||
"@types/jest": "^26.0.22",
|
"@types/jest": "^26.0.22",
|
||||||
"@types/json-schema": "^7.0.7",
|
"@types/json-schema": "^7.0.7",
|
||||||
"ajv": "^8.4.0",
|
"ajv": "^8.4.0",
|
||||||
@ -678,9 +678,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@fosscord/server-util": {
|
"node_modules/@fosscord/server-util": {
|
||||||
"version": "1.3.38",
|
"version": "1.3.39",
|
||||||
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.38.tgz",
|
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.39.tgz",
|
||||||
"integrity": "sha512-TYbs9/Nwx29bWjQwimW/yChHIg/qRoRdgjvTVBKHamEfvZ9XtOnAyvodS1dTys7vJvQJgMzFeZth24NuHm+YFQ==",
|
"integrity": "sha512-skDV2SMBsKMy0Ngz73aXHi9Nkhx6V6FH9pi9TuiJam976g2Dv/QWaBgfPgPNYu6euta986PTFrOqi8R8D2WYWw==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
"@types/mongoose-autopopulate": "^0.10.1",
|
"@types/mongoose-autopopulate": "^0.10.1",
|
||||||
@ -11457,9 +11457,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@fosscord/server-util": {
|
"@fosscord/server-util": {
|
||||||
"version": "1.3.38",
|
"version": "1.3.39",
|
||||||
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.38.tgz",
|
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.39.tgz",
|
||||||
"integrity": "sha512-TYbs9/Nwx29bWjQwimW/yChHIg/qRoRdgjvTVBKHamEfvZ9XtOnAyvodS1dTys7vJvQJgMzFeZth24NuHm+YFQ==",
|
"integrity": "sha512-skDV2SMBsKMy0Ngz73aXHi9Nkhx6V6FH9pi9TuiJam976g2Dv/QWaBgfPgPNYu6euta986PTFrOqi8R8D2WYWw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
"@types/mongoose-autopopulate": "^0.10.1",
|
"@types/mongoose-autopopulate": "^0.10.1",
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fosscord/server-util": "^1.3.38",
|
"@fosscord/server-util": "^1.3.39",
|
||||||
"@types/jest": "^26.0.22",
|
"@types/jest": "^26.0.22",
|
||||||
"@types/json-schema": "^7.0.7",
|
"@types/json-schema": "^7.0.7",
|
||||||
"ajv": "^8.4.0",
|
"ajv": "^8.4.0",
|
||||||
|
@ -17,7 +17,7 @@ import { HTTPError } from "lambert-server";
|
|||||||
import { GuildUpdateSchema } from "../../../schema/Guild";
|
import { GuildUpdateSchema } from "../../../schema/Guild";
|
||||||
import { emitEvent } from "../../../util/Event";
|
import { emitEvent } from "../../../util/Event";
|
||||||
import { check } from "../../../util/instanceOf";
|
import { check } from "../../../util/instanceOf";
|
||||||
import { uploadFile } from "../../../util/cdn";
|
import { handleFile } from "../../../util/cdn";
|
||||||
import "missing-native-js-functions";
|
import "missing-native-js-functions";
|
||||||
|
|
||||||
const router = Router();
|
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);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
|
|
||||||
if (body.icon && body.icon.startsWith('data')) {
|
body.icon = await handleFile("icons", body.icon);
|
||||||
try {
|
body.banner = await handleFile("banners", body.banner);
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
|
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
|
||||||
.populate({ path: "joined_at", match: { id: req.user_id } })
|
.populate({ path: "joined_at", match: { id: req.user_id } })
|
||||||
|
@ -4,7 +4,7 @@ import { HTTPError } from "lambert-server";
|
|||||||
import { getPublicUser } from "../../../util/User";
|
import { getPublicUser } from "../../../util/User";
|
||||||
import { UserModifySchema } from "../../../schema/User";
|
import { UserModifySchema } from "../../../schema/User";
|
||||||
import { check } from "../../../util/instanceOf";
|
import { check } from "../../../util/instanceOf";
|
||||||
import { uploadFile } from "../../../util/cdn";
|
import { handleFile } from "../../../util/cdn";
|
||||||
|
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
@ -14,19 +14,7 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => {
|
router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => {
|
||||||
const body = req.body as UserModifySchema;
|
const body = req.body as UserModifySchema;
|
||||||
|
body.avatar = await handleFile(body.avatar as string);
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: PublicUserProjection }).exec();
|
const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: PublicUserProjection }).exec();
|
||||||
// TODO: dispatch user update event
|
// TODO: dispatch user update event
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Config } from "@fosscord/server-util";
|
import { Config } from "@fosscord/server-util";
|
||||||
import FormData from "form-data";
|
import FormData from "form-data";
|
||||||
|
import { HTTPError } from "lambert-server";
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
export async function uploadFile(path: string, file: Express.Multer.File) {
|
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;
|
if (response.status !== 200) throw result;
|
||||||
return 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user