fully added profile photo submission

This commit is contained in:
Daniel
2023-05-22 15:50:48 +03:30
parent 22d8178c88
commit 59e4dc471f
13 changed files with 194 additions and 70 deletions
+33 -8
View File
@@ -5,9 +5,38 @@
import { prisma } from "@/lib/api/db";
import { AccountSettings } from "@/types/global";
import fs from "fs";
import path from "path";
export default async function (user: AccountSettings, userId: number) {
console.log(typeof user);
console.log(console.log(user.profilePic));
const profilePic = user.profilePic;
if (profilePic && profilePic !== "DELETE") {
if ((user?.profilePic?.length as number) < 1572864) {
try {
const filePath = path.join(
process.cwd(),
`data/uploads/avatar/${userId}.jpg`
);
const base64Data = profilePic.replace(/^data:image\/jpeg;base64,/, "");
fs.writeFile(filePath, base64Data, "base64", function (err) {
console.log(err);
});
} catch (err) {
console.log("Error saving image:", err);
}
} else {
console.log("A file larger than 1.5MB was uploaded.");
}
} else if (profilePic === "DELETE") {
fs.unlink(`data/uploads/avatar/${userId}.jpg`, (err) => {
if (err) console.log(err);
});
}
const updatedUser = await prisma.user.update({
where: {
@@ -19,13 +48,9 @@ export default async function (user: AccountSettings, userId: number) {
collectionProtection: user.collectionProtection,
whitelistedUsers: user.whitelistedUsers,
},
select: {
name: true,
email: true,
collectionProtection: true,
whitelistedUsers: true,
},
});
return { response: updatedUser, status: 200 };
const { password, ...unsensitiveInfo } = updatedUser;
return { response: unsensitiveInfo, status: 200 };
}