2023-10-03 00:58:52 -04:00
|
|
|
import { v4 } from "uuid";
|
|
|
|
import { resolve } from "path";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { isLoggedIn } from "@server/middlewareButNotReally";
|
2023-10-10 22:42:59 -04:00
|
|
|
import sharp from "sharp";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { User } from "@models/user";
|
2023-10-03 00:58:52 -04:00
|
|
|
|
|
|
|
export default eventHandler(async (ev) => {
|
|
|
|
isLoggedIn(ev);
|
|
|
|
let mpd = await readMultipartFormData(ev);
|
|
|
|
if (!mpd) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let si = sharp(mpd[0].data!);
|
|
|
|
let nn = v4();
|
2023-12-30 16:35:35 -05:00
|
|
|
// const apath = await import("~/assets/avatars/" + nn + ".png");
|
2023-10-03 00:58:52 -04:00
|
|
|
await si
|
|
|
|
.resize({
|
|
|
|
height: 500,
|
|
|
|
width: 500,
|
|
|
|
fit: "outside",
|
|
|
|
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
|
|
})
|
|
|
|
.toFormat("png")
|
2023-12-30 16:35:35 -05:00
|
|
|
.toFile(resolve(process.cwd(), `assets/avatars/${nn}.png`));
|
2023-12-29 16:32:32 -05:00
|
|
|
await User.findByIdAndUpdate(ev.context.currentUser!._id, {
|
|
|
|
$set: {
|
|
|
|
"profile.avatar": nn,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-03 00:58:52 -04:00
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
file: `${nn}.png`,
|
|
|
|
};
|
|
|
|
});
|