import { v4 } from "uuid";
import { resolve } from "path";
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
import * as sharp from "sharp";

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();
	await si
		.resize({
			height: 500,
			width: 500,
			fit: "outside",
			background: { r: 0, g: 0, b: 0, alpha: 0 },
		})
		.toFormat("png")
		.toFile(resolve(`public/${nn}.png`));
	return {
		success: true,
		file: `${nn}.png`,
	};
});
